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

feat(instrumentation-express): propagate context and measure full handler spans #2638

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.97%. Comparing base (b641e1b) to head (5c016d9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2638   +/-   ##
=======================================
  Coverage   90.96%   90.97%           
=======================================
  Files         172      172           
  Lines        8137     8140    +3     
  Branches     1649     1649           
=======================================
+ Hits         7402     7405    +3     
  Misses        735      735           
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! 😄

@faranak-cs
Copy link

Waiting for the merge... 🤞

@@ -234,12 +237,15 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment