How to track MongoDB requests from a console appli

2020-08-26 11:07发布

问题:

I have a Console Application project written in C# which I've added Application Insights to with the following NuGet packages.

Microsoft.ApplicationInsights
Microsoft.ApplicationInsights.Agent.Intercept
Microsoft.ApplicationInsights.DependencyCollector
Microsoft.ApplicationInsights.NLogTarget
Microsoft.ApplicationInsights.PerfCounterCollector
Microsoft.ApplicationInsights.Web
Microsoft.ApplicationInsights.WindowsServer
Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel

I've configured my InstrumentationKey in the config file and I'm firing up a TelemetryClient on startup using the with the following code:

var telemetryClient = new TelemetryClient();
telemetryClient.Context.User.Id = Environment.UserName;
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();

Everything is working well except AI is not capturing any requests that get sent to Mongo, I can see requests going off to SQL server in the 'Application map' but no sign of any other external requests. Is there any way that I can see telemetry of requests made to Mongo?

EDIT - Thanks to Peter Bons I ended up with pretty much the following which works like a charm and allows me to distinguish between success and failure:

var telemetryClient = new TelemetryClient();
var connectionString = connectionStringSettings.ConnectionString;
var mongoUrl = new MongoUrl(connectionString);
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);

mongoClientSettings.ClusterConfigurator = clusterConfigurator =>
{
    clusterConfigurator.Subscribe<CommandSucceededEvent>(e =>
    {
        telemetryClient.TrackDependency("MongoDB", e.CommandName, DateTime.Now.Subtract(e.Duration), e.Duration, true);
    });

    clusterConfigurator.Subscribe<CommandFailedEvent>(e =>
    {
        telemetryClient.TrackDependency("MongoDB", $"{e.CommandName} - {e.ToString()}", DateTime.Now.Subtract(e.Duration), e.Duration, false);
    });
};

var mongoClient = new MongoClient(mongoClientSettings);

回答1:

I am not familiar with MongoDB but as far as I can tell there is no default support for it when it comes to Application Insights. But that does not mean you cannot do this, it will just involve some more code.

Again, I am not familiar with MongoDB but according to http://www.mattburkedev.com/logging-queries-from-mongodb-c-number-driver/ there is built-in support for logging the generated queries. Now, we only need to hook this up to Application Insights.

Since you already know how to use the TelemetryClient we can use the custom tracking methods provided by that class. See https://docs.microsoft.com/nl-nl/azure/application-insights/app-insights-api-custom-events-metrics for the available custom tracking methods.

All you need to do is to insert some code like this:

telemetryClient.TrackDependency(
    "MongoDB",               // The name of the dependency
    query,                   // Text of the query
    DateTime.Now,            // Time that query is executed
    TimeSpan.FromSeconds(0), // Time taken to execute query
    true);                   // Indicates success

The class telemetryClient is thread-safe so you can reuse it.

Now, according to the referenced blogpost you should be able to do something like this:

var client = new MongoClient(new MongoClientSettings()
{
    Server = new MongoServerAddress("localhost"),
    ClusterConfigurator = cb =>
    {
        cb.Subscribe<CommandStartedEvent>(e =>
        {
            telemetryClient.TrackDependency(
                "MongoDB",               // The name of the dependency
                e.Command.ToJson()       // Text of the query
                DateTime.Now,            // Time that query is executed
                TimeSpan.FromSeconds(0), // Time taken to execute query
                true);                   // Indicates success
        });
    }
});

Again, I am not familiar with MongoDB but I hope this is a starting point for your imagination on how to adapt it to your needs using your knowledge of MongoDB.

EDIT:

If there is also a CommandCompletedEvent or similar event as opposed to the CommandStartedEvent event you should probably track the dependency there because you should then be able to calculate (or simpel read) the time spent and maybe get the actual value for the success indicator.