Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't flush metrics with early spans #17

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def _sendEarlySpans(
)
early_invocation.end()

_flush(flush_timeout, tracer_provider, meter_provider)
_flush_traces(flush_timeout, tracer_provider, meter_provider)

def _createEarlySpan(
tracer: Tracer,
Expand All @@ -910,28 +910,36 @@ def _flush(
tracer_provider: TracerProvider = None,
meter_provider: MeterProvider = None,
) -> None:
now = time.time()
_flush_traces(flush_timeout, tracer_provider)
_flush_metrics(flush_timeout, meter_provider)

def _flush_traces(
flush_timeout: int,
tracer_provider: TracerProvider = None,
) -> None:
_tracer_provider = tracer_provider or get_tracer_provider()
if hasattr(_tracer_provider, "force_flush"):
try:
# NOTE: `force_flush` before function quit in case of Lambda freeze.
_tracer_provider.force_flush(flush_timeout)
except Exception: # pylint: disable=broad-except
logger.exception("TracerProvider failed to flush traces")
logger.exception("TracerProvider failed to flush metrics")
else:
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."
)

def _flush_metrics(
flush_timeout: int,
meter_provider: MeterProvider = None,
) -> None:
_meter_provider = meter_provider or get_meter_provider()
if hasattr(_meter_provider, "force_flush"):
rem = flush_timeout - (time.time() - now) * 1000
if rem > 0:
try:
# NOTE: `force_flush` before function quit in case of Lambda freeze.
_meter_provider.force_flush(rem)
except Exception: # pylint: disable=broad-except
logger.exception("MeterProvider failed to flush metrics")
try:
# NOTE: `force_flush` before function quit in case of Lambda freeze.
_meter_provider.force_flush(flush_timeout)
except Exception: # pylint: disable=broad-except
logger.exception("MeterProvider failed to flush metrics")
else:
logger.warning(
"MeterProvider was missing `force_flush` method. This is necessary in case of a Lambda freeze and would exist in the OTel SDK implementation."
Expand Down
Loading