How to track MongoDB requests from a console application

I have a Console Application project written in C # that I added to Application Insights 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 configured my InstrumentationKey in the configuration file and I start TelemetryClient at startup using 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 works well, except that the AI โ€‹โ€‹does not capture any queries sent to Mongo, I can see the queries sent to the SQL server in the "Application Map", but there are no signs of any other external requests. Is there any way to see telemetry of requests made in Mongo?

EDIT - Thanks to Peter Bons, I ended up pretty much the following, which works like a charm and allows me to distinguish 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); 
+6
source share
1 answer

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 this does not mean that you cannot do this, it will just require a few more codes.

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 generated requests. Now we only need to associate this with Application Insights.

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

All you have to do is insert 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 TelemetryClient class is thread safe, so you can reuse it.

Now, according to the link blog, you can 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 the 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, unlike the CommandStartedEvent event, you should probably track the dependency there, because then you will have to calculate (or read simpel) the time spent and possibly get the actual value for the success indicator.

+3
source

Source: https://habr.com/ru/post/1014319/


All Articles