Type to search.

Set up MongoDB instrumentation

Serverless Observability

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

APM Agent .NET

The Elastic APM .NET agent enables you to trace the execution of operations in your .NET applications, sending performance metrics and errors to the Elastic APM server.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Package Supported versions
MongoDB.Driver ≥3.9.0 <4.0.0

For the full compatibility matrix including supported installation methods, refer to Data access technologies.

Note

MongoDB.Driver ≥3.7.0 natively emits OpenTelemetry spans. On runtimes where the agent's OpenTelemetry Bridge is supported, those spans are captured automatically, so no extra package is needed. On .NET Framework, the OpenTelemetry Bridge is not supported; use Elastic.Apm.MongoDb instead.

Elastic.Apm.MongoDb requires MongoDB.Driver ≥3.9.0. Earlier 3.x driver versions depend on Snappier and SharpCompress releases that have published security advisories, and the patched SharpCompress releases are not binary compatible with drivers older than 3.9.0. If your application references an older 3.x driver, update it to ≥3.9.0 before updating the agent.

This page assumes the core agent is already set up. If not, see Set up the APM .NET Agent first.

The Elastic.Apm.MongoDb NuGet package instruments the official MongoDB.Driver to capture MongoDB operations as APM spans, including the command name, target database, and duration.

If you are not already using Elastic.Apm.NetCoreAll, install Elastic.Apm.MongoDb. If you are using Elastic.Apm.NetCoreAll, you can skip this install step and continue with Step 2.

Shell
dotnet add package Elastic.Apm.MongoDb

Register the MongoDbEventSubscriber when creating your MongoClient:

C#
using MongoDB.Driver;
using Elastic.Apm.MongoDb;

var settings = MongoClientSettings.FromConnectionString(mongoConnectionString);
settings.ClusterConfigurator = builder => builder.Subscribe(new MongoDbEventSubscriber());
var mongoClient = new MongoClient(settings);

How you complete the setup depends on how you added the APM .NET Agent to your application:

Using Elastic.Apm.NetCoreAll The all-in-one package for ASP.NET Core. No further action is needed. MongoDB calls are captured automatically on every active transaction.

Using Elastic.Apm.MongoDb directly. Subscribe the diagnostics subscriber once at application startup:

C#
using Elastic.Apm;
using Elastic.Apm.MongoDb;

Agent.Subscribe(new MongoDbDiagnosticsSubscriber());

Make sure this is called only once. Calling it multiple times causes the agent to record duplicate spans for MongoDB operations.