Skip to content

Commit

Permalink
feat: additional instrumentation debug diagnostics (#1777)
Browse files Browse the repository at this point in the history
* feat: additional instrumentation diagnostics

* fix: use componentlogger

---------

Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
seemk and blumamir authored Nov 8, 2023
1 parent 7348635 commit adbe86d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
* limitations under the License.
*/

import {
context,
diag,
trace,
isSpanContextValid,
Span,
} from '@opentelemetry/api';
import { context, trace, isSpanContextValid, Span } from '@opentelemetry/api';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
Expand All @@ -43,7 +37,8 @@ export class BunyanInstrumentation extends InstrumentationBase<
new InstrumentationNodeModuleDefinition<typeof BunyanLogger>(
'bunyan',
['<2.0'],
logger => {
(logger, moduleVersion) => {
this._diag.debug(`Applying patch for bunyan@${moduleVersion}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const proto = logger.prototype as any;
if (isWrapped(proto['_emit'])) {
Expand All @@ -58,8 +53,9 @@ export class BunyanInstrumentation extends InstrumentationBase<
);
return logger;
},
logger => {
(logger, moduleVersion) => {
if (logger === undefined) return;
this._diag.debug(`Removing patch for bunyan@${moduleVersion}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._unwrap(logger.prototype as any, '_emit');
}
Expand Down Expand Up @@ -114,7 +110,7 @@ export class BunyanInstrumentation extends InstrumentationBase<
() => hook(span, record),
err => {
if (err) {
diag.error('bunyan instrumentation: error calling logHook', err);
this._diag.error('error calling logHook', err);
}
},
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {
return new InstrumentationNodeModuleDefinition<any>(
'cassandra-driver',
supportedVersions,
driverModule => {
(driverModule, moduleVersion) => {
this._diag.debug(
`Applying patch for cassandra-driver@${moduleVersion}`
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Client = driverModule.Client.prototype as any;

Expand All @@ -73,7 +76,10 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {

return driverModule;
},
driverModule => {
(driverModule, moduleVersion) => {
this._diag.debug(
`Removing patch for cassandra-driver@${moduleVersion}`
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Client = driverModule.Client.prototype as any;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class GraphQLInstrumentation extends InstrumentationBase {
supportedVersions,
// cannot make it work with appropriate type as execute function has 2
//types and/cannot import function but only types
(moduleExports: any) => {
(moduleExports: any, moduleVersion) => {
this._diag.debug(`Applying patch for graphql@${moduleVersion} execute`);
if (isWrapped(moduleExports.execute)) {
this._unwrap(moduleExports, 'execute');
}
Expand All @@ -113,8 +114,11 @@ export class GraphQLInstrumentation extends InstrumentationBase {
);
return moduleExports;
},
moduleExports => {
(moduleExports, moduleVersion) => {
if (moduleExports) {
this._diag.debug(
`Removing patch for graphql@${moduleVersion} execute`
);
this._unwrap(moduleExports, 'execute');
}
}
Expand All @@ -127,15 +131,17 @@ export class GraphQLInstrumentation extends InstrumentationBase {
return new InstrumentationNodeModuleFile<typeof graphqlTypes>(
'graphql/language/parser.js',
supportedVersions,
moduleExports => {
if (isWrapped(moduleExports.execute)) {
(moduleExports, moduleVersion) => {
this._diag.debug(`Applying patch for graphql@${moduleVersion} parse`);
if (isWrapped(moduleExports.parse)) {
this._unwrap(moduleExports, 'parse');
}
this._wrap(moduleExports, 'parse', this._patchParse());
return moduleExports;
},
moduleExports => {
(moduleExports, moduleVersion) => {
if (moduleExports) {
this._diag.debug(`Removing patch for graphql@${moduleVersion} parse`);
this._unwrap(moduleExports, 'parse');
}
}
Expand All @@ -148,15 +154,21 @@ export class GraphQLInstrumentation extends InstrumentationBase {
return new InstrumentationNodeModuleFile<typeof graphqlTypes>(
'graphql/validation/validate.js',
supportedVersions,
moduleExports => {
if (isWrapped(moduleExports.execute)) {
(moduleExports, moduleVersion) => {
this._diag.debug(
`Applying patch for graphql@${moduleVersion} validate`
);
if (isWrapped(moduleExports.validate)) {
this._unwrap(moduleExports, 'validate');
}
this._wrap(moduleExports, 'validate', this._patchValidate());
return moduleExports;
},
moduleExports => {
(moduleExports, moduleVersion) => {
if (moduleExports) {
this._diag.debug(
`Removing patch for graphql@${moduleVersion} validate`
);
this._unwrap(moduleExports, 'validate');
}
}
Expand Down Expand Up @@ -285,7 +297,7 @@ export class GraphQLInstrumentation extends InstrumentationBase {
},
err => {
if (err) {
api.diag.error('Error running response hook', err);
this._diag.error('Error running response hook', err);
}

endSpan(span, undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {

import {
context,
diag,
trace,
Span,
SpanStatusCode,
Expand Down Expand Up @@ -66,7 +65,8 @@ export class PgInstrumentation extends InstrumentationBase {
const modulePG = new InstrumentationNodeModuleDefinition<typeof pgTypes>(
'pg',
['8.*'],
(module: any) => {
(module: any, moduleVersion) => {
this._diag.debug(`Applying patch for pg@${moduleVersion}`);
const moduleExports: typeof pgTypes =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
Expand All @@ -93,11 +93,12 @@ export class PgInstrumentation extends InstrumentationBase {

return module;
},
(module: any) => {
(module: any, moduleVersion) => {
const moduleExports: typeof pgTypes =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
this._diag.debug(`Removing patch for pg@${moduleVersion}`);
if (isWrapped(moduleExports.Client.prototype.query)) {
this._unwrap(moduleExports.Client.prototype, 'query');
}
Expand All @@ -109,7 +110,8 @@ export class PgInstrumentation extends InstrumentationBase {
>(
'pg-pool',
['2.*', '3.*'],
moduleExports => {
(moduleExports, moduleVersion) => {
this._diag.debug(`Applying patch for pg-pool@${moduleVersion}`);
if (isWrapped(moduleExports.prototype.connect)) {
this._unwrap(moduleExports.prototype, 'connect');
}
Expand All @@ -120,7 +122,8 @@ export class PgInstrumentation extends InstrumentationBase {
);
return moduleExports;
},
moduleExports => {
(moduleExports, moduleVersion) => {
this._diag.debug(`Removing patch for pg-pool@${moduleVersion}`);
if (isWrapped(moduleExports.prototype.connect)) {
this._unwrap(moduleExports.prototype, 'connect');
}
Expand Down Expand Up @@ -180,7 +183,7 @@ export class PgInstrumentation extends InstrumentationBase {
private _getClientQueryPatch() {
const plugin = this;
return (original: typeof pgTypes.Client.prototype.query) => {
diag.debug(
this._diag.debug(
`Patching ${PgInstrumentation.COMPONENT}.Client.prototype.query`
);
return function query(this: PgClientExtended, ...args: unknown[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import {
context,
diag,
trace,
isSpanContextValid,
Span,
Expand Down Expand Up @@ -57,16 +56,18 @@ export class WinstonInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleFile<Winston3Logger>(
'winston/lib/winston/logger.js',
winston3Versions,
logger => {
(logger, moduleVersion) => {
this._diag.debug(`Applying patch for winston@${moduleVersion}`);
if (isWrapped(logger.prototype['write'])) {
this._unwrap(logger.prototype, 'write');
}

this._wrap(logger.prototype, 'write', this._getPatchedWrite());
return logger;
},
logger => {
(logger, moduleVersion) => {
if (logger === undefined) return;
this._diag.debug(`Removing patch for winston@${moduleVersion}`);
this._unwrap(logger.prototype, 'write');
}
),
Expand All @@ -81,7 +82,8 @@ export class WinstonInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleFile<Winston2LoggerModule>(
'winston/lib/winston/logger.js',
winstonPre3Versions,
fileExports => {
(fileExports, moduleVersion) => {
this._diag.debug(`Applying patch for winston@${moduleVersion}`);
const proto = fileExports.Logger.prototype;

if (isWrapped(proto.log)) {
Expand All @@ -92,8 +94,9 @@ export class WinstonInstrumentation extends InstrumentationBase {

return fileExports;
},
fileExports => {
(fileExports, moduleVersion) => {
if (fileExports === undefined) return;
this._diag.debug(`Removing patch for winston@${moduleVersion}`);
this._unwrap(fileExports.Logger.prototype, 'log');
}
),
Expand Down Expand Up @@ -121,7 +124,7 @@ export class WinstonInstrumentation extends InstrumentationBase {
() => hook(span, record),
err => {
if (err) {
diag.error('winston instrumentation: error calling logHook', err);
this._diag.error('error calling logHook', err);
}
},
true
Expand Down

0 comments on commit adbe86d

Please sign in to comment.