Skip to content

jbogard/MongoDB.Driver.Core.Extensions.DiagnosticSources

Repository files navigation

MongoDB.Driver.Core.Extensions.DiagnosticSources

CI NuGet NuGet MyGet (dev)

This package supports MongoDB C# Driver versions 3.0.0 and above.

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, or use the DiagnosticsActivityEventSubscriber.ActivitySourceName. Use this name in any ActivityListener-based listeners.

All the available OpenTelemetry semantic tags are set.

Usage with OpenTelemetry

To add the listener for the OpenTelemetry SDK, add your listener in the OpenTelemetry tracing configuration, usually configured with the OpenTelemetry.Extensions.Hosting package:

builder.Services.AddOpenTelemetry()
    // Metrics, logging etc.
    .WithTracing(tracing =>
    {
        // Other tracing configuration

        tracing
            // Other sources (ASP.NET Core, HttpClient etc.)
            .AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources");
    });

This can also be added with the OpenTelemetry SDK directly.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 11