@@ -11,7 +11,7 @@ import {
1111 GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION ,
1212 GLOBAL_INSTRUMENTATION_HOOKS_REGISTRY_BRAND ,
1313} from "../../global-instrumentation-hooks" ;
14- import type { FunctionQuery , InstrumentationConfig , ModuleType } from "./types" ;
14+ import type { FunctionQuery , InstrumentationConfig } from "./types" ;
1515
1616type AnyNode = any ;
1717type TransformFn = (
@@ -23,14 +23,14 @@ type TransformFn = (
2323type TraceOperator = "traceCallback" | "tracePromise" | "traceSync" ;
2424
2525export interface TransformState extends InstrumentationConfig {
26- moduleType : ModuleType ;
2726 moduleVersion : string ;
2827 functionQuery : FunctionQuery ;
2928 operator : TraceOperator ;
3029 functionIndex ?: number ;
3130}
3231
3332const CHANNEL_REGEX = / [ ^ \w ] / g;
33+ const SHARED_HOOK_LOOKUP = "tr_ch_apm$get_hook" ;
3434
3535function formatChannelVariable ( channelName : string ) : string {
3636 return `tr_ch_apm$${ channelName . replace ( CHANNEL_REGEX , "_" ) } ` ;
@@ -49,6 +49,10 @@ export const transforms: Record<string, TransformFn> = {
4949 } = state ;
5050 const channelVariable = formatChannelVariable ( channelName ) ;
5151 const channelGetter = formatChannelGetter ( channelName ) ;
52+ const hasSharedHookLookup = node . body . some (
53+ ( child : AnyNode ) =>
54+ child . declarations ?. [ 0 ] ?. id ?. name === SHARED_HOOK_LOOKUP ,
55+ ) ;
5256
5357 if (
5458 node . body . some (
@@ -58,12 +62,10 @@ export const transforms: Record<string, TransformFn> = {
5862 return ;
5963 }
6064
61- const index = node . body . findIndex (
62- ( child : AnyNode ) => child . directive === "use strict" ,
63- ) ;
64- const code = `
65- let ${ channelVariable } ;
66- const ${ channelGetter } = () => {
65+ const sharedHookLookup = hasSharedHookLookup
66+ ? ""
67+ : `
68+ const ${ SHARED_HOOK_LOOKUP } = (__apm$hookName, __apm$operator) => {
6769 try {
6870 const __apm$hooks = globalThis[${ JSON . stringify (
6971 GLOBAL_INSTRUMENTATION_HOOKS_KEY ,
@@ -76,7 +78,7 @@ export const transforms: Record<string, TransformFn> = {
7678 ) return undefined;
7779 const __apm$hook = Map.prototype.get.call(
7880 __apm$hooks,
79- "orchestrion: ${ name } : ${ channelName } "
81+ __apm$hookName
8082 );
8183 if (
8284 (__apm$hook === null ||
@@ -86,15 +88,34 @@ export const transforms: Record<string, TransformFn> = {
8688 GLOBAL_INSTRUMENTATION_HOOK_BRAND ,
8789 ) } )] !== ${ GLOBAL_INSTRUMENTATION_HOOKS_PROTOCOL_VERSION } ||
8890 typeof __apm$hook.hasSubscribers !== "boolean" ||
89- typeof __apm$hook[${ JSON . stringify ( operator ) } ] !== "function"
91+ typeof __apm$hook[__apm$ operator] !== "function"
9092 ) return undefined;
91- return ${ channelVariable } ??= __apm$hook;
93+ return __apm$hook;
9294 } catch {
9395 return undefined;
9496 }
9597 };
9698 ` ;
99+ const code = `
100+ ${ sharedHookLookup }
101+ let ${ channelVariable } ;
102+ const ${ channelGetter } = () =>
103+ ${ channelVariable } ??= ${ SHARED_HOOK_LOOKUP } (
104+ ${ JSON . stringify ( `orchestrion:${ name } :${ channelName } ` ) } ,
105+ ${ JSON . stringify ( operator ) }
106+ );
107+ ` ;
97108
109+ const sharedHookLookupIndex = node . body . findIndex (
110+ ( child : AnyNode ) =>
111+ child . declarations ?. [ 0 ] ?. id ?. name === SHARED_HOOK_LOOKUP ,
112+ ) ;
113+ const index =
114+ sharedHookLookupIndex === - 1
115+ ? node . body . findIndex (
116+ ( child : AnyNode ) => child . directive === "use strict" ,
117+ )
118+ : sharedHookLookupIndex ;
98119 node . body . splice ( index + 1 , 0 , ...parse ( code ) . body ) ;
99120 } ,
100121
@@ -139,18 +160,14 @@ function traceFunction(
139160 ( ! methodName && ! privateMethodName && ! functionName ) ;
140161 const type = isConstructor ? "ArrowFunctionExpression" : "FunctionExpression" ;
141162
142- node . body = wrap (
143- state ,
144- {
145- type,
146- params : node . params ,
147- body : node . body ,
148- async : node . async ,
149- expression : false ,
150- generator : node . generator ,
151- } ,
152- program ,
153- ) ;
163+ node . body = wrap ( state , {
164+ type,
165+ params : node . params ,
166+ body : node . body ,
167+ async : node . async ,
168+ expression : false ,
169+ generator : node . generator ,
170+ } ) ;
154171
155172 node . generator = false ;
156173 node . async = false ;
@@ -202,22 +219,17 @@ function traceInstanceMethod(
202219 const fn = ctorBody [ 1 ] . expression . right ;
203220
204221 fn . async = operator === "tracePromise" ;
205- fn . body = wrap (
206- state ,
207- { type : "Identifier" , name : `__apm$${ methodName } ` } ,
208- program ,
209- ) ;
222+ fn . body = wrap ( state , {
223+ type : "Identifier" ,
224+ name : `__apm$${ methodName } ` ,
225+ } ) ;
210226
211227 wrapSuper ( fn ) ;
212228
213229 ctor . value . body . body . push ( ...ctorBody ) ;
214230}
215231
216- function wrap (
217- state : TransformState ,
218- node : AnyNode ,
219- program ?: AnyNode ,
220- ) : AnyNode {
232+ function wrap ( state : TransformState , node : AnyNode ) : AnyNode {
221233 const { operator, moduleVersion } = state ;
222234
223235 const wrapper =
0 commit comments