@@ -326,22 +326,36 @@ def add_source(
326326 span .set_attribute ("code.function.name" , frame .f_code .co_name )
327327
328328
329- def add_query_source (span : "sentry_sdk.tracing.Span" ) -> None :
329+ def add_query_source (
330+ span : "Union[sentry_sdk.tracing.Span, sentry_sdk.traces.StreamedSpan]" ,
331+ ) -> None :
330332 """
331333 Adds OTel compatible source code information to a database query span
332334 """
333335 client = sentry_sdk .get_client ()
334- if not client .is_active ():
335- return
336336
337- if span .timestamp is None or span .start_timestamp is None :
337+ if isinstance (span , LegacySpan ):
338+ if not client .is_active ():
339+ return
340+
341+ # In the StreamedSpan case, we need to add the extra span information before
342+ # the span finishes, so it's expected that this will be None. In the LegacySpan case,
343+ # it should already be finished.
344+ if span .timestamp is None :
345+ return
346+
347+ if span .start_timestamp is None :
338348 return
339349
340350 should_add_query_source = client .options .get ("enable_db_query_source" , True )
341351 if not should_add_query_source :
342352 return
343353
344- duration = span .timestamp - span .start_timestamp
354+ end_timestamp = (
355+ datetime .now (timezone .utc ) if span .timestamp is None else span .timestamp
356+ )
357+
358+ duration = end_timestamp - span .start_timestamp
345359 threshold = client .options .get ("db_query_source_threshold_ms" , 0 )
346360 slow_query = duration / timedelta (milliseconds = 1 ) > threshold
347361
0 commit comments