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
Use the existing context if any to start the web spans #1647
Conversation
4006cd1
to
a23652f
Compare
it('should set the parent from the active context if any', () => { | ||
tracer.trace('aws.lambda', parentSpan => { | ||
web.instrument(tracer, config, req, res, 'test.request', span => { | ||
expect(span.context()._traceId.toString(10)).to.equal(parentSpan.context()._traceId.toString(10)) |
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.
expect(span.context()._traceId.toString(10)).to.equal(parentSpan.context()._traceId.toString(10)) | |
expect(span.context().toTraceId()).to.equal(parentSpan.context().toTraceId()) |
Haven't tested it, but if it works, my guess is it's better to use the public API.
Not sure there is a public API to access the parentId
though (for line 143) @rochdev ?
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.
There are public methods for the trace ID and span ID, but not for the parent ID since it's not a value that should ever be needed outside of tests with the current model.
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.
Also worth noting existing tests do exactly the same, so while not ideal I think it's fine for now.
* Use the existing context if any to start the web spans * unit tests
* Use the existing context if any to start the web spans * unit tests
What does this PR do?
Currently web framework (say express) plugins will only try to extract and resume a trace context from the incoming HTTP headers, and if there are no incoming trace headers, it will always create a new trace context. Even if somehow the tracer already have an existing trace context (unexpected), that trace context will NOT be used.
However, when the web framework is deployed in an AWS Lambda function, the datadog lambda library https://github.com/DataDog/datadog-lambda-js always creates a trace context and an
aws.lambda
span outside the express app. In this case, when the express app receives the request, the tracer already have an existing trace context, which should be used to create express app spans, but it's not.This PR updates web.js so that if somehow (like in Lambda) a trace context is created outside the web app, that trace context will be respected and used to create spans for the web app.
Before: The same web request will result in two broken traces instead of one -- one from api gateway + lambda execution, the other from the express
After: Only once single trace for the same web request
Motivation
One single request should be represented in one single trace.
Plugin Checklist
Additional Notes