Skip to content

Commit ba59680

Browse files
blumamirZirak
authored andcommitted
feat(instrumentation): apply unwrap before wrap in base class (open-telemetry#4692)
* feat(instrumentation): apply unwrap before wrap in base class * chore: CHANGELOG
1 parent d9b235c commit ba59680

File tree

4 files changed

+5
-47
lines changed

4 files changed

+5
-47
lines changed

experimental/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All notable changes to experimental packages in this project will be documented
2020

2121
### :rocket: (Enhancement)
2222

23+
* feat(instrumentation): apply unwrap before wrap in base class [#4692](https://github.com/open-telemetry/opentelemetry-js/pull/4692)
2324
* feat(instrumentation): add util to execute span customization hook in base class [#4663](https://github.com/open-telemetry/opentelemetry-js/pull/4663) @blumamir
2425
* feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir
2526
* feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan

experimental/packages/opentelemetry-instrumentation-grpc/src/instrumentation.ts

-28
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import {
5353
} from '@opentelemetry/api';
5454
import {
5555
InstrumentationNodeModuleDefinition,
56-
isWrapped,
5756
InstrumentationBase,
5857
} from '@opentelemetry/instrumentation';
5958
import {
@@ -102,55 +101,28 @@ export class GrpcInstrumentation extends InstrumentationBase<GrpcInstrumentation
102101
'@grpc/grpc-js',
103102
['1.*'],
104103
moduleExports => {
105-
if (isWrapped(moduleExports.Server.prototype.register)) {
106-
this._unwrap(moduleExports.Server.prototype, 'register');
107-
}
108104
// Patch Server methods
109105
this._wrap(
110106
moduleExports.Server.prototype,
111107
'register',
112108
this._patchServer()
113109
);
114110
// Patch Client methods
115-
if (isWrapped(moduleExports.makeGenericClientConstructor)) {
116-
this._unwrap(moduleExports, 'makeGenericClientConstructor');
117-
}
118111
this._wrap(
119112
moduleExports,
120113
'makeGenericClientConstructor',
121114
this._patchClient(moduleExports)
122115
);
123-
if (isWrapped(moduleExports.makeClientConstructor)) {
124-
this._unwrap(moduleExports, 'makeClientConstructor');
125-
}
126116
this._wrap(
127117
moduleExports,
128118
'makeClientConstructor',
129119
this._patchClient(moduleExports)
130120
);
131-
if (isWrapped(moduleExports.loadPackageDefinition)) {
132-
this._unwrap(moduleExports, 'loadPackageDefinition');
133-
}
134121
this._wrap(
135122
moduleExports,
136123
'loadPackageDefinition',
137124
this._patchLoadPackageDefinition(moduleExports)
138125
);
139-
if (isWrapped(moduleExports.Client.prototype)) {
140-
this._unwrap(moduleExports.Client.prototype, 'makeUnaryRequest');
141-
this._unwrap(
142-
moduleExports.Client.prototype,
143-
'makeClientStreamRequest'
144-
);
145-
this._unwrap(
146-
moduleExports.Client.prototype,
147-
'makeServerStreamRequest'
148-
);
149-
this._unwrap(
150-
moduleExports.Client.prototype,
151-
'makeBidiStreamRequest'
152-
);
153-
}
154126
this._wrap(
155127
moduleExports.Client.prototype,
156128
'makeUnaryRequest',

experimental/packages/opentelemetry-instrumentation-http/src/http.ts

-19
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import { VERSION } from './version';
5353
import {
5454
InstrumentationBase,
5555
InstrumentationNodeModuleDefinition,
56-
isWrapped,
5756
safeExecuteInTheMiddle,
5857
} from '@opentelemetry/instrumentation';
5958
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
@@ -111,25 +110,16 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
111110
'http',
112111
['*'],
113112
(moduleExports: Http): Http => {
114-
if (isWrapped(moduleExports.request)) {
115-
this._unwrap(moduleExports, 'request');
116-
}
117113
this._wrap(
118114
moduleExports,
119115
'request',
120116
this._getPatchOutgoingRequestFunction('http')
121117
);
122-
if (isWrapped(moduleExports.get)) {
123-
this._unwrap(moduleExports, 'get');
124-
}
125118
this._wrap(
126119
moduleExports,
127120
'get',
128121
this._getPatchOutgoingGetFunction(moduleExports.request)
129122
);
130-
if (isWrapped(moduleExports.Server.prototype.emit)) {
131-
this._unwrap(moduleExports.Server.prototype, 'emit');
132-
}
133123
this._wrap(
134124
moduleExports.Server.prototype,
135125
'emit',
@@ -152,25 +142,16 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
152142
'https',
153143
['*'],
154144
(moduleExports: Https): Https => {
155-
if (isWrapped(moduleExports.request)) {
156-
this._unwrap(moduleExports, 'request');
157-
}
158145
this._wrap(
159146
moduleExports,
160147
'request',
161148
this._getPatchHttpsOutgoingRequestFunction('https')
162149
);
163-
if (isWrapped(moduleExports.get)) {
164-
this._unwrap(moduleExports, 'get');
165-
}
166150
this._wrap(
167151
moduleExports,
168152
'get',
169153
this._getPatchHttpsOutgoingGetFunction(moduleExports.request)
170154
);
171-
if (isWrapped(moduleExports.Server.prototype.emit)) {
172-
this._unwrap(moduleExports.Server.prototype, 'emit');
173-
}
174155
this._wrap(
175156
moduleExports.Server.prototype,
176157
'emit',

experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { diag } from '@opentelemetry/api';
3434
import type { OnRequireFn } from 'require-in-the-middle';
3535
import { Hook } from 'require-in-the-middle';
3636
import { readFileSync } from 'fs';
37+
import { isWrapped } from '../../utils';
3738

3839
/**
3940
* Base abstract class for instrumenting node plugins
@@ -79,6 +80,9 @@ export abstract class InstrumentationBase<
7980
}
8081

8182
protected override _wrap: typeof wrap = (moduleExports, name, wrapper) => {
83+
if (isWrapped(moduleExports[name])) {
84+
this._unwrap(moduleExports, name);
85+
}
8286
if (!utilTypes.isProxy(moduleExports)) {
8387
return wrap(moduleExports, name, wrapper);
8488
} else {

0 commit comments

Comments
 (0)