Skip to content

Commit 43ba51c

Browse files
Merge branch 'main' into feat/node/prom-client-implementation
2 parents d99c458 + 23aae01 commit 43ba51c

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

plugins/node/instrumentation-undici/src/internal-types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { Channel } from 'diagnostics_channel';
1716

1817
import { UndiciRequest, UndiciResponse } from './types';
1918

2019
export interface ListenerRecord {
2120
name: string;
22-
channel: Channel;
23-
onMessage: (message: any, name: string | symbol) => void;
21+
unsubscribe: () => void;
2422
}
2523

2624
export interface RequestMessage {

plugins/node/instrumentation-undici/src/undici.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
7777

7878
override disable(): void {
7979
super.disable();
80-
this._channelSubs.forEach(sub => sub.channel.unsubscribe(sub.onMessage));
80+
this._channelSubs.forEach(sub => sub.unsubscribe());
8181
this._channelSubs.length = 0;
8282
}
8383

@@ -137,14 +137,29 @@ export class UndiciInstrumentation extends InstrumentationBase<UndiciInstrumenta
137137

138138
private subscribeToChannel(
139139
diagnosticChannel: string,
140-
onMessage: ListenerRecord['onMessage']
140+
onMessage: (message: any, name: string | symbol) => void
141141
) {
142-
const channel = diagch.channel(diagnosticChannel);
143-
channel.subscribe(onMessage);
142+
// `diagnostics_channel` had a ref counting bug until v18.19.0.
143+
// https://github.com/nodejs/node/pull/47520
144+
const [major, minor] = process.version
145+
.replace('v', '')
146+
.split('.')
147+
.map(n => Number(n));
148+
const useNewSubscribe = major > 18 || (major === 18 && minor >= 19);
149+
150+
let unsubscribe: () => void;
151+
if (useNewSubscribe) {
152+
diagch.subscribe?.(diagnosticChannel, onMessage);
153+
unsubscribe = () => diagch.unsubscribe?.(diagnosticChannel, onMessage);
154+
} else {
155+
const channel = diagch.channel(diagnosticChannel);
156+
channel.subscribe(onMessage);
157+
unsubscribe = () => channel.unsubscribe(onMessage);
158+
}
159+
144160
this._channelSubs.push({
145161
name: diagnosticChannel,
146-
channel,
147-
onMessage,
162+
unsubscribe,
148163
});
149164
}
150165

0 commit comments

Comments
 (0)