Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nozik committed Aug 18, 2021
1 parent 3c3971a commit 20f2e44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -414,15 +414,20 @@ export class MongoDBInstrumentation extends InstrumentationBase<
this._config.dbStatementSerializer ||
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<string, unknown>) {
Expand Down
Expand Up @@ -255,6 +255,9 @@ describe('MongoDBInstrumentation', () => {
memoryExporter.reset();
create({
enhancedDatabaseReporting: true,
dbStatementSerializer: (commandObj: Record<string, unknown>) => {
return JSON.stringify(commandObj);
}
});
});

Expand Down

0 comments on commit 20f2e44

Please sign in to comment.