|
20 | 20 | // Register ESM hook and start the SDK.
|
21 | 21 | // This is called for `--import @elastic/opentelemetry-node`.
|
22 | 22 |
|
23 |
| -import * as module from 'node:module'; |
| 23 | +import { register } from 'node:module'; |
24 | 24 | import {isMainThread} from 'node:worker_threads';
|
25 | 25 |
|
| 26 | +// TODO: @opentelemetry/instrumentation should re-export this IITM method. |
| 27 | +// XXX add explicit IITM dep? |
| 28 | +// XXX can we have a guard that there is only one install of IITM in play? |
| 29 | +import { createAddHookMessageChannel } from 'import-in-the-middle'; |
| 30 | + |
26 | 31 | import {log} from './lib/logging.js';
|
27 | 32 |
|
28 |
| -/** |
29 |
| - * Return true iff it looks like the `@elastic/opentelemetry-node/hook.mjs` |
30 |
| - * was loaded via node's `--experimental-loader` option. |
31 |
| - * |
32 |
| - * Dev Note: keep this in sync with "require.js". |
33 |
| - */ |
34 |
| -function haveHookFromExperimentalLoader() { |
35 |
| - const USED_LOADER_OPT = |
36 |
| - /--(experimental-)?loader(\s+|=)@elastic\/opentelemetry-node\/hook.mjs/; |
37 |
| - for (let i = 0; i < process.execArgv.length; i++) { |
38 |
| - const arg = process.execArgv[i]; |
39 |
| - const nextArg = process.execArgv[i + 1]; |
40 |
| - if ( |
41 |
| - (arg === '--loader' || arg === '--experimental-loader') && |
42 |
| - nextArg === '@elastic/opentelemetry-node/hook.mjs' |
43 |
| - ) { |
44 |
| - log.trace('bootstrap-import: --loader hook args used'); |
45 |
| - return true; |
46 |
| - } else if (USED_LOADER_OPT.test(arg)) { |
47 |
| - log.trace('bootstrap-import: --loader hook arg used'); |
48 |
| - return true; |
49 |
| - } |
50 |
| - } |
51 |
| - if ( |
52 |
| - process.env.NODE_OPTIONS && |
53 |
| - USED_LOADER_OPT.test(process.env.NODE_OPTIONS) |
54 |
| - ) { |
55 |
| - log.trace('bootstrap-import: --loader arg used in NODE_OPTIONS'); |
56 |
| - return true; |
57 |
| - } |
58 |
| - return false; |
59 |
| -} |
| 33 | +// Note: If there are *multiple* installations of import-in-the-middle, then |
| 34 | +// only those instrumentations using this same one will be hooked. |
| 35 | +const { registerOptions, waitForAllMessagesAcknowledged } = |
| 36 | + createAddHookMessageChannel(); |
60 | 37 |
|
61 | 38 | if (isMainThread) {
|
62 |
| - if ( |
63 |
| - typeof module.register === 'function' && |
64 |
| - !haveHookFromExperimentalLoader() |
65 |
| - ) { |
66 |
| - log.trace('bootstrap-import: registering module hook'); |
67 |
| - module.register('./hook.mjs', import.meta.url); |
68 |
| - } |
| 39 | + log.trace('bootstrap-import: registering module hook'); |
| 40 | + // XXX module.register('./hook.mjs', import.meta.url); |
| 41 | + // TODO: `@opentelemetry/instrumentation/hook.mjs` needs to re-export initialize |
| 42 | + // register('@opentelemetry/instrumentation/hook.mjs', import.meta.url, registerOptions); |
| 43 | + register('import-in-the-middle/hook.mjs', import.meta.url, registerOptions); |
69 | 44 |
|
70 | 45 | await import('./lib/start.js');
|
| 46 | + |
| 47 | + // Ensure that the loader has acknowledged all the modules before we allow |
| 48 | + // execution to continue. |
| 49 | + await waitForAllMessagesAcknowledged(); |
71 | 50 | }
|
0 commit comments