Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opentelemetry-node): improved ESM instrumentation support #499

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
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';

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} =
createAddHookMessageChannel();

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.

Loading