Uh oh!
There was an error while loading. Please reload this page.
Add tracing plugin for undici/fetch #6939
Replies: 6 comments 3 replies
Awesome to see fetch finally land :) We'll add this to the backlog to work on, but PRs are welcome for anyone who wants to help out! |
Here's an example how we instrumented manually: exportclassHttpClientTracingimplementsHttpClient{constructor(privatereadonlyclient: HttpClient){}asyncsend(request: HttpRequest): Promise<HttpResponse>{consttransaction=Sentry.getCurrentHub().getScope()?.getTransaction();letspan;if(transaction){constcopyURL=newURL(request.getURL());copyURL.search="";span=transaction.startChild({op: "http.client",description: `${request.getMethod()}${copyURL.toString()}`,});}constresponse=awaitthis.client.send(request);if(span){span.setHttpStatus(response.getStatusCode());span.finish();}returnresponse;}} |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am using the diagnostics channel events to do that. Here is the code: // https://github.com/nodejs/undici/pull/1000/files// https://github.com/nodejs/undici/blob/7276126945c52cf7ec61460b36d19f882e1bab82/docs/api/DiagnosticsChannel.md?plain=1#L5importdiagnosticsChannelfrom'node:diagnostics_channel'import*asSentryfrom'@sentry/node'import{typeDiagnosticsChannel}from'undici'// Another approach is to just use a WeakMapconstSentrySpanSymbol=Symbol('kSentryUndiciRequestSpan')typeRequestTypedWithSymbol=DiagnosticsChannel.RequestCreateMessage['request']&{[SentrySpanSymbol]?: Sentry.Span}// we have to create the channel and keep it around, as diagnosticsChannel is using a WeakReference internally// if we did not capture the value, it would be garbage collected and the channel subscription would be removed// see: https://github.com/nodejs/node/pull/42714// Fixed with v19, for now, we gotta keep those exports aroundexportconstundiciCreateChannel=diagnosticsChannel.channel('undici:request:create')undiciCreateChannel.subscribe((msg)=>{const{ request }=msgasDiagnosticsChannel.RequestCreateMessageconstcurrentSpan=Sentry.getCurrentHub().getScope()?.getSpan()if(currentSpan){constcopyURL=newURL(request.path,request.origin)copyURL.search=''constspan=currentSpan.startChild({op: 'http.client',description: `${request.method??'NONE'}${copyURL.toString()}`,})constrequestTyped=requestasRequestTypedWithSymbolrequestTyped[SentrySpanSymbol]=span}})exportconstundiciHeadersChannel=diagnosticsChannel.channel('undici:request:headers')undiciHeadersChannel.subscribe((msg)=>{const{ request, response }=msgasDiagnosticsChannel.RequestHeadersMessageconstrequestTyped=requestasRequestTypedWithSymbolif(requestTyped[SentrySpanSymbol]){constspan=requestTyped[SentrySpanSymbol]span.setHttpStatus(response.statusCode)span.finish()}})exportconstundiciErrorChannel=diagnosticsChannel.channel('undici:request:error')undiciErrorChannel.subscribe((msg)=>{const{ request }=msgasDiagnosticsChannel.RequestErrorMessageconstrequestTyped=requestasRequestTypedWithSymbolif(requestTyped[SentrySpanSymbol]){constspan=requestTyped[SentrySpanSymbol]span.setStatus('unknown_error')span.finish()}})Just import that file before making the request and it is set up. |
Hey! I started to take a look at this here: #7582 |
Released with Sentry.init({integrations: [newSentry.Integrations.Undici()],})Supports Undici We're using it internally in our SvelteKit and NextJS SDK's and seems all good - but please let us know if something seems off. Thanks! |
So would this work without undici? So using Node 19, which has fetch build in already? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi Sentry team! 👋
nodejs/node#41749 has just been merged (adds fetch to Node.js).
The default tracing plugin that ships with the SDK adds a span for each HTTP request of the http/https modules from node.
This is great but we've been using undici for a while now and added manual instrumentation for it.
The newly merged fetch builds on top of undici.
Would be great if there was tracing support built-in for undici/fetch.
Thanks!
Edit from maintainers:
Released with
7.46.0of@sentry/node: https://docs.sentry.io/platforms/node/configuration/integrations/pluggable-integrations/#undiciSupports Undici
4.7.0or higher and requires Node16.7.0or higherAll reactions