Skip to content

Commit 896fb6e

Browse files
committed
Origin improvements
1 parent 01835ed commit 896fb6e

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ def _root_span_to_transaction_event(self, span):
116116
span_id = format_span_id(span.context.span_id)
117117
parent_span_id = format_span_id(span.parent.span_id) if span.parent else None
118118

119-
(op, description, status, _) = extract_span_data(span)
119+
(op, description, status, _, origin) = extract_span_data(span)
120120

121121
trace_context = {
122122
"trace_id": trace_id,
123123
"span_id": span_id,
124-
"origin": SPAN_ORIGIN,
124+
"origin": origin,
125125
"op": op,
126126
"status": status,
127127
} # type: dict[str, Any]
@@ -160,17 +160,17 @@ def _span_to_json(self, span):
160160
span_id = format_span_id(span.context.span_id)
161161
parent_span_id = format_span_id(span.parent.span_id) if span.parent else None
162162

163-
(op, description, status, _) = extract_span_data(span)
163+
(op, description, status, _, origin) = extract_span_data(span)
164164

165165
span_json = {
166166
"trace_id": trace_id,
167167
"span_id": span_id,
168-
"origin": SPAN_ORIGIN,
169168
"op": op,
170169
"description": description,
171170
"status": status,
172171
"start_timestamp": convert_otel_timestamp(span.start_time),
173172
"timestamp": convert_otel_timestamp(span.end_time),
173+
"origin": origin or SPAN_ORIGIN,
174174
} # type: dict[str, Any]
175175

176176
if parent_span_id:

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def _update_span_with_otel_data(self, sentry_span, otel_span):
258258
for key, val in otel_span.attributes.items():
259259
sentry_span.set_data(key, val)
260260

261-
(op, description, status, http_status) = extract_span_data(otel_span)
261+
(op, description, status, http_status, _) = extract_span_data(otel_span)
262262
sentry_span.op = op
263263
sentry_span.description = description
264264

@@ -269,7 +269,7 @@ def _update_span_with_otel_data(self, sentry_span, otel_span):
269269

270270
def _update_transaction_with_otel_data(self, sentry_span, otel_span):
271271
# type: (SentrySpan, OTelSpan) -> None
272-
(op, _, status, http_status) = extract_span_data(otel_span)
272+
(op, _, status, http_status, _) = extract_span_data(otel_span)
273273
sentry_span.op = op
274274

275275
if http_status:

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from opentelemetry.sdk.trace import ReadableSpan
77
from sentry_sdk.consts import SPANSTATUS
88
from sentry_sdk.tracing import get_span_status_from_http_code
9+
from sentry_sdk.integrations.opentelemetry.consts import SentrySpanAttribute
910
from urllib3.util import parse_url as urlparse
1011

1112
from sentry_sdk import get_client
@@ -76,13 +77,17 @@ def convert_otel_timestamp(time):
7677

7778

7879
def extract_span_data(span):
79-
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int]]
80+
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int], Optional[str]]
8081
op = span.name
8182
description = span.name
8283
status, http_status = extract_span_status(span)
84+
origin = None
8385

8486
if span.attributes is None:
85-
return (op, description, status, http_status)
87+
return (op, description, status, http_status, origin)
88+
89+
origin = span.attributes.get(SentrySpanAttribute.ORIGIN)
90+
description = span.attributes.get(SentrySpanAttribute.DESCRIPTION) or description
8691

8792
http_method = span.attributes.get(SpanAttributes.HTTP_METHOD)
8893
http_method = cast("Optional[str]", http_method)
@@ -95,26 +100,21 @@ def extract_span_data(span):
95100

96101
rpc_service = span.attributes.get(SpanAttributes.RPC_SERVICE)
97102
if rpc_service:
98-
return ("rpc", description, status, http_status)
103+
return ("rpc", description, status, http_status, origin)
99104

100105
messaging_system = span.attributes.get(SpanAttributes.MESSAGING_SYSTEM)
101106
if messaging_system:
102-
return ("message", description, status, http_status)
107+
return ("message", description, status, http_status, origin)
103108

104109
faas_trigger = span.attributes.get(SpanAttributes.FAAS_TRIGGER)
105110
if faas_trigger:
106-
return (
107-
str(faas_trigger),
108-
description,
109-
status,
110-
http_status,
111-
)
111+
return (str(faas_trigger), description, status, http_status, origin)
112112

113-
return (op, description, status, http_status)
113+
return (op, description, status, http_status, origin)
114114

115115

116116
def span_data_for_http_method(span):
117-
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int]]
117+
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int], Optional[str]]
118118
span_attributes = span.attributes or {}
119119

120120
op = "http"
@@ -150,11 +150,13 @@ def span_data_for_http_method(span):
150150

151151
status, http_status = extract_span_status(span)
152152

153-
return (op, description, status, http_status)
153+
origin = span_attributes.get(SentrySpanAttribute.ORIGIN)
154+
155+
return (op, description, status, http_status, origin)
154156

155157

156158
def span_data_for_db_query(span):
157-
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int]]
159+
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int], Optional[str]]
158160
span_attributes = span.attributes or {}
159161

160162
op = "db"
@@ -163,8 +165,9 @@ def span_data_for_db_query(span):
163165
statement = cast("Optional[str]", statement)
164166

165167
description = statement or span.name
168+
origin = span_attributes.get(SentrySpanAttribute.ORIGIN)
166169

167-
return (op, description, None, None)
170+
return (op, description, None, None, origin)
168171

169172

170173
def extract_span_status(span):

