Add MongoDB explain insights to OpenTelemetry traces
Standard OpenTelemetry database spans show query execution time, but fail to report explain plans, execution stats, or index recommendations. TraceMole hooks into OpenTelemetry spans to capture explain details asynchronously.
Tracing Architecture
TraceMole client SDKs wrap standard, open-source `@opentelemetry/api` layers, pushing spans cleanly to any OTLP ingestion system or hosted TraceMole platform.
Explain logic runs strictly out-of-band using non-blocking child threads. We prevent telemetry aggregation tasks from delaying database client operations.
Connect MongoDB explain stats to API traces
Standard application traces capture execution paths and latencies of your code block operations but miss what happens inside the database engine. By wrapping the MongoDB driver commands and registering a custom trace listener, TraceMole injects span context directly into MongoDB command monitor cycles. This allows us to map execution stages and scan plans back to specific HTTP routes and API transactions.
Why MongoDB queries become slow
MongoDB query execution slows down when data structures lack index support, forcing the database engine to load every record from disk to identify match filters. When unindexed queries execute concurrently, they consume database memory and block connection queues. Correlating API traces with explain statistics allows developers to pinpoint slow queries and instantly find the exact files causing the latency.
OpenTelemetry SDK Config
TraceMole uses OpenTelemetry instrumentation to monitor driver queries transparently. Setup is straightforward:
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({
url: "http://localhost:4318/v1/traces",
headers: { "x-api-key": process.env.TRACE_MOLE_API_KEY },
}),
instrumentations: [
new MongoDBInstrumentation({ requireParentSpan: false }),
]
});
sdk.start();