Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

vhatsura/elastic-apm-mongo

Repository files navigation

Superseeded by Elastic.Apm.MongoDb

MongoDB auto instrumentation for Elastic Apm

Build Status Quality Gate Status Coverage Maintainability Rating NuGet Badge

An auto instrumentation of MongoDB events for Elastic Apm solution.

Installation

Install-Package ElasticApm.MongoDB

Usage

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.

Roadmap

Contributing