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! |
Beta Was this translation helpful? Give feedback.
-
Here's an example how we instrumented manually: export class HttpClientTracing implements HttpClient {
constructor(private readonly client: HttpClient) {}
async send(request: HttpRequest): Promise<HttpResponse> {
const transaction = Sentry.getCurrentHub().getScope()?.getTransaction();
let span;
if (transaction) {
const copyURL = new URL(request.getURL());
copyURL.search = "";
span = transaction.startChild({
op: "http.client",
description: `${request.getMethod()} ${copyURL.toString()}`,
});
}
const response = await this.client.send(request);
if (span) {
span.setHttpStatus(response.getStatusCode());
span.finish();
}
return response;
}
} |
Beta Was this translation helpful? Give feedback.
-
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#L5
import diagnosticsChannel from 'node:diagnostics_channel'
import * as Sentry from '@sentry/node'
import { type DiagnosticsChannel } from 'undici'
// Another approach is to just use a WeakMap
const SentrySpanSymbol = Symbol('kSentryUndiciRequestSpan')
type RequestTypedWithSymbol = 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 around
export const undiciCreateChannel = diagnosticsChannel.channel('undici:request:create')
undiciCreateChannel.subscribe((msg) => {
const { request } = msg as DiagnosticsChannel.RequestCreateMessage
const currentSpan = Sentry.getCurrentHub().getScope()?.getSpan()
if (currentSpan) {
const copyURL = new URL(request.path, request.origin)
copyURL.search = ''
const span = currentSpan.startChild({
op: 'http.client',
description: `${request.method ?? 'NONE'} ${copyURL.toString()}`,
})
const requestTyped = request as RequestTypedWithSymbol
requestTyped[SentrySpanSymbol] = span
}
})
export const undiciHeadersChannel = diagnosticsChannel.channel('undici:request:headers')
undiciHeadersChannel.subscribe((msg) => {
const { request, response } = msg as DiagnosticsChannel.RequestHeadersMessage
const requestTyped = request as RequestTypedWithSymbol
if (requestTyped[SentrySpanSymbol]) {
const span = requestTyped[SentrySpanSymbol]
span.setHttpStatus(response.statusCode)
span.finish()
}
})
export const undiciErrorChannel = diagnosticsChannel.channel('undici:request:error')
undiciErrorChannel.subscribe((msg) => {
const { request } = msg as DiagnosticsChannel.RequestErrorMessage
const requestTyped = request as RequestTypedWithSymbol
if (requestTyped[SentrySpanSymbol]) {
const span = requestTyped[SentrySpanSymbol]
span.setStatus('unknown_error')
span.finish()
}
}) Just import that file before making the request and it is set up. |
Beta Was this translation helpful? Give feedback.
-
Hey! I started to take a look at this here: #7582 |
Beta Was this translation helpful? Give feedback.
-
Released with Sentry.init({
integrations: [new Sentry.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! |
Beta Was this translation helpful? Give feedback.
-
So would this work without undici? So using Node 19, which has fetch build in already? |
Beta Was this translation helpful? Give feedback.
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.0
of@sentry/node
: https://docs.sentry.io/platforms/node/configuration/integrations/pluggable-integrations/#undiciSupports Undici
4.7.0
or higher and requires Node16.7.0
or higherBeta Was this translation helpful? Give feedback.
All reactions