Fix incorrect parent id for the aws.lambda span#166
Conversation
8ef2935 to
ca0d0ad
Compare
Codecov Report
@@ Coverage Diff @@
## main #166 +/- ##
==========================================
- Coverage 86.73% 86.53% -0.21%
==========================================
Files 31 31
Lines 1184 1203 +19
Branches 235 236 +1
==========================================
+ Hits 1027 1041 +14
- Misses 102 106 +4
- Partials 55 56 +1
Continue to review full report at Codecov.
|
| @@ -1,4 +1,3 @@ | |||
| XXXX-XX-XX XX:XX:XX.XXX XXXX-XXXX-XXXX-XXXX-XXXX INFO Snapshot test http requests successfully made to URLs: https://ip-ranges.datadoghq.com,https://ip-ranges.datadoghq.eu | |||
There was a problem hiding this comment.
Comparing with the logs below, I don't think this line should be there. Probably missed from previous integration test updates.
| {"e":XXXX,"m":"serverless.integration_test.execution","t":["tagkey:tagvalue","eventsource:SNS","dd_lambda_layer:datadog-nodev12.XX.X"],"v":1} | ||
| {"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-plugin-dev-async-metrics_node12_with_plugin","error":0,"meta":{"_dd.origin":"lambda","service":"integration-plugin-dev-async-metrics_node12_with_plugin","cold_start":"false","function_arn":"XXXX_node12_with_plugin","function_version":"$LATEST","request_id":"XXXX","resource_names":"integration-plugin-dev-async-metrics_node12_with_plugin","datadog_lambda":"XXXX","dd_trace":"XXXX","_dd.parent_source":"xray","function_trigger.event_source":"sns","function_trigger.event_source_arn":"arn:aws:sns:us-east-2:123456789012:sns-lambda"},"metrics":{"_sample_rate":1,"_sampling_priority_v1":2},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"}]]} | ||
| XXXX-XX-XX XX:XX:XX.XXX XXXX-XXXX-XXXX-XXXX-XXXX INFO [dd.trace_id=XXXX dd.span_id=XXXX] Processed SNS request | ||
| {"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-plugin-dev-async-metrics_node12_with_plugin","error":0,"meta":{"_dd.origin":"lambda","service":"integration-plugin-dev-async-metrics_node12_with_plugin","cold_start":"false","function_arn":"XXXX_node12_with_plugin","function_version":"$LATEST","request_id":"XXXX","resource_names":"integration-plugin-dev-async-metrics_node12_with_plugin","datadog_lambda":"XXXX","dd_trace":"XXXX","_dd.parent_source":"xray","function_trigger.event_source":"sns","function_trigger.event_source_arn":"arn:aws:sns:us-east-2:123456789012:sns-lambda"},"metrics":{"_sample_rate":1,"_sampling_priority_v1":2},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"}]]} |
There was a problem hiding this comment.
Perhaps the new version of tracer logs the traces a little bit later than the previous version, not sure, but probably doesn't hurt.
|
|
||
| // Get the trace headers from the root trace context. | ||
| get rootTraceHeaders(): Partial<TraceHeaders> { | ||
| const rootTraceContext = this.rootTraceContext; |
There was a problem hiding this comment.
It looks like we set the root trace context to being the incoming Datadog trace headers. However, when hybrid tracing is enabled, we want to use the PARENT ID from X-Ray but the TRACE ID from Datadog. Is that accounted for here?
There was a problem hiding this comment.
Here is the relevant logic in Go: https://github.com/DataDog/datadog-lambda-go/blob/main/internal/trace/context.go#L45
There was a problem hiding this comment.
Yes, the root trace context is derived from X-Ray when there is not incoming Datadog trace headers.
There was a problem hiding this comment.
I think Nick is right about the desired behaviour. Otherwise the parent on the root span swaps between the incoming datadog span, (when it's available), and the root x-ray segment. This was an existing issue, so we don't have to fix it with this PR, but I think we would change the logic here: https://github.com/DataDog/datadog-lambda-js/blob/main/src/trace/context.ts#L88, to set the parentId to the x-ray when merge x-ray traces is enabled.
DarcyRaynerDD
left a comment
There was a problem hiding this comment.
LGTM, Nick's comment is a potential issue we should fix, but I don't think we have to do it in this PR.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #166 +/- ##
==========================================
- Coverage 86.73% 86.53% -0.21%
==========================================
Files 31 31
Lines 1184 1203 +19
Branches 235 236 +1
==========================================
+ Hits 1027 1041 +14
- Misses 102 106 +4
- Partials 55 56 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What does this PR do?
The
currentTraceHeadersmethod callscurrentTraceContext, which always return the current trace context. This method is good for getting the trace headers for the injection to downstream calls, because it always sets the parent id to the currently active Datadog span or X-Ray segment. However, it shouldn't be used to determine the parent of theaws.lambdaspan, because it doesn't matter if the user creates a custom span or segment. Theaws.lambdaspan should always be parented to the root trace context, i.e., the parent span should either be from the incoming trace context or the X-Ray root segment.Perhaps this wasn't an issue before, because
getXraySegmentdoesn't return anything unless X-Ray active tracing is enabled, but it now always return a segment? But it doesn't matter anymore,getXraySegmentshouldn't be called when determining the parent of theaws.lambdaspan anyway.To fix the issue,
rootTraceHeaderswas added toTraceContextServiceand it always return the trace headers from the root trace context. When determining theaws.lambdaspan's parent inlistener.js, callrootTraceHeadersinstead ofcurrentTraceHeaders.Other changes included:
dd-traceto0.31.0version to allow the Datadog trace context propagate through AWS SDKlambda.invoke().Motivation
When the incoming Lambda event object or context object contains a Datadog trace context, the automatically generated
aws.lambdaspan should be parented to it. However, it currently parents the span to the X-Ray segment instead, even when X-Ray active tracing is not enabled. This results theaws.lambdaspan being an orphan (parent id points to a non-existent span).Testing Guidelines
Additional Notes
Types of Changes
Check all that apply