diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index c76d0ca3dc..156769080a 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1272,7 +1272,7 @@ def set_tag(self, key, value): def set_data(self, key, value): # type: (str, Any) -> None - pass + self._otel_span.set_attribute(key, value) def set_status(self, status): # type: (str) -> None diff --git a/tests/integrations/opentelemetry/test_potel.py b/tests/integrations/opentelemetry/test_potel.py index deaa0df8c9..1d315b9974 100644 --- a/tests/integrations/opentelemetry/test_potel.py +++ b/tests/integrations/opentelemetry/test_potel.py @@ -208,3 +208,45 @@ def test_children_span_nesting_mixed(capture_envelopes): assert db_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] assert http_span["parent_span_id"] == payload["contexts"]["trace"]["span_id"] assert redis_span["parent_span_id"] == db_span["span_id"] + + +def test_span_attributes_in_data_started_with_otel(capture_envelopes): + envelopes = capture_envelopes() + + with tracer.start_as_current_span("request") as request_span: + request_span.set_attributes({"foo": "bar", "baz": 42}) + with tracer.start_as_current_span("db") as db_span: + db_span.set_attributes({"abc": 99, "def": "moo"}) + + (envelope,) = envelopes + (item,) = envelope.items + payload = item.payload.json + + assert payload["contexts"]["trace"]["data"] == {"foo": "bar", "baz": 42} + assert payload["spans"][0]["data"] == {"abc": 99, "def": "moo"} + + +def test_span_data_started_with_sentry(capture_envelopes): + envelopes = capture_envelopes() + + with sentry_sdk.start_span(op="http", description="request") as request_span: + request_span.set_data("foo", "bar") + with sentry_sdk.start_span(op="db", description="statement") as db_span: + db_span.set_data("baz", 42) + + (envelope,) = envelopes + (item,) = envelope.items + payload = item.payload.json + + assert payload["contexts"]["trace"]["data"] == { + "foo": "bar", + "sentry.origin": "manual", + "sentry.description": "request", + "sentry.op": "http", + } + assert payload["spans"][0]["data"] == { + "baz": 42, + "sentry.origin": "manual", + "sentry.description": "statement", + "sentry.op": "db", + }