Skip to content

Conversation

@raphael-theriault-swi
Copy link
Member

Which problem is this PR solving?

Currently the Express instrumentation does not propagate context and does not properly instrument request handlers.

Short description of the changes

Context is propagated for middleware and request handler spans, while their next callback is reset to the parent context to avoid extreme nesting as suggested in #2022. Request handler spans are also no longer ended prematurely.

Router spans are kept as-is since they are broken and propagating context for them would make things even more confusing than they already are.

Example traces

Before

example trace before the changes

After

example trace after the changes

Related issues

@raphael-theriault-swi
Copy link
Member Author

cc @pichlermarc fixes discussed wednesday

@codecov
Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.52%. Comparing base (b947650) to head (172695e).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2638   +/-   ##
=======================================
  Coverage   91.52%   91.52%           
=======================================
  Files         171      171           
  Lines        8150     8153    +3     
  Branches     1653     1653           
=======================================
+ Hits         7459     7462    +3     
  Misses        691      691           
Files with missing lines Coverage Δ
...try-instrumentation-express/src/instrumentation.ts 98.61% <100.00%> (+0.02%) ⬆️

Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

this looks good, thank you for working on this. 🙌
cc @JamieDanielson and @pkanal (component owners) for comments :)

@mikesir87
Copy link

Apologies for the outsider coming in on this PR, but reporting that I've manually applied this PR into a local working copy to validate it works with my sample apps and it worked! In fact, it already helped me catch a case or two where I was missing an await call on a Kafka publish or DB update that was running after the request handler had returned. Excited to see this contribution! 😄

@faranahmadk
Copy link

Waiting for the merge... 🤞

}

let spanHasEnded = false;
// TODO: Fix router spans (getRouterPath does not work properly) to
Copy link
Member

Choose a reason for hiding this comment

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

Can you expand on what you mean by not working properly or create an issue so this doesn't get lost?

Copy link
Member Author

Choose a reason for hiding this comment

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

You can see in the nested routers test that the created router spans never include the /posts section of the route. If I were to propagate context and nest them we'd end up with /api/user/:id -> /:postId -> /api/user/posts/:postId which is really confusing.

@raphael-theriault-swi
Copy link
Member Author

@JamieDanielson @pkanal Sorry, gonna bother you again for an approval on this one :)

Copy link
Member

@JamieDanielson JamieDanielson left a comment

Choose a reason for hiding this comment

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

Thank you for working on this! Should we mark this as a breaking change (still experimental but mostly for signaling) since it updates parent span ID for some spans?

@pichlermarc pichlermarc changed the title feat(instrumentation-express): propagate context and measure full handler spans feat(instrumentation-express)!: propagate context and measure full handler spans Feb 26, 2025
@pichlermarc
Copy link
Member

@JamieDanielson good point, I changed the title so that it's categorized as such. 🙂

@pichlermarc pichlermarc merged commit 353dbb0 into open-telemetry:main Feb 26, 2025
26 checks passed
@dyladan dyladan mentioned this pull request Feb 26, 2025
deejay1 pushed a commit to deejay1/opentelemetry-js-contrib that referenced this pull request Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

8 participants