8000 Make sure op/name/description are set correctly on transactions/spans… · getsentry/sentry-python@13441e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13441e3

Browse files
authored
Make sure op/name/description are set correctly on transactions/spans (#3519)
Make sure that `op`, `name`, `description` are set correctly on `Span`s and `Transaction`s.
1 parent 97bf513 commit 13441e3

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from typing import cast
24
from contextlib import contextmanager
35

@@ -115,6 +117,13 @@ def start_transaction(self, custom_sampling_context=None, **kwargs):
115117

116118
def start_span(self, custom_sampling_context=None, **kwargs):
117119
# type: (Optional[SamplingContext], Any) -> POTelSpan
120+
if kwargs.get("description") is not None:
121+
warnings.warn(
122+
"The `description` parameter is deprecated. Please use `name` instead.",
123+
DeprecationWarning,
124+
stacklevel=2,
125+
)
126+
118127
return POTelSpan(**kwargs, scope=self)
119128

120129

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ def extract_span_data(span):
9696
if span.attributes is None:
9797
return (op, description, status, http_status, origin)
9898

99-
origin = span.attributes.get(SentrySpanAttribute.ORIGIN)
99+
op = span.attributes.get(SentrySpanAttribute.OP) or op
100100
description = span.attributes.get(SentrySpanAttribute.DESCRIPTION) or description
101+
origin = span.attributes.get(SentrySpanAttribute.ORIGIN)
101102

102103
http_method = span.attributes.get(SpanAttributes.HTTP_METHOD)
103104
http_method = cast("Optional[str]", http_method)

sentry_sdk/tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ def __init__(
12211221
start_timestamp = convert_to_otel_timestamp(start_timestamp)
12221222

12231223
self._otel_span = tracer.start_span(
1224-
description or op or "", start_time=start_timestamp
1224+
name or description or op or "", start_time=start_timestamp
12251225
)
12261226

12271227
self.origin = origin or DEFAULT_SPAN_ORIGIN
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sentry_sdk
2+
3+
4+
def test_transaction_name_span_description_compat(
5+
sentry_init,
6+
capture_events,
7+
):
8+
sentry_init(traces_sample_rate=1.0)
9+
10+
events = capture_events()
11+
12+
with sentry_sdk.start_transaction(
13+
name="trx-name",
14+
op="trx-op",
15+
) as trx:
16+
with sentry_sdk.start_span(
17+
description="span-desc",
18+
op="span-op",
19+
) as spn:
20+
...
21+
22+
assert trx.__class__.__name__ == "POTelSpan"
23+
assert trx.op == "trx-op"
24+
assert trx.name == "trx-name"
25+
assert trx.description is None
26+
27+
assert trx._otel_span is not None
28+
assert trx._otel_span.name == "trx-name"
29+
assert trx._otel_span.attributes["sentry.op"] == "trx-op"
30+
assert trx._otel_span.attributes["sentry.name"] == "trx-name"
31+
assert "sentry.description" not in trx._otel_span.attributes
32+
33+
assert spn.__class__.__name__ == "POTelSpan"
34+
assert spn.op == "span-op"
35+
assert spn.description == "span-desc"
36+
assert spn.name is None
37+
38+
assert spn._otel_span is not None
39+
assert spn._otel_span.name == "span-desc"
40+
assert spn._otel_span.attributes["sentry.op"] == "span-op"
41+
assert spn._otel_span.attributes["sentry.description"] == "span-desc"
42+
assert "sentry.name" not in spn._otel_span.attributes
43+
44+
transaction = events[0]
45+
assert transaction["transaction"] == "trx-name"
46+
assert transaction["contexts"]["trace"]["op"] == "trx-op"
47+
assert transaction["contexts"]["trace"]["data"]["sentry.op"] == "trx-op"
48+
assert transaction["contexts"]["trace"]["data"]["sentry.name"] == "trx-name"
49+
assert "sentry.description" not in transaction["contexts"]["trace"]["data"]
50+
51+
span = transaction["spans"][0]
52+
assert span["description"] == "span-desc"
53+
assert span["op"] == "span-op"
54+
assert span["data"]["sentry.op"] == "span-op"
55+
assert span["data"]["sentry.description"] == "span-desc"
56+
assert "sentry.name" not in span["data"]

0 commit comments

Comments
 (0)
0