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
@@ -79,7 +80,8 @@ def on_end(self, span):
7980
8081 is_root_span = not span .parent or span .parent .is_remote
8182 if is_root_span :
82- # if have a root span ending, we build a transaction and send it
83+ # if have a root span ending, stop the profiler, build a transaction and send it
84+ self ._stop_profile (span )
8385 self ._flush_root_span (span )
8486 else :
8587 self ._append_child_span (span )
@@ -112,6 +114,7 @@ def _add_root_span(self, span, parent_span):
112114 def _start_profile (self , span ):
113115 # type: (Span) -> None
114116 try_autostart_continuous_profiler ()
117+
115118 profiler_id = get_profiler_id ()
116119 thread_id , thread_name = get_current_thread_meta ()
117120
@@ -130,13 +133,26 @@ def _start_profile(self, span):
130133 # unix timestamp that is on span.start_time
131134 # setting it to 0 means the profiler will internally measure time on start
132135 profile = Profile (sampled , 0 )
136+
133137 sampling_context = create_sampling_context (
134138 span .name , span .attributes , span .parent , span .context .trace_id
135139 )
136140 profile ._set_initial_sampling_decision (sampling_context )
137141 profile .__enter__ ()
138142 set_sentry_meta (span , "profile" , profile )
139143
144+ continuous_profile = try_profile_lifecycle_trace_start ()
145+ profiler_id = get_profiler_id ()
146+ if profiler_id :
147+ span .set_attribute (SPANDATA .PROFILER_ID , profiler_id )
148+ set_sentry_meta (span , "continuous_profile" , continuous_profile )
149+
150+ def _stop_profile (self , span ):
151+ # type: (ReadableSpan) -> None
152+ continuous_profiler = get_sentry_meta (span , "continuous_profile" )
153+ if continuous_profiler :
154+ continuous_profiler .stop ()
155+
140156 def _flush_root_span (self , span ):
141157 # type: (ReadableSpan) -> None
142158 transaction_event = self ._root_span_to_transaction_event (span )
0 commit comments