|
1 | 1 | import type { Client } from '@sentry/core'; |
2 | | -import { addNonEnumerableProperty } from '@sentry/core'; |
| 2 | +import { addNonEnumerableProperty, waitForTracingChannelBinding } from '@sentry/core'; |
3 | 3 | import { getOrchestrionInjectedModules } from './detect'; |
| 4 | +import * as diagnosticsChannel from 'node:diagnostics_channel'; |
4 | 5 |
|
5 | 6 | const INSTRUMENTATION_FN_SYMBOL = Symbol.for('InstrumentationFn'); |
6 | | -type InstrumentationFn = (() => void) & { [INSTRUMENTATION_FN_SYMBOL]?: boolean }; |
| 7 | +// oxlint-disable-next-line typescript/no-explicit-any |
| 8 | +type InstrumentationFn = ((...args: any[]) => void) & { [INSTRUMENTATION_FN_SYMBOL]?: boolean }; |
7 | 9 |
|
8 | 10 | /** |
9 | | - * Instrument the provided callback to run when one of the provided module names has been orchestrion-injected already. |
10 | | - * Make sure to pass the same callback instance to this as is passed to `instrumentOrchestrionLazy`. |
| 11 | + * Run the provided instrumentation callback when one of the provided module names is orchestrion-injected. |
| 12 | + * If it is already injected, it will invoce the callback immediately (e.g. when build-time injection is used). |
| 13 | + * If runtime injection is used, it may invoke the callback at a later point in time, when the injection actually happens. |
| 14 | + * The callback will never be invoked more than once. |
11 | 15 | */ |
12 | | -export function instrumentOrchestrion(moduleNames: string[], callback: InstrumentationFn) { |
| 16 | +export function invokeOrchestrionInstrumentation<Callback extends InstrumentationFn>( |
| 17 | + client: Client, |
| 18 | + moduleNames: string[], |
| 19 | + callback: Callback, |
| 20 | + args: Parameters<Callback>, |
| 21 | +) { |
| 22 | + // `tracingChannel` is unavailable before Node 18.19 so do nothing in that case. |
| 23 | + if (!diagnosticsChannel.tracingChannel) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // If already injected, skip |
13 | 28 | if (hasBeenInjected(callback)) { |
14 | 29 | return; |
15 | 30 | } |
16 | 31 |
|
17 | | - const modules = getOrchestrionInjectedModules(); |
18 | | - if (moduleNames.some(name => modules.includes(name))) { |
19 | | - callback(); |
| 32 | + const instrumentationFn = () => { |
20 | 33 | markAsInjected(callback); |
21 | | - } |
22 | | -} |
23 | 34 |
|
24 | | -/** |
25 | | - * Run the provided instrumentation callback when one of the provided module names has been orchestrion-injected at runtime. |
26 | | - * This listens to the `orchestrion.module-runtime-injected` event and runs the callback when the event is emitted. |
27 | | - * Make sure to pass the same callback instance to this as is passed to `instrumentOrchestrion`. |
28 | | - */ |
29 | | -export function instrumentOrchestrionLazy(client: Client, moduleNames: string[], callback: InstrumentationFn) { |
30 | | - if (hasBeenInjected(callback)) { |
| 35 | + waitForTracingChannelBinding(() => { |
| 36 | + callback(...args); |
| 37 | + }); |
| 38 | + }; |
| 39 | + |
| 40 | + // First, check if the modules have been injected already |
| 41 | + const modules = getOrchestrionInjectedModules(); |
| 42 | + if (moduleNames.some(name => modules.includes(name))) { |
| 43 | + instrumentationFn(); |
31 | 44 | return; |
32 | 45 | } |
33 | 46 |
|
| 47 | + // Then, register a listener for the `orchestrion.module-runtime-injected` event |
34 | 48 | const cleanup = client.on('orchestrion.module-runtime-injected', (moduleName: string) => { |
| 49 | + if (hasBeenInjected(callback)) { |
| 50 | + cleanup(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
35 | 54 | if (moduleNames.includes(moduleName)) { |
36 | | - if (hasBeenInjected(callback)) { |
37 | | - cleanup(); |
38 | | - return; |
39 | | - } |
40 | | - callback(); |
41 | | - markAsInjected(callback); |
| 55 | + instrumentationFn(); |
42 | 56 | cleanup(); |
43 | 57 | } |
44 | 58 | }); |
|
0 commit comments