You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@opentelemetry/instrumentation-mongodb

Package Overview
Dependencies
Maintainers
3
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@opentelemetry/instrumentation-mongodb

OpenTelemetry instrumentation for `mongodb` database client for MongoDB

0.56.0
latest
Source
npmnpm
Version published
Weekly downloads
5.8M
1.42%
Maintainers
3
Weekly downloads
 
Created

What is @opentelemetry/instrumentation-mongodb?

@opentelemetry/instrumentation-mongodb is an npm package that provides automatic tracing for MongoDB operations using the OpenTelemetry framework. It helps in monitoring and collecting performance metrics and distributed traces for MongoDB operations, which can be useful for debugging and optimizing database interactions.

What are @opentelemetry/instrumentation-mongodb's main functionalities?

Automatic Tracing

This feature allows you to automatically trace MongoDB operations. The code sample shows how to set up the NodeTracerProvider and register the MongoDB instrumentation to start collecting traces.

const { NodeTracerProvider } = require('@opentelemetry/node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
  instrumentations: [
    new MongoDBInstrumentation(),
  ],
});

Custom Span Attributes

This feature allows you to add custom attributes to the spans created for MongoDB operations. The code sample demonstrates how to use the `responseHook` to add the MongoDB operation result as a span attribute.

const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');

const mongoInstrumentation = new MongoDBInstrumentation({
  responseHook: (span, result) => {
    span.setAttribute('mongodb.result', JSON.stringify(result));
  },
});

Error Handling

This feature allows you to handle errors in MongoDB operations and set the span status accordingly. The code sample shows how to use the `responseHook` to check if the result is an error and set the span status.

const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');

const mongoInstrumentation = new MongoDBInstrumentation({
  responseHook: (span, result) => {
    if (result instanceof Error) {
      span.setStatus({ code: 2, message: result.message });
    }
  },
});

Other packages similar to @opentelemetry/instrumentation-mongodb

Keywords

mongodb

FAQs

Package last updated on 09 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.