-
Notifications
You must be signed in to change notification settings - Fork 533
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
feat(instrumentation-aws-lambda): Changed capturing of X-Ray context as span link #1411
Changes from 1 commit
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 |
---|---|---|
|
@@ -34,9 +34,11 @@ import { | |
SpanKind, | ||
SpanStatusCode, | ||
TextMapGetter, | ||
TraceFlags, | ||
TracerProvider, | ||
ROOT_CONTEXT, | ||
Link, | ||
isSpanContextValid, | ||
TraceFlags, | ||
} from '@opentelemetry/api'; | ||
import { | ||
AWSXRAY_TRACE_ID_HEADER, | ||
|
@@ -156,11 +158,12 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { | |
const parent = AwsLambdaInstrumentation._determineParent( | ||
event, | ||
context, | ||
config.disableAwsContextPropagation === true, | ||
config.eventContextExtractor || | ||
AwsLambdaInstrumentation._defaultEventContextExtractor | ||
); | ||
|
||
const links = AwsLambdaInstrumentation._determineLinks(); | ||
|
||
const name = context.functionName; | ||
const span = plugin.tracer.startSpan( | ||
name, | ||
|
@@ -174,6 +177,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { | |
context.invokedFunctionArn | ||
), | ||
}, | ||
links: links, | ||
}, | ||
parent | ||
); | ||
|
@@ -356,32 +360,8 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { | |
private static _determineParent( | ||
event: any, | ||
context: Context, | ||
disableAwsContextPropagation: boolean, | ||
eventContextExtractor: EventContextExtractor | ||
): OtelContext { | ||
let parent: OtelContext | undefined = undefined; | ||
if (!disableAwsContextPropagation) { | ||
const lambdaTraceHeader = process.env[traceContextEnvironmentKey]; | ||
if (lambdaTraceHeader) { | ||
parent = awsPropagator.extract( | ||
otelContext.active(), | ||
{ [AWSXRAY_TRACE_ID_HEADER]: lambdaTraceHeader }, | ||
headerGetter | ||
); | ||
} | ||
if (parent) { | ||
const spanContext = trace.getSpan(parent)?.spanContext(); | ||
if ( | ||
spanContext && | ||
(spanContext.traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED | ||
) { | ||
// Trace header provided by Lambda only sampled if a sampled context was propagated from | ||
// an upstream cloud service such as S3, or the user is using X-Ray. In these cases, we | ||
// need to use it as the parent. | ||
return parent; | ||
} | ||
} | ||
} | ||
const extractedContext = safeExecuteInTheMiddle( | ||
() => eventContextExtractor(event, context), | ||
e => { | ||
|
@@ -396,10 +376,32 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { | |
if (trace.getSpan(extractedContext)?.spanContext()) { | ||
return extractedContext; | ||
} | ||
if (!parent) { | ||
// No context in Lambda environment or HTTP headers. | ||
return ROOT_CONTEXT; | ||
// No context in Lambda environment or HTTP headers. | ||
return ROOT_CONTEXT; | ||
} | ||
|
||
private static _determineLinks(): Link[] { | ||
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. I'm concerned that changing this behavior also being a breaking change. Will X-Ray customers who once saw their Lambda span as a direct child of the incoming context now only see it linked to the incoming context? Or will it still have a parent-child relationship (as before) but now ALSO have a link? I suppose this should have been discussed in the spec change process, but I wasn't aware of it :( 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. @willarmiros to be clear since you are the component owner, are you asking that we block this change? Since this comes from the spec, I would lean on the side of accepting it. Also, since this component is |
||
let parent: OtelContext | undefined = undefined; | ||
const lambdaTraceHeader = process.env[traceContextEnvironmentKey]; | ||
if (lambdaTraceHeader) { | ||
parent = awsPropagator.extract( | ||
otelContext.active(), | ||
{ [AWSXRAY_TRACE_ID_HEADER]: lambdaTraceHeader }, | ||
headerGetter | ||
); | ||
if (parent) { | ||
const spanContext = trace.getSpan(parent)?.spanContext(); | ||
if ( | ||
spanContext && | ||
isSpanContextValid(spanContext) && | ||
spanContext.traceFlags & TraceFlags.SAMPLED | ||
) { | ||
return [ | ||
{ context: spanContext, attributes: { source: 'x-ray-env' } }, | ||
]; | ||
} | ||
} | ||
} | ||
return parent; | ||
return []; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't removing this a breaking change?