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

Fix api.addContext so it works on the current span (not the root) #347

Merged
merged 1 commit into from Sep 21, 2021

Conversation

ajvondrak
Copy link
Contributor

The documented behavior for api.addContext is that it adds fields to
the current span. However, the code was accessing index 0 of the context
stack, which will always be the root span. Tests previously did not
catch this because api.addContext was only exercised in a single-span
trace. A similar issue exists for api.removeContext, although it's
deprecated. But the patch is simple, so I applied it to both functions.

This probably wasn't caught sooner because span.addContext gets used
way more, particularly with asynchronous spans. As evidence, the
auto-instrumentation does not use api.addContext at all!

Note that running npm automatically bumped the package-lock.json, since
v2.7.0 of the Beeline had been released without changing the lock file.

@ajvondrak ajvondrak requested a review from a team May 7, 2021 20:51
@vreynolds vreynolds self-assigned this May 7, 2021
@vreynolds
Copy link
Contributor

@honeycombio/integrations-team thoughts on whether this is a breaking change? It's been this way for a while, which makes me wonder if people have come to expect current (incorrect) behavior. The function itself is pretty front and center in the docs: https://docs.honeycomb.io/getting-data-in/javascript/beeline-nodejs/ but may not be used that much in the wild.

@vreynolds vreynolds added version: bump major A PR that breaks backwards compatibility. status: blocked type: bug Something isn't working labels May 12, 2021
@bdarfler
Copy link
Contributor

This requires a major version bump. For that reason, we are going to hold off merging until this or other major version bump work becomes pressing.

@vreynolds vreynolds removed their assignment Aug 11, 2021
@nicwise
Copy link

nicwise commented Sep 3, 2021

WAY more confusingly: https://honeycombpollinators.slack.com/archives/CLMLJ7CDV/p1630626416019700

I have 2 projects. One is our main backend (apollo lambda). One is a small ses/sns notification lambda.

The main backend works fine. Nice traces + spans, info in the right places etc.

The new one doesn't. All the addContext attaches it to the root span. I can't put anything in a sub-span unless I add it in startSpan or use span.addContext. If I had to pass around the span in the main project it would break the whole model and wrapper we have for observability. Bad times.

Both are on the same version of node, same env (lambda), same version of beeline. Only difference is that the small one isn't using async anywhere (even the event handler isn't) and the other one very much is, tho the startTrace isn't in a function which is marked as async - YET (I need to upgrade middy which would force me to make it async)

I'd very much like this to be more predictable. I think this would make it so.

@nicwise
Copy link

nicwise commented Sep 3, 2021

here's the not at all cut down version of the new, smaller handler from above. The main app is all middleware etc and needs a lot of code removing to make sense, so I can't post that.

https://gist.github.com/nicwise/35a38c6c17cab6d01bfae7e17ced37ac

@vreynolds
Copy link
Contributor

@nicwise thanks for reporting! The difference you're seeing (sorta) makes sense at first glance, because startAsyncSpan creates a temporary new stack, so anything you add within that async context would treat that started span as the "root".

We'll chat about this again when the team's back after the holiday -- might be we just bite the bullet and do a major bump for this.

@vreynolds vreynolds added the type: discussion Requests for comments, discussions about possible enhancements. label Sep 3, 2021
@vreynolds vreynolds self-assigned this Sep 10, 2021
@vreynolds vreynolds removed status: blocked type: discussion Requests for comments, discussions about possible enhancements. labels Sep 10, 2021
The documented behavior for `api.addContext` is that it adds fields to
the current span. However, the code was accessing index 0 of the context
stack, which will always be the *root* span. Tests previously did not
catch this because `api.addContext` was only exercised in a single-span
trace. A similar issue exists for `api.removeContext`, although it's
deprecated. But the patch is simple, so I applied it to both functions.

This probably wasn't caught sooner because `span.addContext` gets used
way more, particularly with asynchronous spans. As evidence, the
auto-instrumentation does not use `api.addContext` at all!

Note that running npm automatically bumped the package-lock.json, since
v2.7.0 of the Beeline had been released without changing the lock file.
@vreynolds vreynolds merged commit 24ad15c into main Sep 21, 2021
@vreynolds vreynolds deleted the vondrak.fix-add-context branch September 21, 2021 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working version: bump major A PR that breaks backwards compatibility.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants