From 8362ac7a1f9f25cecb41a149e18c2dd0be51a1c8 Mon Sep 17 00:00:00 2001 From: Ran Nozik Date: Wed, 18 Aug 2021 12:51:53 +0300 Subject: [PATCH] minor fix --- .../src/instrumentation.ts | 29 +++++++++++-------- .../src/types.ts | 7 ++--- .../test/mongodb.test.ts | 3 ++ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index ecc4619050..ad1780d996 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -160,7 +160,7 @@ export class MongoDBInstrumentation extends InstrumentationBase< ns, server, // eslint-disable-next-line @typescript-eslint/no-explicit-any - (ops[0] as any) + ops[0] as any ); const patchedCallback = instrumentation._patchEnd(span, resultHandler); // handle when options is the callback to send the correct number of args @@ -412,20 +412,25 @@ export class MongoDBInstrumentation extends InstrumentationBase< const commandObj = command.query ?? command.q ?? command; const dbStatementSerializer: DbStatementSerializer = this._config.dbStatementSerializer || - this.defaultDbStatementSerializer.bind(this); + this._defaultDbStatementSerializer.bind(this); - safeExecuteInTheMiddle(() => { - const query = dbStatementSerializer(commandObj); - span.setAttribute(SemanticAttributes.DB_STATEMENT, JSON.stringify(query)); - }, err => { - if (err) { - diag.error('Error running dbStatementSerializer hook', err); - } - }, - true) + if (typeof dbStatementSerializer === 'function') { + safeExecuteInTheMiddle( + () => { + const query = dbStatementSerializer(commandObj); + span.setAttribute(SemanticAttributes.DB_STATEMENT, query); + }, + err => { + if (err) { + this._diag.error('Error running dbStatementSerializer hook', err); + } + }, + true + ); + } } - private defaultDbStatementSerializer (commandObj: Record) { + private _defaultDbStatementSerializer(commandObj: Record) { const enhancedDbReporting = !!this._config?.enhancedDatabaseReporting; const resultObj = enhancedDbReporting ? commandObj diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts index b074407d44..fd54361449 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/types.ts @@ -23,14 +23,11 @@ export interface MongoDBInstrumentationExecutionResponseHook { /** * Function that can be used to serialize db.statement tag - * @param cmdName - The name of the command (eg. set, get, mset) - * @param cmdArgs - Array of arguments passed to the command + * @param cmd - MongoDB command object * * @returns serialized string that will be used as the db.statement attribute. */ - export type DbStatementSerializer = ( - cmd: Record -) => string; +export type DbStatementSerializer = (cmd: Record) => string; export interface MongoDBInstrumentationConfig extends InstrumentationConfig { /** diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts index 2b5068b089..fe1bd7dc9b 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb.test.ts @@ -255,6 +255,9 @@ describe('MongoDBInstrumentation', () => { memoryExporter.reset(); create({ enhancedDatabaseReporting: true, + dbStatementSerializer: (commandObj: Record) => { + return JSON.stringify(commandObj); + }, }); });