-
Notifications
You must be signed in to change notification settings - Fork 45
Darcy.rayner/dd trace support #53
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
Changes from 11 commits
a7e617d
666658d
c32bdec
353dde6
082683c
fc8b1fd
7e8d8b9
76c3632
47a622b
281e791
903d7c5
b40dcb6
8bd253d
e9d26ec
8d6fbfd
3b47b39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
import logging | ||
import traceback | ||
|
||
from datadog_lambda.cold_start import set_cold_start | ||
from datadog_lambda.cold_start import set_cold_start, is_cold_start | ||
from datadog_lambda.metric import ( | ||
lambda_stats, | ||
submit_invocations_metric, | ||
|
@@ -16,9 +16,14 @@ | |
from datadog_lambda.patch import patch_all | ||
from datadog_lambda.tracing import ( | ||
extract_dd_trace_context, | ||
set_correlation_ids, | ||
inject_correlation_ids, | ||
get_dd_trace_context, | ||
dd_tracing_enabled, | ||
set_correlation_ids, | ||
) | ||
from datadog_lambda.constants import TraceContextSource | ||
from ddtrace import tracer | ||
from ddtrace.propagation.http import HTTPPropagator | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -81,13 +86,21 @@ def __init__(self, func): | |
self.logs_injection = ( | ||
os.environ.get("DD_LOGS_INJECTION", "true").lower() == "true" | ||
) | ||
self.merge_xray_traces = ( | ||
os.environ.get("DD_MERGE_XRAY_TRACES", "false").lower() == "true" | ||
) | ||
self.handler_name = os.environ.get("_HANDLER", "handler") | ||
self.function_name = os.environ.get("AWS_LAMBDA_FUNCTION_NAME", "function") | ||
self.propagator = HTTPPropagator() | ||
|
||
# Inject trace correlation ids to logs | ||
if self.logs_injection: | ||
inject_correlation_ids() | ||
|
||
# Patch HTTP clients to propagate Datadog trace context | ||
patch_all() | ||
if not dd_tracing_enabled: | ||
# When using dd_trace_py it will patch all the http clients for us, | ||
DarcyRaynerDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Patch HTTP clients to propagate Datadog trace context | ||
patch_all() | ||
logger.debug("datadog_lambda_wrapper initialized") | ||
except Exception: | ||
traceback.print_exc() | ||
|
@@ -105,24 +118,59 @@ def __call__(self, event, context, **kwargs): | |
|
||
def _before(self, event, context): | ||
try: | ||
|
||
set_cold_start() | ||
submit_invocations_metric(context) | ||
# Extract Datadog trace context from incoming requests | ||
extract_dd_trace_context(event) | ||
dd_context = extract_dd_trace_context(event) | ||
|
||
self.span = None | ||
if dd_tracing_enabled: | ||
self.span = self._create_dd_trace_py_span(context, dd_context) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nits, i feel this function is more about "creating a span for the function execution" rather than "creating a dd trace py span". Perhaps |
||
else: | ||
set_correlation_ids() | ||
|
||
# Set log correlation ids using extracted trace context | ||
set_correlation_ids() | ||
logger.debug("datadog_lambda_wrapper _before() done") | ||
except Exception: | ||
traceback.print_exc() | ||
|
||
def _after(self, event, context): | ||
try: | ||
if self.span: | ||
self.span.finish() | ||
if not self.flush_to_log: | ||
lambda_stats.flush(float("inf")) | ||
logger.debug("datadog_lambda_wrapper _after() done") | ||
except Exception: | ||
traceback.print_exc() | ||
|
||
def _create_dd_trace_py_span(self, context, trace_context): | ||
DarcyRaynerDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
span_context = None | ||
if ( | ||
trace_context["source"] == TraceContextSource.EVENT | ||
or self.merge_xray_traces | ||
): | ||
headers = get_dd_trace_context() | ||
span_context = self.propagator.extract(headers) | ||
|
||
tags = {} | ||
if context: | ||
tags = { | ||
"cold_start": is_cold_start(), | ||
"function_arn": context.invoked_function_arn, | ||
"request_id": context.aws_request_id, | ||
"resource_names": context.function_name, | ||
} | ||
args = { | ||
"service": self.function_name, | ||
DarcyRaynerDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"resource": self.handler_name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be future proof, let' use |
||
"span_type": "serverless", | ||
"child_of": span_context, | ||
} | ||
span = tracer.start_span("aws.lambda", **args) | ||
if span: | ||
span.set_tags(tags) | ||
return span | ||
|
||
|
||
datadog_lambda_wrapper = _LambdaDecorator |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
./scripts/build_layers.sh | ||
./scripts/publish_layers.sh us-east-1 |
Uh oh!
There was an error while loading. Please reload this page.