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(instrumentation): apply unwrap before wrap in base class #4692

Merged
merged 4 commits into from
May 13, 2024
Merged
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 experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(instrumentation): apply unwrap before wrap in base class [#4692](https://github.com/open-telemetry/opentelemetry-js/pull/4692)
* feat(instrumentation): add util to execute span customization hook in base class [#4663](https://github.com/open-telemetry/opentelemetry-js/pull/4663) @blumamir
* feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir
* feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import {
} from '@opentelemetry/api';
import {
InstrumentationNodeModuleDefinition,
isWrapped,
InstrumentationBase,
} from '@opentelemetry/instrumentation';
import {
Expand Down Expand Up @@ -102,55 +101,28 @@ export class GrpcInstrumentation extends InstrumentationBase<GrpcInstrumentation
'@grpc/grpc-js',
['1.*'],
moduleExports => {
if (isWrapped(moduleExports.Server.prototype.register)) {
this._unwrap(moduleExports.Server.prototype, 'register');
}
// Patch Server methods
this._wrap(
moduleExports.Server.prototype,
'register',
this._patchServer()
);
// Patch Client methods
if (isWrapped(moduleExports.makeGenericClientConstructor)) {
this._unwrap(moduleExports, 'makeGenericClientConstructor');
}
this._wrap(
moduleExports,
'makeGenericClientConstructor',
this._patchClient(moduleExports)
);
if (isWrapped(moduleExports.makeClientConstructor)) {
this._unwrap(moduleExports, 'makeClientConstructor');
}
this._wrap(
moduleExports,
'makeClientConstructor',
this._patchClient(moduleExports)
);
if (isWrapped(moduleExports.loadPackageDefinition)) {
this._unwrap(moduleExports, 'loadPackageDefinition');
}
this._wrap(
moduleExports,
'loadPackageDefinition',
this._patchLoadPackageDefinition(moduleExports)
);
if (isWrapped(moduleExports.Client.prototype)) {
this._unwrap(moduleExports.Client.prototype, 'makeUnaryRequest');
this._unwrap(
moduleExports.Client.prototype,
'makeClientStreamRequest'
);
this._unwrap(
moduleExports.Client.prototype,
'makeServerStreamRequest'
);
this._unwrap(
moduleExports.Client.prototype,
'makeBidiStreamRequest'
);
}
this._wrap(
moduleExports.Client.prototype,
'makeUnaryRequest',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import { VERSION } from './version';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
Expand Down Expand Up @@ -111,25 +110,16 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
'http',
['*'],
(moduleExports: Http): Http => {
if (isWrapped(moduleExports.request)) {
this._unwrap(moduleExports, 'request');
}
this._wrap(
moduleExports,
'request',
this._getPatchOutgoingRequestFunction('http')
);
if (isWrapped(moduleExports.get)) {
this._unwrap(moduleExports, 'get');
}
this._wrap(
moduleExports,
'get',
this._getPatchOutgoingGetFunction(moduleExports.request)
);
if (isWrapped(moduleExports.Server.prototype.emit)) {
this._unwrap(moduleExports.Server.prototype, 'emit');
}
this._wrap(
moduleExports.Server.prototype,
'emit',
Expand All @@ -152,25 +142,16 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
'https',
['*'],
(moduleExports: Https): Https => {
if (isWrapped(moduleExports.request)) {
this._unwrap(moduleExports, 'request');
}
this._wrap(
moduleExports,
'request',
this._getPatchHttpsOutgoingRequestFunction('https')
);
if (isWrapped(moduleExports.get)) {
this._unwrap(moduleExports, 'get');
}
this._wrap(
moduleExports,
'get',
this._getPatchHttpsOutgoingGetFunction(moduleExports.request)
);
if (isWrapped(moduleExports.Server.prototype.emit)) {
this._unwrap(moduleExports.Server.prototype, 'emit');
}
this._wrap(
moduleExports.Server.prototype,
'emit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { diag } from '@opentelemetry/api';
import type { OnRequireFn } from 'require-in-the-middle';
import { Hook } from 'require-in-the-middle';
import { readFileSync } from 'fs';
import { isWrapped } from '../../utils';

/**
* Base abstract class for instrumenting node plugins
Expand Down Expand Up @@ -79,6 +80,9 @@ export abstract class InstrumentationBase<
}

protected override _wrap: typeof wrap = (moduleExports, name, wrapper) => {
if (isWrapped(moduleExports[name])) {
this._unwrap(moduleExports, name);
}
if (!utilTypes.isProxy(moduleExports)) {
return wrap(moduleExports, name, wrapper);
} else {
Expand Down