如何跟踪来自控制台应用程序的 MongoDB 请求
How to track MongoDB requests from a console application
我有一个用 C# 编写的控制台应用程序项目,我已将 Application Insights 添加到以下 NuGet 包中。
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:
var telemetryClient = new TelemetryClient();
telemetryClient.Context.User.Id = Environment.UserName;
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
一切正常,除了 AI 没有捕获任何发送到 Mongo 的请求,我可以看到请求发送到 'Application map' 中的 SQL 服务器,但没有迹象表明任何其他外部请求。有什么方法可以查看对 Mongo 发出的请求的遥测数据?
编辑——多亏了 Peter Bons,我得到了几乎以下内容,它就像一个魅力,让我能够区分成功和失败:
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);
我不熟悉 MongoDB,但据我所知,在 Application Insights 方面没有默认支持。但这并不意味着你不能这样做,它只会涉及更多代码。
同样,我不熟悉 MongoDB,但根据 http://www.mattburkedev.com/logging-queries-from-mongodb-c-number-driver/,built-in 支持记录生成的查询。现在,我们只需要将它连接到 Application Insights。
由于您已经知道如何使用 TelemetryClient
,我们可以使用 class 提供的自定义跟踪方法。有关可用的自定义跟踪方法,请参阅 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
class telemetryClient
是 thread-safe 所以你可以重复使用它。
现在,根据引用的博文,您应该可以执行如下操作:
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 的了解使其适应您的需求。
编辑:
如果还有一个 CommandCompletedEvent
或与 CommandStartedEvent
事件相反的类似事件,您可能应该跟踪那里的依赖关系,因为您应该能够计算(或简单读取)时间花费并可能获得成功指标的实际值。
我有一个用 C# 编写的控制台应用程序项目,我已将 Application Insights 添加到以下 NuGet 包中。
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:
var telemetryClient = new TelemetryClient();
telemetryClient.Context.User.Id = Environment.UserName;
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
telemetryClient.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
一切正常,除了 AI 没有捕获任何发送到 Mongo 的请求,我可以看到请求发送到 'Application map' 中的 SQL 服务器,但没有迹象表明任何其他外部请求。有什么方法可以查看对 Mongo 发出的请求的遥测数据?
编辑——多亏了 Peter Bons,我得到了几乎以下内容,它就像一个魅力,让我能够区分成功和失败:
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);
我不熟悉 MongoDB,但据我所知,在 Application Insights 方面没有默认支持。但这并不意味着你不能这样做,它只会涉及更多代码。
同样,我不熟悉 MongoDB,但根据 http://www.mattburkedev.com/logging-queries-from-mongodb-c-number-driver/,built-in 支持记录生成的查询。现在,我们只需要将它连接到 Application Insights。
由于您已经知道如何使用 TelemetryClient
,我们可以使用 class 提供的自定义跟踪方法。有关可用的自定义跟踪方法,请参阅 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
class telemetryClient
是 thread-safe 所以你可以重复使用它。
现在,根据引用的博文,您应该可以执行如下操作:
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 的了解使其适应您的需求。
编辑:
如果还有一个 CommandCompletedEvent
或与 CommandStartedEvent
事件相反的类似事件,您可能应该跟踪那里的依赖关系,因为您应该能够计算(或简单读取)时间花费并可能获得成功指标的实际值。