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.AddOpenTelemetry(builder => {
builder
// Configure exporters
.UseZipkin()
// Configure adapters
.UseRequestAdapter()
.UseDependencyAdapter()
.AddMongoDBAdapter(); // Adds MongoDB OTel support
});
By default, the command text is not logged. To change this, configure the options:
services.AddOpenTelemetry(builder => {
builder
// Configure exporters
.UseZipkin()
// Configure adapters
.UseRequestAdapter()
.UseDependencyAdapter()
.AddMongoDBAdapter(opt => opt.CaptureCommandText = true); // Adds MongoDB OTel support
});
This package supports the latest released OpenTelemetry beta package on NuGet.