Skip to content

Commit

Permalink
feat(opentelemetry-node): improved ESM instrumentation support
Browse files Browse the repository at this point in the history
This updates to usage of IITM's support for only hooking modules intended
to be hooked (added in IITM 1.11.0, see nodejs/import-in-the-middle#146).
This helps workaround cases where IITM hooking breaks some modules.
The openai-chat.mjs is one such example.
  • Loading branch information
trentm committed Dec 21, 2024
1 parent 8fddaf7 commit f66cfc0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build
.DS_Store
tmp
/packages/mockotlpserver/db
/openai.env
33 changes: 33 additions & 0 deletions examples/openai-chat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// An ES Modules (ESM) version of "openai-chat.js".
//
// Usage:
// OPENAI_API_KEY=sk-... \
// node --import @elastic/opentelemetry-node openai-chat.mjs

import {OpenAI} from 'openai';

const openai = new OpenAI();
const result = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{role: 'user', content: 'Why is the sky blue? Answer briefly.'}],
});
console.log(result.choices[0]?.message?.content);
59 changes: 19 additions & 40 deletions packages/opentelemetry-node/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,31 @@
// Register ESM hook and start the SDK.
// This is called for `--import @elastic/opentelemetry-node`.

import * as module from 'node:module';
import { register } from 'node:module';

Check failure on line 23 in packages/opentelemetry-node/import.mjs

View workflow job for this annotation

GitHub Actions / lint

Replace `·register·` with `register`
import {isMainThread} from 'node:worker_threads';

// TODO: @opentelemetry/instrumentation should re-export this IITM method.
// XXX add explicit IITM dep?
// XXX can we have a guard that there is only one install of IITM in play?
import { createAddHookMessageChannel } from 'import-in-the-middle';

Check failure on line 29 in packages/opentelemetry-node/import.mjs

View workflow job for this annotation

GitHub Actions / lint

Replace `·createAddHookMessageChannel·` with `createAddHookMessageChannel`

import {log} from './lib/logging.js';

/**
* Return true iff it looks like the `@elastic/opentelemetry-node/hook.mjs`
* was loaded via node's `--experimental-loader` option.
*
* Dev Note: keep this in sync with "require.js".
*/
function haveHookFromExperimentalLoader() {
const USED_LOADER_OPT =
/--(experimental-)?loader(\s+|=)@elastic\/opentelemetry-node\/hook.mjs/;
for (let i = 0; i < process.execArgv.length; i++) {
const arg = process.execArgv[i];
const nextArg = process.execArgv[i + 1];
if (
(arg === '--loader' || arg === '--experimental-loader') &&
nextArg === '@elastic/opentelemetry-node/hook.mjs'
) {
log.trace('bootstrap-import: --loader hook args used');
return true;
} else if (USED_LOADER_OPT.test(arg)) {
log.trace('bootstrap-import: --loader hook arg used');
return true;
}
}
if (
process.env.NODE_OPTIONS &&
USED_LOADER_OPT.test(process.env.NODE_OPTIONS)
) {
log.trace('bootstrap-import: --loader arg used in NODE_OPTIONS');
return true;
}
return false;
}
// Note: If there are *multiple* installations of import-in-the-middle, then
// only those instrumentations using this same one will be hooked.
const { registerOptions, waitForAllMessagesAcknowledged } =

Check failure on line 35 in packages/opentelemetry-node/import.mjs

View workflow job for this annotation

GitHub Actions / lint

Replace `·registerOptions,·waitForAllMessagesAcknowledged·` with `registerOptions,·waitForAllMessagesAcknowledged`
createAddHookMessageChannel();

Check failure on line 36 in packages/opentelemetry-node/import.mjs

View workflow job for this annotation

GitHub Actions / lint

Insert `··`

if (isMainThread) {
if (
typeof module.register === 'function' &&
!haveHookFromExperimentalLoader()
) {
log.trace('bootstrap-import: registering module hook');
module.register('./hook.mjs', import.meta.url);
}
log.trace('bootstrap-import: registering module hook');
// XXX module.register('./hook.mjs', import.meta.url);
// TODO: `@opentelemetry/instrumentation/hook.mjs` needs to re-export initialize
// register('@opentelemetry/instrumentation/hook.mjs', import.meta.url, registerOptions);
register('import-in-the-middle/hook.mjs', import.meta.url, registerOptions);

await import('./lib/start.js');

// Ensure that the loader has acknowledged all the modules before we allow
// execution to continue.
await waitForAllMessagesAcknowledged();
}
14 changes: 7 additions & 7 deletions packages/opentelemetry-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f66cfc0

Please sign in to comment.