This repo include the package:
The MongoDB.Driver.Core.Extensions.OpenTelemetry
package provides adapters to OpenTelemetry.
To use MongoDB.Driver.Core.Extensions.OpenTelemetry
, you need to configure your MongoClientSettings
to add this MongoDB event subscriber through the MongoDB.Driver.Core.Extensions.DiagnosticSources package:
var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
var mongoClient = new MongoClient(clientSettings);
That event subscriber exposes Activity events via a DiagnosticListener under the root activity name, MongoDB.Driver.Core.Events.Command
. To subscribe, you may use the DiagnosticListener.AllListeners
observable.
Once you've configured your MongoDB client to expose diagnostics events as above, you can configure OpenTelemetry (typically through the OpenTelemetry.Extensions.Hosting package).
services.AddOpenTelemetryTracing(builder => {
builder
// Configure exporters
.AddZipkinExporter()
// Configure adapters
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddMongoDBInstrumentation(); // Adds MongoDB OTel support
});
This extension method only adds a source with the appropriate name:
public static TracerProviderBuilder AddMongoDBInstrumentation(this TracerProviderBuilder builder)
{
return builder.AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources");
}
This package supports OpenTelemetry version 1.0.1
.