tests/integrations/opentelemetry/test_potel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_root_span_transaction_payload_started_with_otel_only(capture_envelopes)
4141
trace_context = contexts["trace"]
4242
assert "trace_id" in trace_context
4343
assert "span_id" in trace_context
44-
assert trace_context["origin"] == "auto.otel"
44+
assert trace_context["origin"] == "manual"
4545
assert trace_context["op"] == "request"
4646
assert trace_context["status"] == "ok"
4747

@@ -62,7 +62,7 @@ def test_child_span_payload_started_with_otel_only(capture_envelopes):
6262

6363
assert span["op"] == "db"
6464
assert span["description"] == "db"
65-
assert span["origin"] == "auto.otel"
65+
assert span["origin"] == "manual"
6666
assert span["status"] == "ok"
6767
assert span["span_id"] is not None
6868
assert span["trace_id"] == payload["contexts"]["trace"]["trace_id"]
@@ -124,7 +124,7 @@ def test_root_span_transaction_payload_started_with_sentry_only(capture_envelope
124124
trace_context = contexts["trace"]
125125
assert "trace_id" in trace_context
126126
assert "span_id" in trace_context
127-
assert trace_context["origin"] == "auto.otel"
127+
assert trace_context["origin"] == "manual"
128128
assert trace_context["op"] == "request"
129129
assert trace_context["status"] == "ok"
130130

@@ -145,7 +145,7 @@ def test_child_span_payload_started_with_sentry_only(capture_envelopes):
145145

146146
assert span["op"] == "db"
147147
assert span["description"] == "db"
148-
assert span["origin"] == "auto.otel"
148+
assert span["origin"] == "manual"
149149
assert span["status"] == "ok"
150150
assert span["span_id"] is not None
151151
assert span["trace_id"] == payload["contexts"]["trace"]["trace_id"]

tests/integrations/opentelemetry/test_utils.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"description": "OTel Span Blank",
2424
"status": "ok",
2525
"http_status_code": None,
26+
"origin": None,
2627
},
2728
),
2829
(
@@ -36,6 +37,7 @@
3637
"description": "OTel Span RPC",
3738
"status": "ok",
3839
"http_status_code": None,
40+
"origin": None,
3941
},
4042
),
4143
(
@@ -49,6 +51,7 @@
4951
"description": "OTel Span Messaging",
5052
"status": "ok",
5153
"http_status_code": None,
54+
"origin": None,
5255
},
5356
),
5457
(
@@ -62,6 +65,7 @@
6265
"description": "OTel Span FaaS",
6366
"status": "ok",
6467
"http_status_code": None,
68+
"origin": None,
6569
},
6670
),
6771
],
@@ -72,12 +76,13 @@ def test_extract_span_data(name, status, attributes, expected):
7276
otel_span.status = Status(StatusCode.UNSET)
7377
otel_span.attributes = attributes
7478

75-
op, description, status, http_status_code = extract_span_data(otel_span)
79+
op, description, status, http_status_code, origin = extract_span_data(otel_span)
7680
result = {
7781
"op": op,
7882
"description": description,
7983
"status": status,
8084
"http_status_code": http_status_code,
85+
"origin": origin,
8186
}
8287
assert result == expected
8388

@@ -99,6 +104,7 @@ def test_extract_span_data(name, status, attributes, expected):
99104
"description": "GET",
100105
"status": "ok",
101106
"http_status_code": None,
107+
"origin": None,
102108
},
103109
),
104110
(
@@ -113,6 +119,7 @@ def test_extract_span_data(name, status, attributes, expected):
113119
"description": "GET /target",
114120
"status": "ok",
115121
"http_status_code": None,
122+
"origin": None,
116123
},
117124
),
118125
(
@@ -127,6 +134,7 @@ def test_extract_span_data(name, status, attributes, expected):
127134
"description": "GET example.com",
128135
"status": "ok",
129136
"http_status_code": None,
137+
"origin": None,
130138
},
131139
),
132140
(
@@ -142,6 +150,7 @@ def test_extract_span_data(name, status, attributes, expected):
142150
"description": "GET /target",
143151
"status": "ok",
144152
"http_status_code": None,
153+
"origin": None,
145154
},
146155
),
147156
(
@@ -156,6 +165,7 @@ def test_extract_span_data(name, status, attributes, expected):
156165
"description": "GET https://example.com/bla/",
157166
"status": "ok",
158167
"http_status_code": None,
168+
"origin": None,
159169
},
160170
),
161171
],
@@ -166,12 +176,15 @@ def test_span_data_for_http_method(kind, status, attributes, expected):
166176
otel_span.status = status
167177
otel_span.attributes = attributes
168178

169-
op, description, status, http_status_code = span_data_for_http_method(otel_span)
179+
op, description, status, http_status_code, origin = span_data_for_http_method(
180+
otel_span
181+
)
170182
result = {
171183
"op": op,
172184
"description": description,
173185
"status": status,
174186
"http_status_code": http_status_code,
187+
"origin": origin,
175188
}
176189
assert result == expected
177190

@@ -181,19 +194,21 @@ def test_span_data_for_db_query():
181194
otel_span.name = "OTel Span"
182195
otel_span.attributes = {}
183196

184-
op, description, status, http_status = span_data_for_db_query(otel_span)
197+
op, description, status, http_status, origin = span_data_for_db_query(otel_span)
185198
assert op == "db"
186199
assert description == "OTel Span"
187200
assert status is None
188201
assert http_status is None
202+
assert origin is None
189203

190204
otel_span.attributes = {"db.statement": "SELECT * FROM table;"}
191205

192-
op, description, status, http_status = span_data_for_db_query(otel_span)
206+
op, description, status, http_status, origin = span_data_for_db_query(otel_span)
193207
assert op == "db"
194208
assert description == "SELECT * FROM table;"
195209
assert status is None
196210
assert http_status is None
211+
assert origin is None
197212

198213

199214
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)