1818from sentry_sdk .profiler .continuous_profiler import (
1919 try_autostart_continuous_profiler ,
2020 get_profiler_id ,
21+ try_profile_lifecycle_trace_start ,
2122)
2223from sentry_sdk .profiler .transaction_profiler import Profile
2324from sentry_sdk .integrations .opentelemetry .sampler import create_sampling_context
@@ -80,7 +81,8 @@ def on_end(self, span):
8081
8182 is_root_span = not span .parent or span .parent .is_remote
8283 if is_root_span :
83- # if have a root span ending, we build a transaction and send it
84+ # if have a root span ending, stop the profiler, build a transaction and send it
85+ self ._stop_profile (span )
8486 self ._flush_root_span (span )
8587 else :
8688 self ._append_child_span (span )
@@ -113,6 +115,7 @@ def _add_root_span(self, span, parent_span):
113115 def _start_profile (self , span ):
114116 # type: (Span) -> None
115117 try_autostart_continuous_profiler ()
118+
116119 profiler_id = get_profiler_id ()
117120 thread_id , thread_name = get_current_thread_meta ()
118121
@@ -131,13 +134,26 @@ def _start_profile(self, span):
131134 # unix timestamp that is on span.start_time
132135 # setting it to 0 means the profiler will internally measure time on start
133136 profile = Profile (sampled , 0 )
137+
134138 sampling_context = create_sampling_context (
135139 span .name , span .attributes , span .parent , span .context .trace_id
136140 )
137141 profile ._set_initial_sampling_decision (sampling_context )
138142 profile .__enter__ ()
139143 set_sentry_meta (span , "profile" , profile )
140144
145+ continuous_profile = try_profile_lifecycle_trace_start ()
146+ profiler_id = get_profiler_id ()
147+ if profiler_id :
148+ span .set_attribute (SPANDATA .PROFILER_ID , profiler_id )
149+ set_sentry_meta (span , "continuous_profile" , continuous_profile )
150+
151+ def _stop_profile (self , span ):
152+ # type: (ReadableSpan) -> None
153+ continuous_profiler = get_sentry_meta (span , "continuous_profile" )
154+ if continuous_profiler :
155+ continuous_profiler .stop ()
156+
141157 def _flush_root_span (self , span ):
142158 # type: (ReadableSpan) -> None
143159 transaction_event = self ._root_span_to_transaction_event (span )
0 commit comments