1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
from unittest import mock
import pytest
from sentry_sdk.tracing import Transaction
from sentry_sdk.tracing_utils import extract_sentrytrace_data
@pytest.mark.parametrize("sampled", [True, False, None])
def test_to_traceparent(sampled):
transaction = Transaction(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
trace_id="12312012123120121231201212312012",
sampled=sampled,
)
traceparent = transaction.to_traceparent()
parts = traceparent.split("-")
assert parts[0] == "12312012123120121231201212312012" # trace_id
assert parts[1] == transaction.span_id # parent_span_id
if sampled is None:
assert len(parts) == 2
else:
assert parts[2] == "1" if sampled is True else "0" # sampled
@pytest.mark.parametrize("sampling_decision", [True, False])
def test_sentrytrace_extraction(sampling_decision):
sentrytrace_header = "12312012123120121231201212312012-0415201309082013-{}".format(
1 if sampling_decision is True else 0
)
assert extract_sentrytrace_data(sentrytrace_header) == {
"trace_id": "12312012123120121231201212312012",
"parent_span_id": "0415201309082013",
"parent_sampled": sampling_decision,
}
def test_iter_headers(monkeypatch):
monkeypatch.setattr(
Transaction,
"to_traceparent",
mock.Mock(return_value="12312012123120121231201212312012-0415201309082013-0"),
)
transaction = Transaction(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
)
headers = dict(transaction.iter_headers())
assert (
headers["sentry-trace"] == "12312012123120121231201212312012-0415201309082013-0"
)
|