Skip to content

chore: lazy load some imports #581

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

Merged
merged 5 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion datadog_lambda/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019 Datadog, Inc.
import hashlib
import logging
import os
import base64
Expand Down Expand Up @@ -387,6 +386,8 @@ def extract_context_from_kinesis_event(event, lambda_context):


def _deterministic_sha256_hash(s: str, part: str) -> int:
import hashlib

sha256_hash = hashlib.sha256(s.encode()).hexdigest()
# First two chars is '0b'. zfill to ensure 256 bits, but we only care about the first 128 bits
binary_hash = bin(int(sha256_hash, 16))[2:].zfill(256)
Expand Down
20 changes: 13 additions & 7 deletions datadog_lambda/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
XraySubsegment,
Headers,
)
from datadog_lambda.metric import (
flush_stats,
submit_invocations_metric,
submit_errors_metric,
)
from datadog_lambda.module_name import modify_module_name
from datadog_lambda.patch import patch_all
from datadog_lambda.span_pointers import calculate_span_pointers
Expand Down Expand Up @@ -248,7 +243,11 @@ def __call__(self, event, context, **kwargs):
self.response = self.func(event, context, **kwargs)
return self.response
except Exception:
submit_errors_metric(context)
if not should_use_extension:
from datadog_lambda.metric import submit_invocations_metric

submit_invocations_metric(context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want this change. You replaced an errors metric with an invocation metric. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, thanks for catching that!


if self.span:
self.span.set_traceback()
raise
Expand Down Expand Up @@ -284,7 +283,12 @@ def _before(self, event, context):
try:
self.response = None
set_cold_start(init_timestamp_ns)
submit_invocations_metric(context)

if not should_use_extension:
from datadog_lambda.metric import submit_invocations_metric

submit_invocations_metric(context)

self.trigger_tags = extract_trigger_tags(event, context)
# Extract Datadog trace context and source from incoming requests
dd_context, trace_context_source, event_source = extract_dd_trace_context(
Expand Down Expand Up @@ -383,6 +387,8 @@ def _after(self, event, context):
logger.debug("Failed to create cold start spans. %s", e)

if not self.flush_to_log or should_use_extension:
from datadog_lambda.metric import flush_stats

flush_stats(context)
if should_use_extension and self.local_testing_mode:
# when testing locally, the extension does not know when an
Expand Down
Loading