Skip to content
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

Merged
merged 2 commits into from Oct 12, 2021

Conversation

tianchu
Copy link
Contributor

@tianchu tianchu commented Oct 8, 2021

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

image

image

After: Only once single trace for the same web request

image

Motivation

One single request should be represented in one single trace.

Plugin Checklist

Additional Notes

@tianchu tianchu requested a review from a team as a code owner October 8, 2021 17:29
@tianchu tianchu force-pushed the tian.chu/use-existing-context-for-web branch from 4006cd1 to a23652f Compare October 8, 2021 19:11
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ?

Copy link
Member

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.

Copy link
Member

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.

@rochdev rochdev added this to the 1.5.1 milestone Oct 12, 2021
@rochdev rochdev added bug Something isn't working integrations labels Oct 12, 2021
@bengl bengl merged commit 6c55168 into master Oct 12, 2021
rochdev pushed a commit that referenced this pull request Nov 2, 2021
* Use the existing context if any to start the web spans

* unit tests
rochdev pushed a commit that referenced this pull request Nov 2, 2021
* Use the existing context if any to start the web spans

* unit tests
Qard added a commit that referenced this pull request Dec 20, 2022
Qard added a commit that referenced this pull request Dec 20, 2022
Qard added a commit that referenced this pull request Dec 22, 2022
Qard added a commit that referenced this pull request Dec 22, 2022
Qard added a commit that referenced this pull request Dec 23, 2022
juan-fernandez pushed a commit that referenced this pull request Jan 3, 2023
juan-fernandez pushed a commit that referenced this pull request Jan 4, 2023
@tlhunter tlhunter deleted the tian.chu/use-existing-context-for-web branch January 19, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working integrations semver-patch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants