How to track MongoDB requests from a console application How to track MongoDB requests from a console application azure azure

How to track MongoDB requests from a console application


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.