Skip to content

Commit 31eb60d

Browse files
feat!(instrumentation): add patch and unpatch diag log messages (#4641)
* refactor(@opentelemetry/instrumentation): add patch and unpatch diag log messages * chore: CHANGELOG * fix: exclude version in core packages --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 99431df commit 31eb60d

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
99

1010
### :boom: Breaking Change
1111

12+
* feat(instrumentation): add patch and unpatch diag log messages [#4641](https://github.com/open-telemetry/opentelemetry-js/pull/4641)
13+
* Instrumentations should not log patch and unpatch messages to diag channel.
1214
* feat!(instrumentation): remove moduleExports generic type from instrumentation registration [#4598](https://github.com/open-telemetry/opentelemetry-js/pull/4598) @blumamir
1315
* breaking for instrumentation authors that depend on
1416
* `InstrumentationBase`

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ export class GrpcInstrumentation extends InstrumentationBase {
101101
new InstrumentationNodeModuleDefinition(
102102
'@grpc/grpc-js',
103103
['1.*'],
104-
(moduleExports, version) => {
105-
this._diag.debug(`Applying patch for @grpc/grpc-js@${version}`);
104+
moduleExports => {
106105
if (isWrapped(moduleExports.Server.prototype.register)) {
107106
this._unwrap(moduleExports.Server.prototype, 'register');
108107
}
@@ -174,9 +173,8 @@ export class GrpcInstrumentation extends InstrumentationBase {
174173
);
175174
return moduleExports;
176175
},
177-
(moduleExports, version) => {
176+
moduleExports => {
178177
if (moduleExports === undefined) return;
179-
this._diag.debug(`Removing patch for @grpc/grpc-js@${version}`);
180178

181179
this._unwrap(moduleExports.Server.prototype, 'register');
182180
this._unwrap(moduleExports, 'makeClientConstructor');

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,10 @@ export class HttpInstrumentation extends InstrumentationBase {
111111
}
112112

113113
private _getHttpInstrumentation() {
114-
const version = process.versions.node;
115114
return new InstrumentationNodeModuleDefinition(
116115
'http',
117116
['*'],
118117
(moduleExports: Http): Http => {
119-
this._diag.debug(`Applying patch for http@${version}`);
120118
if (isWrapped(moduleExports.request)) {
121119
this._unwrap(moduleExports, 'request');
122120
}
@@ -145,7 +143,6 @@ export class HttpInstrumentation extends InstrumentationBase {
145143
},
146144
(moduleExports: Http) => {
147145
if (moduleExports === undefined) return;
148-
this._diag.debug(`Removing patch for http@${version}`);
149146

150147
this._unwrap(moduleExports, 'request');
151148
this._unwrap(moduleExports, 'get');
@@ -155,12 +152,10 @@ export class HttpInstrumentation extends InstrumentationBase {
155152
}
156153

157154
private _getHttpsInstrumentation() {
158-
const version = process.versions.node;
159155
return new InstrumentationNodeModuleDefinition(
160156
'https',
161157
['*'],
162158
(moduleExports: Https): Https => {
163-
this._diag.debug(`Applying patch for https@${version}`);
164159
if (isWrapped(moduleExports.request)) {
165160
this._unwrap(moduleExports, 'request');
166161
}
@@ -189,7 +184,6 @@ export class HttpInstrumentation extends InstrumentationBase {
189184
},
190185
(moduleExports: Https) => {
191186
if (moduleExports === undefined) return;
192-
this._diag.debug(`Removing patch for https@${version}`);
193187

194188
this._unwrap(moduleExports, 'request');
195189
this._unwrap(moduleExports, 'get');

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ export abstract class InstrumentationBase
183183
if (typeof module.patch === 'function') {
184184
module.moduleExports = exports;
185185
if (this._enabled) {
186+
this._diag.debug(
187+
'Applying instrumentation patch for nodejs core module on require hook',
188+
{
189+
module: module.name,
190+
}
191+
);
186192
return module.patch(exports);
187193
}
188194
}
@@ -199,6 +205,14 @@ export abstract class InstrumentationBase
199205
if (typeof module.patch === 'function') {
200206
module.moduleExports = exports;
201207
if (this._enabled) {
208+
this._diag.debug(
209+
'Applying instrumentation patch for module on require hook',
210+
{
211+
module: module.name,
212+
version: module.moduleVersion,
213+
baseDir,
214+
}
215+
);
202216
return module.patch(exports, module.moduleVersion);
203217
}
204218
}
@@ -216,6 +230,16 @@ export abstract class InstrumentationBase
216230
return supportedFileInstrumentations.reduce<T>((patchedExports, file) => {
217231
file.moduleExports = patchedExports;
218232
if (this._enabled) {
233+
this._diag.debug(
234+
'Applying instrumentation patch for nodejs module file on require hook',
235+
{
236+
module: module.name,
237+
version: module.moduleVersion,
238+
fileName: file.name,
239+
baseDir,
240+
}
241+
);
242+
219243
// patch signature is not typed, so we cast it assuming it's correct
220244
return file.patch(patchedExports, module.moduleVersion) as T;
221245
}
@@ -233,10 +257,25 @@ export abstract class InstrumentationBase
233257
if (this._hooks.length > 0) {
234258
for (const module of this._modules) {
235259
if (typeof module.patch === 'function' && module.moduleExports) {
260+
this._diag.debug(
261+
'Applying instrumentation patch for nodejs module on instrumentation enabled',
262+
{
263+
module: module.name,
264+
version: module.moduleVersion,
265+
}
266+
);
236267
module.patch(module.moduleExports, module.moduleVersion);
237268
}
238269
for (const file of module.files) {
239270
if (file.moduleExports) {
271+
this._diag.debug(
272+
'Applying instrumentation patch for nodejs module file on instrumentation enabled',
273+
{
274+
module: module.name,
275+
version: module.moduleVersion,
276+
fileName: file.name,
277+
}
278+
);
240279
file.patch(file.moduleExports, module.moduleVersion);
241280
}
242281
}
@@ -279,10 +318,25 @@ export abstract class InstrumentationBase
279318

280319
for (const module of this._modules) {
281320
if (typeof module.unpatch === 'function' && module.moduleExports) {
321+
this._diag.debug(
322+
'Removing instrumentation patch for nodejs module on instrumentation disabled',
323+
{
324+
module: module.name,
325+
version: module.moduleVersion,
326+
}
327+
);
282328
module.unpatch(module.moduleExports, module.moduleVersion);
283329
}
284330
for (const file of module.files) {
285331
if (file.moduleExports) {
332+
this._diag.debug(
333+
'Removing instrumentation patch for nodejs module file on instrumentation disabled',
334+
{
335+
module: module.name,
336+
version: module.moduleVersion,
337+
fileName: file.name,
338+
}
339+
);
286340
file.unpatch(file.moduleExports, module.moduleVersion);
287341
}
288342
}

0 commit comments

Comments
 (0)