8000 Origin improvements (#3432) · getsentry/sentry-python@52fca29 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52fca29

Browse files
authored
Origin improvements (#3432)
1 parent 76ccff5 commit 52fca29

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
@@ -259,7 +259,7 @@ def _update_span_with_otel_data(self, sentry_span, otel_span):
259259
for key, val in otel_span.attributes.items():
260260
sentry_span.set_data(key, val)
261261

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

@@ -270,7 +270,7 @@ def _update_span_with_otel_data(self, sentry_span, otel_span):
270270

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

276276
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.utils import Dsn
@@ -77,13 +78,17 @@ def convert_otel_timestamp(time):
7778

7879

7980
def extract_span_data(span):
80-
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int]]
81+
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int], Optional[str]]
8182
op = span.name
8283
description = span.name
8384
status, http_status = extract_span_status(span)
85+
origin = None
8486

8587
if span.attributes is None:
86-
return (op, description, status, http_status)
88+
return (op, description, status, http_status, origin)
89+
90+
origin = span.attributes.get(SentrySpanAttribute.ORIGIN)
91+
description = span.attributes.get(SentrySpanAttribute.DESCRIPTION) or description
8792

8893
http_method = span.attributes.get(SpanAttributes.HTTP_METHOD)
8994
http_method = cast("Optional[str]", http_method)
@@ -96,26 +101,21 @@ def extract_span_data(span):
96101

97102
rpc_service = span.attributes.get(SpanAttributes.RPC_SERVICE)
98103
if rpc_service:
99-
return ("rpc", description, status, http_status)
104+
return ("rpc", description, status, http_status, origin)
100105

101106
messaging_system = span.attributes.get(SpanAttributes.MESSAGING_SYSTEM)
102107
if messaging_system:
103-
return ("message", description, status, http_status)
108+
return ("message", description, status, http_status, origin)
104109

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

114-
return (op, description, status, http_status)
114+
return (op, description, status, http_status, origin)
115115

116116

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

121121
op = "http"
@@ -151,11 +151,13 @@ def span_data_for_http_method(span):
151151

152152
status, http_status = extract_span_status(span)
153153

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

156158

157159
def span_data_for_db_query(span):
158-
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int]]
160+
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int], Optional[str]]
159161
span_attributes = span.attributes or {}
160162

161163
op = "db"
@@ -164,8 +166,9 @@ def span_data_for_db_query(span):
164166
statement = cast("Optional[str]", statement)
165167

166168
description = statement or span.name
169+
origin = span_attributes.get(SentrySpanAttribute.ORIGIN)
167170

168-
return (op, description, None, None)
171+
return (op, description, None, None, origin)
169172

170173

171174
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)
0