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 8362ac7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 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 @@ -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<string, unknown>) {
private _defaultDbStatementSerializer(commandObj: Record<string, unknown>) {
const enhancedDbReporting = !!this._config?.enhancedDatabaseReporting;
const resultObj = enhancedDbReporting
? commandObj
Expand Down
Expand Up @@ -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, unknown>
) => string;
export type DbStatementSerializer = (cmd: Record<string, unknown>) => string;

export interface MongoDBInstrumentationConfig extends InstrumentationConfig {
/**
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 8362ac7

Please sign in to comment.