@@ -76,6 +76,7 @@ def custom_event_context_extractor(lambda_event):
76
76
77
77
from wrapt import wrap_function_wrapper
78
78
79
+ from opentelemetry import context as context_api
79
80
from opentelemetry .context .context import Context
80
81
from opentelemetry .instrumentation .aws_lambda .package import _instruments
81
82
from opentelemetry .instrumentation .aws_lambda .version import __version__
@@ -303,66 +304,76 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
303
304
schema_url = "https://opentelemetry.io/schemas/1.11.0" ,
304
305
)
305
306
306
- with tracer .start_as_current_span (
307
- name = orig_handler_name ,
308
- context = parent_context ,
309
- kind = span_kind ,
310
- ) as span :
311
- if span .is_recording ():
312
- lambda_context = args [1 ]
313
- # NOTE: The specs mention an exception here, allowing the
314
- # `SpanAttributes.CLOUD_RESOURCE_ID` attribute to be set as a span
315
- # attribute instead of a resource attribute.
316
- #
317
- # See more:
318
- # https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md#resource-detector
319
- span .set_attribute (
320
- SpanAttributes .CLOUD_RESOURCE_ID ,
321
- lambda_context .invoked_function_arn ,
322
- )
323
- span .set_attribute (
324
- SpanAttributes .FAAS_INVOCATION_ID ,
325
- lambda_context .aws_request_id ,
326
- )
327
-
328
- # NOTE: `cloud.account.id` can be parsed from the ARN as the fifth item when splitting on `:`
329
- #
330
- # See more:
331
- # https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md#all-triggers
332
- account_id = lambda_context .invoked_function_arn .split (":" )[4 ]
333
- span .set_attribute (
334
- ResourceAttributes .CLOUD_ACCOUNT_ID ,
335
- account_id ,
336
- )
307
+ token = context_api .attach (parent_context )
308
+ try :
309
+ with tracer .start_as_current_span (
310
+ name = orig_handler_name ,
311
+ context = parent_context ,
312
+ kind = span_kind ,
313
+ ) as span :
314
+ if span .is_recording ():
315
+ lambda_context = args [1 ]
316
+ # NOTE: The specs mention an exception here, allowing the
317
+ # `SpanAttributes.CLOUD_RESOURCE_ID` attribute to be set as a span
318
+ # attribute instead of a resource attribute.
319
+ #
320
+ # See more:
321
+ # https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md#resource-detector
322
+ span .set_attribute (
323
+ SpanAttributes .CLOUD_RESOURCE_ID ,
324
+ lambda_context .invoked_function_arn ,
325
+ )
326
+ span .set_attribute (
327
+ SpanAttributes .FAAS_INVOCATION_ID ,
328
+ lambda_context .aws_request_id ,
329
+ )
337
330
338
- exception = None
339
- result = None
340
- try :
341
- result = call_wrapped (* args , ** kwargs )
342
- except Exception as exc : # pylint: disable=W0703
343
- exception = exc
344
- span .set_status (Status (StatusCode .ERROR ))
345
- span .record_exception (exception )
346
-
347
- # If the request came from an API Gateway, extract http attributes from the event
348
- # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#api-gateway
349
- # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions
350
- if isinstance (lambda_event , dict ) and lambda_event .get (
351
- "requestContext"
352
- ):
353
- span .set_attribute (SpanAttributes .FAAS_TRIGGER , "http" )
354
-
355
- if lambda_event .get ("version" ) == "2.0" :
356
- _set_api_gateway_v2_proxy_attributes (lambda_event , span )
357
- else :
358
- _set_api_gateway_v1_proxy_attributes (lambda_event , span )
359
-
360
- if isinstance (result , dict ) and result .get ("statusCode" ):
331
+ # NOTE: `cloud.account.id` can be parsed from the ARN as the fifth item when splitting on `:`
332
+ #
333
+ # See more:
334
+ # https://github.com/open-telemetry/semantic-conventions/blob/main/docs/faas/aws-lambda.md#all-triggers
335
+ account_id = lambda_context .invoked_function_arn .split (
336
+ ":"
337
+ )[4 ]
361
338
span .set_attribute (
362
- SpanAttributes . HTTP_STATUS_CODE ,
363
- result . get ( "statusCode" ) ,
339
+ ResourceAttributes . CLOUD_ACCOUNT_ID ,
340
+ account_id ,
364
341
)
365
342
343
+ exception = None
344
+ result = None
345
+ try :
346
+ result = call_wrapped (* args , ** kwargs )
347
+ except Exception as exc : # pylint: disable=W0703
348
+ exception = exc
349
+ span .set_status (Status (StatusCode .ERROR ))
350
+ span .record_exception (exception )
351
+
352
+ # If the request came from an API Gateway, extract http attributes from the event
353
+ # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#api-gateway
354
+ # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions
355
+ if isinstance (lambda_event , dict ) and lambda_event .get (
356
+ "requestContext"
357
+ ):
358
+ span .set_attribute (SpanAttributes .FAAS_TRIGGER , "http" )
359
+
360
+ if lambda_event .get ("version" ) == "2.0" :
361
+ _set_api_gateway_v2_proxy_attributes (
362
+ lambda_event , span
363
+ )
364
+ else :
365
+ _set_api_gateway_v1_proxy_attributes (
366
+ lambda_event , span
367
+ )
368
+
369
+ if isinstance (result , dict ) and result .get ("statusCode" ):
370
+ span .set_attribute (
371
+ SpanAttributes .HTTP_STATUS_CODE ,
372
+ result .get ("statusCode" ),
373
+ )
374
+ finally :
375
+ context_api .detach (token )
376
+
366
377
now = time .time ()
367
378
_tracer_provider = tracer_provider or get_tracer_provider ()
368
379
if hasattr (_tracer_provider , "force_flush" ):
0 commit comments