@@ -278,17 +278,14 @@ def add_source(
278278 if isinstance (span , LegacySpan ):
279279 span .set_data (SPANDATA .CODE_LINENO , lineno )
280280 else :
281- span .set_attribute (SPANDATA . CODE_LINENO , lineno )
281+ span .set_attribute ("code.line.number" , lineno )
282282
283283 try :
284284 namespace = frame .f_globals .get ("__name__" )
285285 except Exception :
286286 namespace = None
287- if namespace is not None :
288- if isinstance (span , LegacySpan ):
289- span .set_data (SPANDATA .CODE_NAMESPACE , namespace )
290- else :
291- span .set_attribute (SPANDATA .CODE_NAMESPACE , namespace )
287+ if namespace is not None and isinstance (span , LegacySpan ):
288+ span .set_data (SPANDATA .CODE_NAMESPACE , namespace )
292289
293290 filepath = _get_frame_module_abs_path (frame )
294291 if filepath is not None :
@@ -303,7 +300,7 @@ def add_source(
303300 span .set_data (SPANDATA .CODE_FILEPATH , in_app_path )
304301 else :
305302 if in_app_path is not None :
306- span .set_attribute (SPANDATA . CODE_FILEPATH , in_app_path )
303+ span .set_attribute ("code.file.path" , in_app_path )
307304
308305 try :
309306 code_function = frame .f_code .co_name
@@ -314,7 +311,7 @@ def add_source(
314311 if isinstance (span , LegacySpan ):
315312 span .set_data (SPANDATA .CODE_FUNCTION , frame .f_code .co_name )
316313 else :
317- span .set_attribute (SPANDATA . CODE_FUNCTION , frame .f_code .co_name )
314+ span .set_attribute ("code.function.name" , frame .f_code .co_name )
318315
319316
320317def add_query_source (span : "sentry_sdk.tracing.Span" ) -> None :
@@ -347,31 +344,44 @@ def add_query_source(span: "sentry_sdk.tracing.Span") -> None:
347344 )
348345
349346
350- def add_http_request_source_for_streamed_span (
351- span : "sentry_sdk.traces.StreamedSpan" ,
347+ def add_http_request_source (
348+ span : "Union[ sentry_sdk.tracing.Span, sentry_sdk. traces.StreamedSpan] " ,
352349) -> None :
353350 """
354- Adds OTel compatible source code information to a span for an outgoing HTTP request.
355-
356- This is intended to be used with StreamedSpans, not legacy Spans.
357-
358- StreamedSpans need to have this information added before the span finishes, which
359- is why some of the checks that exist in `add_http_request_source` are not present here.
351+ Adds OTel compatible source code information to a span for an outgoing HTTP request
360352 """
361353 client = sentry_sdk .get_client ()
362354
355+ if isinstance (span , LegacySpan ):
356+ if not client .is_active ():
357+ return
358+
359+ # In the StreamedSpan case, we need to add the extra span information before
360+ # the span finishes, so it's expected that this will be None. In the LegacySpan case,
361+ # it should already be finished.
362+ if span .timestamp is None :
363+ return
364+
363365 if span .start_timestamp is None :
364366 return
365367
366368 should_add_request_source = client .options .get ("enable_http_request_source" , True )
367369 if not should_add_request_source :
368370 return
369371
370- if span .start_timestamp_monotonic_ns is not None :
371- elapsed = nanosecond_time () - span .start_timestamp_monotonic_ns
372- end_timestamp = span .start_timestamp + timedelta (microseconds = elapsed / 1000 )
372+ if span .timestamp is None :
373+ if (
374+ isinstance (span , sentry_sdk .traces .StreamedSpan )
375+ and span .start_timestamp_monotonic_ns is not None
376+ ):
377+ elapsed = nanosecond_time () - span .start_timestamp_monotonic_ns
378+ end_timestamp = span .start_timestamp + timedelta (
379+ microseconds = elapsed / 1000
380+ )
381+ else :
382+ end_timestamp = datetime .now (timezone .utc )
373383 else :
374- end_timestamp = datetime . now ( timezone . utc )
384+ end_timestamp = span . timestamp
375385
376386 duration = end_timestamp - span .start_timestamp
377387 threshold = client .options .get ("http_request_source_threshold_ms" , 0 )
@@ -388,36 +398,6 @@ def add_http_request_source_for_streamed_span(
388398 )
389399
390400
391- def add_http_request_source (span : "sentry_sdk.tracing.Span" ) -> None :
392- """
393- Adds OTel compatible source code information to a span for an outgoing HTTP request
394- """
395- client = sentry_sdk .get_client ()
396- if not client .is_active ():
397- return
398-
399- if span .timestamp is None or span .start_timestamp is None :
400- return
401-
402- should_add_request_source = client .options .get ("enable_http_request_source" , True )
403- if not should_add_request_source :
404- return
405-
406- duration = span .timestamp - span .start_timestamp
407- threshold = client .options .get ("http_request_source_threshold_ms" , 0 )
408- slow_query = duration / timedelta (milliseconds = 1 ) > threshold
409-
410- if not slow_query :
411- return
412-
413- add_source (
414- span = span ,
415- project_root = client .options ["project_root" ],
416- in_app_include = client .options .get ("in_app_include" ),
417- in_app_exclude = client .options .get ("in_app_exclude" ),
418- )
419-
420-
421401def extract_sentrytrace_data (
422402 header : "Optional[str]" ,
423403) -> "Optional[Dict[str, Union[str, bool, None]]]" :
0 commit comments