@@ -95,7 +95,7 @@ def convert_from_otel_timestamp(time):
9595
9696
9797def convert_to_otel_timestamp (time ):
98- # type: (Union[datetime.datetime , float]) -> int
98+ # type: (Union[datetime, float]) -> int
9999 """Convert a datetime to an OTel timestamp (with nanosecond precision)."""
100100 if isinstance (time , datetime ):
101101 return int (time .timestamp () * 1e9 )
@@ -121,9 +121,12 @@ def extract_span_data(span):
121121 if span .attributes is None :
122122 return (op , description , status , http_status , origin )
123123
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+ attribute_op = cast ("Optional[str]" , span .attributes .get (SentrySpanAttribute .OP ))
125+ op = attribute_op or op
126+ description = cast (
127+ "str" , span .attributes .get (SentrySpanAttribute .DESCRIPTION ) or description
128+ )
129+ origin = cast ("Optional[str]" , span .attributes .get (SentrySpanAttribute .ORIGIN ))
127130
128131 http_method = span .attributes .get (SpanAttributes .HTTP_METHOD )
129132 http_method = cast ("Optional[str]" , http_method )
@@ -137,7 +140,7 @@ def extract_span_data(span):
137140 rpc_service = span .attributes .get (SpanAttributes .RPC_SERVICE )
138141 if rpc_service :
139142 return (
140- span . attributes . get ( SentrySpanAttribute . OP ) or "rpc" ,
143+ attribute_op or "rpc" ,
141144 description ,
142145 status ,
143146 http_status ,
@@ -147,7 +150,7 @@ def extract_span_data(span):
147150 messaging_system = span .attributes .get (SpanAttributes .MESSAGING_SYSTEM )
148151 if messaging_system :
149152 return (
150- span . attributes . get ( SentrySpanAttribute . OP ) or "message" ,
153+ attribute_op or "message" ,
151154 description ,
152155 status ,
153156 http_status ,
@@ -165,7 +168,7 @@ def span_data_for_http_method(span):
165168 # type: (ReadableSpan) -> OtelExtractedSpanData
166169 span_attributes = span .attributes or {}
167170
168- op = span_attributes .get (SentrySpanAttribute .OP )
171+ op = cast ( "Optional[str]" , span_attributes .get (SentrySpanAttribute .OP ) )
169172 if op is None :
170173 op = "http"
171174
@@ -183,6 +186,7 @@ def span_data_for_http_method(span):
183186 description = span_attributes .get (
184187 SentrySpanAttribute .DESCRIPTION
185188 ) or span_attributes .get (SentrySpanAttribute .NAME )
189+ description = cast ("Optional[str]" , description )
186190 if description is None :
187191 description = f"{ http_method } "
188192
@@ -205,7 +209,7 @@ def span_data_for_http_method(span):
205209
206210 status , http_status = extract_span_status (span )
207211
208- origin = span_attributes .get (SentrySpanAttribute .ORIGIN )
212+ origin = cast ( "Optional[str]" , span_attributes .get (SentrySpanAttribute .ORIGIN ) )
209213
210214 return (op , description , status , http_status , origin )
211215
@@ -214,13 +218,13 @@ def span_data_for_db_query(span):
214218 # type: (ReadableSpan) -> OtelExtractedSpanData
215219 span_attributes = span .attributes or {}
216220
217- op = span_attributes .get (SentrySpanAttribute .OP , OP .DB )
221+ op = cast ( "str" , span_attributes .get (SentrySpanAttribute .OP , OP .DB ) )
218222
219223 statement = span_attributes .get (SpanAttributes .DB_STATEMENT , None )
220224 statement = cast ("Optional[str]" , statement )
221225
222226 description = statement or span .name
223- origin = span_attributes .get (SentrySpanAttribute .ORIGIN )
227+ origin = cast ( "Optional[str]" , span_attributes .get (SentrySpanAttribute .ORIGIN ) )
224228
225229 return (op , description , None , None , origin )
226230
@@ -293,19 +297,20 @@ def extract_span_attributes(span, namespace):
293297 """
294298 Extract Sentry-specific span attributes and make them look the way Sentry expects.
295299 """
296- extracted_attrs = {}
300+ extracted_attrs = {} # type: dict[str, Any]
297301
298302 for attr , value in (span .attributes or {}).items ():
299303 if attr .startswith (namespace ):
300304 key = attr [len (namespace ) + 1 :]
301305
302306 if namespace == SentrySpanAttribute .MEASUREMENT :
303- value = {
307+ value = cast ("tuple[str, str]" , value )
308+ extracted_attrs [key ] = {
304309 "value" : float (value [0 ]),
305310 "unit" : value [1 ],
306311 }
307-
308- extracted_attrs [key ] = value
312+ else :
313+ extracted_attrs [key ] = value
309314
310315 return extracted_attrs
311316
@@ -457,7 +462,7 @@ def set_sentry_meta(span, key, value):
457462 # type: (Union[AbstractSpan, ReadableSpan], str, Any) -> None
458463 sentry_meta = getattr (span , "_sentry_meta" , {})
459464 sentry_meta [key ] = value
460- span ._sentry_meta = sentry_meta
465+ span ._sentry_meta = sentry_meta # type: ignore[union-attr]
461466
462467
463468def get_profile_context (span ):
0 commit comments