Skip to content

Commit

Permalink
Fix OpenTelemetry CJS preloads (#9859)
Browse files Browse the repository at this point in the history
* Fix OpenTelemetry CJS preloads

* Improve comment

* Add changeset
  • Loading branch information
nwalters512 committed May 16, 2024
1 parent 4cabe17 commit b697912
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-apes-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@prairielearn/opentelemetry': patch
---

Fix CommonJS preloads of packages
13 changes: 9 additions & 4 deletions packages/opentelemetry/src/commonjs-preloads.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { createRequire } from 'module';

// The packages below were determined by inspecting the implementation of each
// instrumentation package and finding which packages/files they're patching.
const PRELOAD_PACKAGES = [
// @opentelemetry/instrumentation-aws
// @opentelemetry/instrumentation-aws-sdk
'@aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js',
'@aws-sdk/middleware-stack/dist-cjs/MiddlewareStack.js',
'@aws-sdk/middleware-stack',
'@smithy/middleware-stack',
'@aws-sdk/smithy-client',
'aws-sdk/lib/core.js',
'aws-sdk',
Expand All @@ -27,11 +30,13 @@ const PRELOAD_PACKAGES = [
'redis',
];

const require = createRequire(import.meta.url);
for (const pkg of PRELOAD_PACKAGES) {
try {
// TODO: switch to using `createRequire` once this is native ESM.
require(pkg);
} catch (e) {
// Ignore; package is likely not installed.
} catch (e: any) {
// If the package is not found, it's fine, it just means that it wasn't
// installed. We'll throw any other errors.
if (e.code !== 'MODULE_NOT_FOUND') throw e;
}
}

0 comments on commit b697912

Please sign in to comment.