Skip to content

Commit 8d10a80

Browse files
committed
Update force_flush() check based on PR feedback
Signed-off-by: Anthony J Mirabella <[email protected]>
1 parent 8c1b6dd commit 8d10a80

File tree

1 file changed

+22
-18
lines changed
  • instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda

1 file changed

+22
-18
lines changed

instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -361,28 +361,32 @@ def _instrumented_lambda_handler_call(
361361

362362
now = time.time()
363363
_tracer_provider = tracer_provider or get_tracer_provider()
364-
try:
365-
# NOTE: `force_flush` before function quit in case of Lambda freeze.
366-
# Assumes we are using the OpenTelemetry SDK implementation of the
367-
# `TracerProvider`.
368-
_tracer_provider.force_flush(flush_timeout)
369-
except Exception: # pylint: disable=broad-except
370-
logger.error(
371-
"TracerProvider was missing `force_flush` method. This is necessary in case of a Lambda freeze and would exist in the OTel SDK implementation."
372-
)
373-
374-
rem = flush_timeout - (time.time()-now)*1000
375-
if rem > 0:
376-
_meter_provider = meter_provider or get_meter_provider()
364+
if hasattr(_tracer_provider, "force_flush"):
377365
try:
378366
# NOTE: `force_flush` before function quit in case of Lambda freeze.
379-
# Assumes we are using the OpenTelemetry SDK implementation of the
380-
# `MeterProvider`.
381-
_meter_provider.force_flush(rem)
367+
_tracer_provider.force_flush(flush_timeout)
382368
except Exception: # pylint: disable=broad-except
383-
logger.error(
384-
"MeterProvider was missing `force_flush` method. This is necessary in case of a Lambda freeze and would exist in the OTel SDK implementation."
369+
logger.exception(
370+
f"TracerProvider failed to flush traces"
385371
)
372+
else:
373+
logger.warning("TracerProvider was missing `force_flush` method. This is necessary in case of a Lambda freeze and would exist in the OTel SDK implementation.")
374+
375+
_meter_provider = meter_provider or get_meter_provider()
376+
if hasattr(_meter_provider, "force_flush"):
377+
rem = flush_timeout - (time.time()-now)*1000
378+
if rem > 0:
379+
try:
380+
# NOTE: `force_flush` before function quit in case of Lambda freeze.
381+
_meter_provider.force_flush(rem)
382+
except Exception: # pylint: disable=broad-except
383+
logger.exception(
384+
f"MeterProvider failed to flush traces"
385+
)
386+
else:
387+
logger.warning(
388+
"MeterProvider was missing `force_flush` method. This is necessary in case of a Lambda freeze and would exist in the OTel SDK implementation."
389+
)
386390

387391
return result
388392

0 commit comments

Comments
 (0)