44 */
55
66'use strict'
7+
78// eslint-disable-next-line n/no-unsupported-features/node-builtins
89const dc = require ( 'node:diagnostics_channel' )
10+ const recordSupportabilityMetric = require ( './record-supportability-metric.js' )
11+ const resolvePackageVersion = require ( './resolve-package-version.js' )
912
1013/**
1114 * The baseline parameters available to all subscribers.
@@ -34,6 +37,8 @@ const dc = require('node:diagnostics_channel')
3437 * This is the same string one would pass to the `require` function.
3538 */
3639class Subscriber {
40+ #usageMetricRecorded = false
41+
3742 /**
3843 * @param {SubscriberParams } params to function
3944 */
@@ -46,7 +51,7 @@ class Subscriber {
4651
4752 set channels ( channels ) {
4853 if ( ! Array . isArray ( channels ) ) {
49- throw new Error ( 'channels must be a collection of with propertiesof channel and hook' )
54+ throw new Error ( 'channels must be a collection of objects with properties channel and hook' )
5055 }
5156 this . _channels = channels
5257 }
@@ -73,18 +78,43 @@ class Subscriber {
7378
7479 subscribe ( ) {
7580 for ( let index = 0 ; index < this . channels . length ; index ++ ) {
81+ const chan = this . channels [ index ]
7682 const { hook, channel } = this . channels [ index ]
7783 const boundHook = hook . bind ( this )
78- dc . subscribe ( channel , boundHook )
79- this . channels [ index ] . boundHook = boundHook
84+ chan . boundHook = boundHook
85+ chan . eventHandler = ( message , name ) => {
86+ this . #supportability( )
87+ boundHook ( message , name )
88+ }
89+ dc . subscribe ( channel , chan . eventHandler )
8090 }
8191 }
8292
8393 unsubscribe ( ) {
8494 for ( let index = 0 ; index < this . channels . length ; index ++ ) {
85- const { channel, boundHook } = this . channels [ index ]
86- dc . unsubscribe ( channel , boundHook )
95+ const { channel, eventHandler } = this . channels [ index ]
96+ dc . unsubscribe ( channel , eventHandler )
97+ }
98+ }
99+
100+ /**
101+ * Since this class subscribes to tracing channels natively published by
102+ * target modules, we do not get the package metadata that Orchestrion
103+ * provides in its channel events. So we have to try and find the package
104+ * manifest and get the version out of it in order to record our
105+ * supportability metric.
106+ */
107+ #supportability( ) {
108+ if ( this . #usageMetricRecorded === true ) {
109+ return
87110 }
111+ const version = resolvePackageVersion ( this . id )
112+ recordSupportabilityMetric ( {
113+ agent : this . agent ,
114+ moduleName : this . id ,
115+ moduleVersion : version
116+ } )
117+ this . #usageMetricRecorded = true
88118 }
89119}
90120
0 commit comments