Skip to content

Commit c9cb2e9

Browse files
committed
update to latest dc
1 parent 17ea1af commit c9cb2e9

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

packages/node/src/integrations/diagnostics_channel.d.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vendored from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8fc6d58e6434810867a9483e2107ea51bfca9153/types/node/diagnostics_channel.d.ts
1+
// Vendored from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5a94716c6788f654aea7999a5fc28f4f1e7c48ad/types/node/diagnostics_channel.d.ts
22

33
// License:
44
// This project is licensed under the MIT license.
@@ -77,6 +77,45 @@ declare module 'diagnostics_channel' {
7777
*/
7878
function channel(name: string | symbol): Channel;
7979
type ChannelListener = (message: unknown, name: string | symbol) => void;
80+
/**
81+
* Register a message handler to subscribe to this channel. This message handler will be run synchronously
82+
* whenever a message is published to the channel. Any errors thrown in the message handler will
83+
* trigger an 'uncaughtException'.
84+
*
85+
* ```js
86+
* import diagnostics_channel from 'diagnostics_channel';
87+
*
88+
* diagnostics_channel.subscribe('my-channel', (message, name) => {
89+
* // Received data
90+
* });
91+
* ```
92+
*
93+
* @since v18.7.0, v16.17.0
94+
* @param name The channel name
95+
* @param onMessage The handler to receive channel messages
96+
*/
97+
function subscribe(name: string | symbol, onMessage: ChannelListener): void;
98+
/**
99+
* Remove a message handler previously registered to this channel with diagnostics_channel.subscribe(name, onMessage).
100+
*
101+
* ```js
102+
* import diagnostics_channel from 'diagnostics_channel';
103+
*
104+
* function onMessage(message, name) {
105+
* // Received data
106+
* }
107+
*
108+
* diagnostics_channel.subscribe('my-channel', onMessage);
109+
*
110+
* diagnostics_channel.unsubscribe('my-channel', onMessage);
111+
* ```
112+
*
113+
* @since v18.7.0, v16.17.0
114+
* @param name The channel name
115+
* @param onMessage The previous subscribed handler to remove
116+
* @returns `true` if the handler was found, `false` otherwise
117+
*/
118+
function unsubscribe(name: string | symbol, onMessage: ChannelListener): boolean;
80119
/**
81120
* The class `Channel` represents an individual named channel within the data
82121
* pipeline. It is use to track subscribers and to publish messages when there

packages/node/src/integrations/undici.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ export class Undici implements Integration {
5656
*/
5757
public name: string = Undici.id;
5858

59-
// Have to hold all built channels in memory otherwise they get garbage collected
60-
// See: https://github.com/nodejs/node/pull/42714
61-
// This has been fixed in Node 19+
62-
private _channels = new Set<DiagnosticsChannel.Channel>();
63-
6459
private readonly _options: UndiciOptions;
6560

6661
public constructor(_options: Partial<UndiciOptions> = {}) {
@@ -82,13 +77,12 @@ export class Undici implements Integration {
8277
// no-op
8378
}
8479

85-
if (!ds) {
80+
if (!ds || !ds.subscribe) {
8681
return;
8782
}
8883

8984
// https://github.com/nodejs/undici/blob/e6fc80f809d1217814c044f52ed40ef13f21e43c/docs/api/DiagnosticsChannel.md
90-
const requestCreateChannel = this._setupChannel(ds, ChannelName.RequestCreate);
91-
requestCreateChannel.subscribe(message => {
85+
ds.subscribe(ChannelName.RequestCreate, message => {
9286
const { request } = message as RequestCreateMessage;
9387

9488
const url = new URL(request.path, request.origin);
@@ -105,12 +99,12 @@ export class Undici implements Integration {
10599
const activeSpan = scope.getSpan();
106100

107101
if (activeSpan && client) {
108-
const options = client.getOptions();
102+
const clientOptions = client.getOptions();
109103

110104
// eslint-disable-next-line deprecation/deprecation
111-
const shouldCreateSpan = options.shouldCreateSpanForRequest
105+
const shouldCreateSpan = clientOptions.shouldCreateSpanForRequest
112106
? // eslint-disable-next-line deprecation/deprecation
113-
options.shouldCreateSpanForRequest(stringUrl)
107+
clientOptions.shouldCreateSpanForRequest(stringUrl)
114108
: true;
115109

116110
if (shouldCreateSpan) {
@@ -131,9 +125,9 @@ export class Undici implements Integration {
131125
request.__sentry__ = span;
132126

133127
// eslint-disable-next-line deprecation/deprecation
134-
const shouldPropagate = options.tracePropagationTargets
128+
const shouldPropagate = clientOptions.tracePropagationTargets
135129
? // eslint-disable-next-line deprecation/deprecation
136-
stringMatchesSomePattern(stringUrl, options.tracePropagationTargets)
130+
stringMatchesSomePattern(stringUrl, clientOptions.tracePropagationTargets)
137131
: true;
138132

139133
if (shouldPropagate) {
@@ -151,8 +145,7 @@ export class Undici implements Integration {
151145
}
152146
});
153147

154-
const requestEndChannel = this._setupChannel(ds, ChannelName.RequestEnd);
155-
requestEndChannel.subscribe(message => {
148+
ds.subscribe(ChannelName.RequestEnd, message => {
156149
const { request, response } = message as RequestEndMessage;
157150

158151
const url = new URL(request.path, request.origin);
@@ -188,8 +181,7 @@ export class Undici implements Integration {
188181
}
189182
});
190183

191-
const requestErrorChannel = this._setupChannel(ds, ChannelName.RequestError);
192-
requestErrorChannel.subscribe(message => {
184+
ds.subscribe(ChannelName.RequestError, message => {
193185
const { request } = message as RequestErrorMessage;
194186

195187
const url = new URL(request.path, request.origin);
@@ -224,14 +216,4 @@ export class Undici implements Integration {
224216
}
225217
});
226218
}
227-
228-
/** */
229-
private _setupChannel(
230-
ds: typeof DiagnosticsChannel,
231-
name: Parameters<typeof DiagnosticsChannel.channel>[0],
232-
): DiagnosticsChannel.Channel {
233-
const channel = ds.channel(name);
234-
this._channels.add(channel);
235-
return channel;
236-
}
237219
}

0 commit comments

Comments
 (0)