Skip to content

jbogard/MongoDB.Driver.Core.Extensions.DiagnosticSources

Repository files navigation

MongoDB.Driver.Core.Extensions.DiagnosticSources

CI NuGet NuGet MyGet (dev)

Usage

This repo includes the package:

The MongoDB.Driver.Core.Extensions.DiagnosticSources package extends the core MongoDB C# driver to expose telemetry information via System.Diagnostics.

To use MongoDB.Driver.Core.Extensions.DiagnosticSources, you need to configure your MongoClientSettings to add this MongoDB event subscriber:

var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
var mongoClient = new MongoClient(clientSettings);

To capture the command text as part of the activity:

var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
var options = new InstrumentationOptions { CaptureCommandText = true };
clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber(options));
var mongoClient = new MongoClient(clientSettings);

To filter activities by collection name:

var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
var options = new InstrumentationOptions { ShouldStartActivity = @event => !"collectionToIgnore".Equals(@event.GetCollectionName()) };
clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber(options));
var mongoClient = new MongoClient(clientSettings);

This package exposes an ActivitySource with a Name the same as the assembly, MongoDB.Driver.Core.Extensions.DiagnosticSources. Use this name in any ActivityListener-based listeners.

All the available OpenTelemetry semantic tags are set.

This package supports MongoDB C# Driver versions 2.17.1 to 3.0.