2017-01-23 74 views
6

我有一个用C#编写的控制台应用程序项目,我已将Application Insights添加到以下NuGet包中。如何跟踪控制台应用程序中的MongoDB请求

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 

我已经在配置文件中配置我InstrumentationKey和我使用下面的代码发射了TelemetryClient在启动时:除了AI

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

一切工作以及没有捕捉任何发送到Mongo的请求,我都可以看到请求在“应用程序映射”中转到SQL服务器,但没有任何其他外部请求的迹象。有什么方法可以看到对Mongo提出的请求的遥测?

编辑 - 感谢彼得丝宝我结束了几乎它就像一个魅力,让我成功与失败之间的区别如下:

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); 

回答

3

我不熟悉的MongoDB,但据当涉及到Application Insights时,我可以告诉它没有默认的支持。但这并不意味着你不能这样做,它只会涉及更多的代码。

同样,我不熟悉MongoDB,但根据http://www.mattburkedev.com/logging-queries-from-mongodb-c-number-driver/,内置支持记录生成的查询。现在,我们只需要将其与Application Insights挂钩。

既然您已经知道如何使用TelemetryClient,我们可以使用该类提供的自定义跟踪方法。有关可用的自定义追踪方法,请参阅https://docs.microsoft.com/nl-nl/azure/application-insights/app-insights-api-custom-events-metrics

所有你需要做的是插入一些代码:

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 

telemetryClient是线程安全的,所以你可以重复使用它。现在

,根据所引用的博客帖子,你应该能够做这样的事情:

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 
     }); 
    } 
}); 

同样,我不熟悉的MongoDB,但我希望这是一个出发点为你的想象力如何使用你对MongoDB的了解来适应你的需求。

编辑:

如果有也作为反对CommandStartedEvent事件CommandCompletedEvent或类似的活动你应该跟踪依赖关系,因为你应该然后能计算出(或simpel阅读)所花的时间,也许获取成功指标的实际价值。

+3

然后如果你这样做...那么也许你应该为它创建一个github回购并与世界分享它? :) –

+0

伟大的答案彼得,这不是真的那种在GitHub中工作的东西,但我可能会把我在做什么的博客文章放在一起,我已经添加了代码,我最终与我的问题。 –

+0

现在有点晚了,但我确实已经开始写博客文章了:https://sequence7.net/2017/02/09/monitoring-mongodb-with-application-insights/ –