Superseeded by Elastic.Apm.MongoDb
An auto instrumentation of MongoDB events for Elastic Apm solution.
Install-Package ElasticApm.MongoDB
MongoDB.Driver uses own event notification approach instead of commonly used System.Diagnostics.DiagnosticSource. By this reason, before Elastic.Apm
configuration we need to configure MongoClient
. It can be done with ClusterConfigurator
action in MongoClientSettings
:
var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
// OR (FromConnectionString is available in MongoDB.Driver 2.7+)
var settings = MongoClientSettings.FromConnectionString(mongoConnectionString);
settings.ClusterConfigurator = builder => builder.Subscribe(new MongoEventSubscriber());
var mongoClient = new MongoClient(settings);
MongoEventSubscriber
allows to spread command events from MongoDB.Driver in application via DiagnosticSource
.
Now we can configure Elastic.Apm
agent:
Agent.Subscribe(new MongoDiagnosticsSubscriber());
or in case of ASP.NET Core application:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseElasticApm(Configuration, new MongoDiagnosticsSubscriber());
//Rest of the Configure() method...
}
}
NOTE: don't forget to include other subscribers.