Skip to content

fix(@opentelemetry/instrumentation-dataloader): fix instrumentation of ESM-imported dataloader #2813

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 15 additions & 7 deletions plugins/node/instrumentation-dataloader/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
type LoadFn = (typeof Dataloader.prototype)['load'];
type LoadManyFn = (typeof Dataloader.prototype)['loadMany'];

function extractModuleExports(module: any) {
return module[Symbol.toStringTag] === 'Module'
? module.default // ESM

Check warning on line 49 in plugins/node/instrumentation-dataloader/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/instrumentation-dataloader/src/instrumentation.ts#L49

Added line #L49 was not covered by tests
: module; // CommonJS
}

export class DataloaderInstrumentation extends InstrumentationBase<DataloaderInstrumentationConfig> {
constructor(config: DataloaderInstrumentationConfig = {}) {
super(PACKAGE_NAME, PACKAGE_VERSION, config);
Expand All @@ -55,18 +61,20 @@
MODULE_NAME,
['>=2.0.0 <3'],
dataloader => {
this._patchLoad(dataloader.prototype);
this._patchLoadMany(dataloader.prototype);
const dataloaderExports = extractModuleExports(dataloader);
this._patchLoad(dataloaderExports.prototype);
this._patchLoadMany(dataloaderExports.prototype);

return this._getPatchedConstructor(dataloader);
return this._getPatchedConstructor(dataloaderExports);
},
dataloader => {
if (isWrapped(dataloader.prototype.load)) {
this._unwrap(dataloader.prototype, 'load');
const dataloaderExports = extractModuleExports(dataloader);
if (isWrapped(dataloaderExports.prototype.load)) {
this._unwrap(dataloaderExports.prototype, 'load');
}

if (isWrapped(dataloader.prototype.loadMany)) {
this._unwrap(dataloader.prototype, 'loadMany');
if (isWrapped(dataloaderExports.prototype.loadMany)) {
this._unwrap(dataloaderExports.prototype, 'loadMany');
}
}
) as InstrumentationNodeModuleDefinition,
Expand Down