Skip to content

Commit

Permalink
end span on commandFailed
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Oct 18, 2024
1 parent 1e2e11e commit 1339608
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/tracing/src/traceDatabaseCalls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { trace, context } from '@opentelemetry/api';
import { trace, context, SpanStatusCode } from '@opentelemetry/api';
import type { MongoClient } from 'mongodb';

import { isTracingEnabled } from '.';
Expand Down Expand Up @@ -45,5 +45,21 @@ export const initDatabaseTracing = (client: MongoClient) => {
span.end();
});

client.on('commandFailed', (event) => DurationStart.delete(event.requestId));
client.on('commandFailed', (event) => {
if (!DurationStart.has(event.requestId)) {
return;
}

const { span } = DurationStart.get(event.requestId);

DurationStart.delete(event.requestId);

span.recordException(event.failure);
span.setStatus({
code: SpanStatusCode.ERROR,
message: event.failure.message,
});

span.end();
});
};

0 comments on commit 1339608

Please sign in to comment.