@@ -172,29 +172,37 @@ def _start_as_current_span(
172172 kind : opentelemetry .trace .SpanKind ,
173173 context : Optional [Context ] = None ,
174174 ) -> Iterator [None ]:
175- with self .tracer .start_as_current_span (
176- name ,
177- attributes = attributes ,
178- kind = kind ,
179- context = context ,
180- set_status_on_exception = False ,
181- ) as span :
182- if input :
183- input .headers = self ._context_to_headers (input .headers )
184- try :
185- yield None
186- except Exception as exc :
187- if (
188- not isinstance (exc , ApplicationError )
189- or exc .category != ApplicationErrorCategory .BENIGN
190- ):
191- span .set_status (
192- Status (
193- status_code = StatusCode .ERROR ,
194- description = f"{ type (exc ).__name__ } : { exc } " ,
175+ if context :
176+ token = opentelemetry .context .attach (context )
177+ else :
178+ token = None
179+ try :
180+ with self .tracer .start_as_current_span (
181+ name ,
182+ attributes = attributes ,
183+ kind = kind ,
184+ context = context ,
185+ set_status_on_exception = False ,
186+ ) as span :
187+ if input :
188+ input .headers = self ._context_to_headers (input .headers )
189+ try :
190+ yield None
191+ except Exception as exc :
192+ if (
193+ not isinstance (exc , ApplicationError )
194+ or exc .category != ApplicationErrorCategory .BENIGN
195+ ):
196+ span .set_status (
197+ Status (
198+ status_code = StatusCode .ERROR ,
199+ description = f"{ type (exc ).__name__ } : { exc } " ,
200+ )
195201 )
196- )
197- raise
202+ raise
203+ finally :
204+ if token :
205+ opentelemetry .context .detach (token )
198206
199207 def _completed_workflow_span (
200208 self , params : _CompletedWorkflowSpanParams
@@ -305,34 +313,17 @@ async def execute_activity(
305313 self , input : temporalio .worker .ExecuteActivityInput
306314 ) -> Any :
307315 info = temporalio .activity .info ()
308- extracted_ctx = self .root ._context_from_headers (input .headers )
309-
310- if extracted_ctx :
311- token = opentelemetry .context .attach (extracted_ctx )
312- try :
313- with self .root ._start_as_current_span (
314- f"RunActivity:{ info .activity_type } " ,
315- attributes = {
316- "temporalWorkflowID" : info .workflow_id ,
317- "temporalRunID" : info .workflow_run_id ,
318- "temporalActivityID" : info .activity_id ,
319- },
320- kind = opentelemetry .trace .SpanKind .SERVER ,
321- ):
322- return await super ().execute_activity (input )
323- finally :
324- opentelemetry .context .detach (token )
325- else :
326- with self .root ._start_as_current_span (
327- f"RunActivity:{ info .activity_type } " ,
328- attributes = {
329- "temporalWorkflowID" : info .workflow_id ,
330- "temporalRunID" : info .workflow_run_id ,
331- "temporalActivityID" : info .activity_id ,
332- },
333- kind = opentelemetry .trace .SpanKind .SERVER ,
334- ):
335- return await super ().execute_activity (input )
316+ with self .root ._start_as_current_span (
317+ f"RunActivity:{ info .activity_type } " ,
318+ context = self .root ._context_from_headers (input .headers ),
319+ attributes = {
320+ "temporalWorkflowID" : info .workflow_id ,
321+ "temporalRunID" : info .workflow_run_id ,
322+ "temporalActivityID" : info .activity_id ,
323+ },
324+ kind = opentelemetry .trace .SpanKind .SERVER ,
325+ ):
326+ return await super ().execute_activity (input )
336327
337328
338329class _InputWithHeaders (Protocol ):
0 commit comments