Skip to content

Commit 05c1aa5

Browse files
committed
lazy load metrics
also hashlib right away
1 parent 95f9aed commit 05c1aa5

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

datadog_lambda/tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# under the Apache License Version 2.0.
33
# This product includes software developed at Datadog (https://www.datadoghq.com/).
44
# Copyright 2019 Datadog, Inc.
5-
import hashlib
65
import logging
76
import os
87
import base64
@@ -387,6 +386,7 @@ def extract_context_from_kinesis_event(event, lambda_context):
387386

388387

389388
def _deterministic_sha256_hash(s: str, part: str) -> int:
389+
import hashlib
390390
sha256_hash = hashlib.sha256(s.encode()).hexdigest()
391391
# First two chars is '0b'. zfill to ensure 256 bits, but we only care about the first 128 bits
392392
binary_hash = bin(int(sha256_hash, 16))[2:].zfill(256)

datadog_lambda/wrapper.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
XraySubsegment,
2424
Headers,
2525
)
26-
from datadog_lambda.metric import (
27-
flush_stats,
28-
submit_invocations_metric,
29-
submit_errors_metric,
30-
)
3126
from datadog_lambda.module_name import modify_module_name
3227
from datadog_lambda.patch import patch_all
3328
from datadog_lambda.span_pointers import calculate_span_pointers
@@ -248,7 +243,10 @@ def __call__(self, event, context, **kwargs):
248243
self.response = self.func(event, context, **kwargs)
249244
return self.response
250245
except Exception:
251-
submit_errors_metric(context)
246+
if not should_use_extension:
247+
from datadog_lambda.metric import submit_invocations_metric
248+
submit_invocations_metric(context)
249+
252250
if self.span:
253251
self.span.set_traceback()
254252
raise
@@ -284,7 +282,11 @@ def _before(self, event, context):
284282
try:
285283
self.response = None
286284
set_cold_start(init_timestamp_ns)
287-
submit_invocations_metric(context)
285+
286+
if not should_use_extension:
287+
from datadog_lambda.metric import submit_invocations_metric
288+
submit_invocations_metric(context)
289+
288290
self.trigger_tags = extract_trigger_tags(event, context)
289291
# Extract Datadog trace context and source from incoming requests
290292
dd_context, trace_context_source, event_source = extract_dd_trace_context(
@@ -383,6 +385,7 @@ def _after(self, event, context):
383385
logger.debug("Failed to create cold start spans. %s", e)
384386

385387
if not self.flush_to_log or should_use_extension:
388+
from datadog_lambda.metric import flush_stats
386389
flush_stats(context)
387390
if should_use_extension and self.local_testing_mode:
388391
# when testing locally, the extension does not know when an

0 commit comments

Comments
 (0)