@@ -95,7 +95,7 @@ def convert_from_otel_timestamp(time):
95
95
96
96
97
97
def convert_to_otel_timestamp (time ):
98
- # type: (Union[datetime.datetime , float]) -> int
98
+ # type: (Union[datetime, float]) -> int
99
99
"""Convert a datetime to an OTel timestamp (with nanosecond precision)."""
100
100
if isinstance (time , datetime ):
101
101
return int (time .timestamp () * 1e9 )
@@ -121,9 +121,11 @@ def extract_span_data(span):
121
121
if span .attributes is None :
122
122
return (op , description , status , http_status , origin )
123
123
124
- op = span .attributes .get (SentrySpanAttribute .OP ) or op
125
- description = span .attributes .get (SentrySpanAttribute .DESCRIPTION ) or description
126
- origin = span .attributes .get (SentrySpanAttribute .ORIGIN )
124
+ op = cast ("str" , span .attributes .get (SentrySpanAttribute .OP ) or op )
125
+ description = cast (
126
+ "str" , span .attributes .get (SentrySpanAttribute .DESCRIPTION ) or description
127
+ )
128
+ origin = cast ("Optional[str]" , span .attributes .get (SentrySpanAttribute .ORIGIN ))
127
129
128
130
http_method = span .attributes .get (SpanAttributes .HTTP_METHOD )
129
131
http_method = cast ("Optional[str]" , http_method )
@@ -137,7 +139,7 @@ def extract_span_data(span):
137
139
rpc_service = span .attributes .get (SpanAttributes .RPC_SERVICE )
138
140
if rpc_service :
139
141
return (
140
- span . attributes . get ( SentrySpanAttribute . OP ) or "rpc" ,
142
+ op or "rpc" ,
141
143
description ,
142
144
status ,
143
145
http_status ,
@@ -147,7 +149,7 @@ def extract_span_data(span):
147
149
messaging_system = span .attributes .get (SpanAttributes .MESSAGING_SYSTEM )
148
150
if messaging_system :
149
151
return (
150
- span . attributes . get ( SentrySpanAttribute . OP ) or "message" ,
152
+ op or "message" ,
151
153
description ,
152
154
status ,
153
155
http_status ,
@@ -165,7 +167,7 @@ def span_data_for_http_method(span):
165
167
# type: (ReadableSpan) -> OtelExtractedSpanData
166
168
span_attributes = span .attributes or {}
167
169
168
- op = span_attributes .get (SentrySpanAttribute .OP )
170
+ op = cast ( "Optional[str]" , span_attributes .get (SentrySpanAttribute .OP ) )
169
171
if op is None :
170
172
op = "http"
171
173
@@ -183,6 +185,7 @@ def span_data_for_http_method(span):
183
185
description = span_attributes .get (
184
186
SentrySpanAttribute .DESCRIPTION
185
187
) or span_attributes .get (SentrySpanAttribute .NAME )
188
+ description = cast ("Optional[str]" , description )
186
189
if description is None :
187
190
description = f"{ http_method } "
188
191
@@ -205,7 +208,7 @@ def span_data_for_http_method(span):
205
208
206
209
status , http_status = extract_span_status (span )
207
210
208
- origin = span_attributes .get (SentrySpanAttribute .ORIGIN )
211
+ origin = cast ( "Optional[str]" , span_attributes .get (SentrySpanAttribute .ORIGIN ) )
209
212
210
213
return (op , description , status , http_status , origin )
211
214
@@ -214,13 +217,13 @@ def span_data_for_db_query(span):
214
217
# type: (ReadableSpan) -> OtelExtractedSpanData
215
218
span_attributes = span .attributes or {}
216
219
217
- op = span_attributes .get (SentrySpanAttribute .OP , OP .DB )
220
+ op = cast ( "str" , span_attributes .get (SentrySpanAttribute .OP , OP .DB ) )
218
221
219
222
statement = span_attributes .get (SpanAttributes .DB_STATEMENT , None )
220
223
statement = cast ("Optional[str]" , statement )
221
224
222
225
description = statement or span .name
223
- origin = span_attributes .get (SentrySpanAttribute .ORIGIN )
226
+ origin = cast ( "Optional[str]" , span_attributes .get (SentrySpanAttribute .ORIGIN ) )
224
227
225
228
return (op , description , None , None , origin )
226
229
@@ -293,19 +296,20 @@ def extract_span_attributes(span, namespace):
293
296
"""
294
297
Extract Sentry-specific span attributes and make them look the way Sentry expects.
295
298
"""
296
- extracted_attrs = {}
299
+ extracted_attrs = {} # type: dict[str, Any]
297
300
298
301
for attr , value in (span .attributes or {}).items ():
299
302
if attr .startswith (namespace ):
300
303
key = attr [len (namespace ) + 1 :]
301
304
302
305
if namespace == SentrySpanAttribute .MEASUREMENT :
303
- value = {
306
+ value = cast ("tuple[str, str]" , value )
307
+ extracted_attrs [key ] = {
304
308
"value" : float (value [0 ]),
305
309
"unit" : value [1 ],
306
310
}
307
-
308
- extracted_attrs [key ] = value
311
+ else :
312
+ extracted_attrs [key ] = value
309
313
310
314
return extracted_attrs
311
315
@@ -457,7 +461,7 @@ def set_sentry_meta(span, key, value):
457
461
# type: (Union[AbstractSpan, ReadableSpan], str, Any) -> None
458
462
sentry_meta = getattr (span , "_sentry_meta" , {})
459
463
sentry_meta [key ] = value
460
- span ._sentry_meta = sentry_meta
464
+ span ._sentry_meta = sentry_meta # type: ignore[union-attr]
461
465
462
466
463
467
def get_profile_context (span ):
0 commit comments