Skip to content

Commit

Permalink
Make sure op/name/description are set correctly on transactions/spans (
Browse files Browse the repository at this point in the history
…#3519)

Make sure that `op`, `name`, `description` are set correctly on `Span`s and `Transaction`s.
  • Loading branch information
antonpirker authored Sep 12, 2024
1 parent 97bf513 commit 13441e3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
9 changes: 9 additions & 0 deletions sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from typing import cast
from contextlib import contextmanager

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

def start_span(self, custom_sampling_context=None, **kwargs):
# type: (Optional[SamplingContext], Any) -> POTelSpan
if kwargs.get("description") is not None:
warnings.warn(
"The `description` parameter is deprecated. Please use `name` instead.",
DeprecationWarning,
stacklevel=2,
)

return POTelSpan(**kwargs, scope=self)


Expand Down
3 changes: 2 additions & 1 deletion sentry_sdk/integrations/opentelemetry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def extract_span_data(span):
if span.attributes is None:
return (op, description, status, http_status, origin)

origin = span.attributes.get(SentrySpanAttribute.ORIGIN)
op = span.attributes.get(SentrySpanAttribute.OP) or op
description = span.attributes.get(SentrySpanAttribute.DESCRIPTION) or description
origin = span.attributes.get(SentrySpanAttribute.ORIGIN)

http_method = span.attributes.get(SpanAttributes.HTTP_METHOD)
http_method = cast("Optional[str]", http_method)
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ def __init__(
start_timestamp = convert_to_otel_timestamp(start_timestamp)

self._otel_span = tracer.start_span(
description or op or "", start_time=start_timestamp
name or description or op or "", start_time=start_timestamp
)

self.origin = origin or DEFAULT_SPAN_ORIGIN
Expand Down
56 changes: 56 additions & 0 deletions tests/integrations/opentelemetry/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sentry_sdk


def test_transaction_name_span_description_compat(
sentry_init,
capture_events,
):
sentry_init(traces_sample_rate=1.0)

events = capture_events()

with sentry_sdk.start_transaction(
name="trx-name",
op="trx-op",
) as trx:
with sentry_sdk.start_span(
description="span-desc",
op="span-op",
) as spn:
...

assert trx.__class__.__name__ == "POTelSpan"
assert trx.op == "trx-op"
assert trx.name == "trx-name"
assert trx.description is None

assert trx._otel_span is not None
assert trx._otel_span.name == "trx-name"
assert trx._otel_span.attributes["sentry.op"] == "trx-op"
assert trx._otel_span.attributes["sentry.name"] == "trx-name"
assert "sentry.description" not in trx._otel_span.attributes

assert spn.__class__.__name__ == "POTelSpan"
assert spn.op == "span-op"
assert spn.description == "span-desc"
assert spn.name is None

assert spn._otel_span is not None
assert spn._otel_span.name == "span-desc"
assert spn._otel_span.attributes["sentry.op"] == "span-op"
assert spn._otel_span.attributes["sentry.description"] == "span-desc"
assert "sentry.name" not in spn._otel_span.attributes

transaction = events[0]
assert transaction["transaction"] == "trx-name"
assert transaction["contexts"]["trace"]["op"] == "trx-op"
assert transaction["contexts"]["trace"]["data"]["sentry.op"] == "trx-op"
assert transaction["contexts"]["trace"]["data"]["sentry.name"] == "trx-name"
assert "sentry.description" not in transaction["contexts"]["trace"]["data"]

span = transaction["spans"][0]
assert span["description"] == "span-desc"
assert span["op"] == "span-op"
assert span["data"]["sentry.op"] == "span-op"
assert span["data"]["sentry.description"] == "span-desc"
assert "sentry.name" not in span["data"]

0 comments on commit 13441e3

Please sign in to comment.