TelemetryClient 在 Application Insights 中产生不一致的结果

TelemetryClient produces inconsistent results in Application Insights

我尝试在刷新和不刷新的情况下跟踪自定义指标。但是,指标仅间歇性地显示在 "Custom" 部分下的 Application Insights 中。第一个问题:是否需要在每次 "TrackMetric(metric)" 调用后 运行 "flush()" 才能将遥测数据发送到 Application Insights?第二:为什么会出现这种间歇性行为?我一次只写一个指标,所以我并没有用数千个单独的调用使 Application Insights 超载。这是我的代码(这是来自一个简单的控制台应用程序):

    public class Program
{
    public static void Main(string[] args)
    {
        var telemetryClient = new TelemetryClient()
        {
            Context = { InstrumentationKey = "{{hidden instrumentation key}}" }
        };
        var metric = new MetricTelemetry
        {
            Name = "ImsWithContextMetric2",
            Sum = 42.0
        };
        telemetryClient.TrackMetric(metric);
        telemetryClient.Flush();
    }
}

我在 Application Insights 中也遇到了这种奇怪的行为,其中我添加的自定义指标显示在 "Unavailable/deprecated Metrics" 部分下。在 "Custom" 部分下弹出了一个我什至没有添加的名为 "Process CPU (all cores)" 的指标。知道为什么会发生这种奇怪的行为吗?:

Is it required to run "flush()" after every single "TrackMetric(metric)" call in order for the telemetry to be sent to Application Insights?

由于您正在使用控制台应用程序向 Application Insights 发送事件,这可能是短暂的,因此每隔一段时间调用 .Flush() 绝对是一个好习惯。 SDK 使用 InMemoryChannel 发送遥测数据,并使用内存队列批量发送。所以调用.Flush()非常重要,这样就可以强推数据了。一个好的做法可能是在事件发生后增加一点等待时间:

telemetryClient.Flush();
Thread.Sleep(1000);

更多阅读:Flushing data, Ensure you don't lose telemetry

However, the metrics only intermittently shows up in Application Insights under the "Custom" section. Why is there this intermittent behavior? I'm only writing one metric at a time, so it's not as if I'm overloading Application Insights with thousands of separate calls.

有时 Azure 门户中显示的指标会有延迟。它也可能长达几分钟。但是如果你设置正确,你没有超过 throttling limit, and adaptive sampling 被禁用,那么没有理由认为遥测应该是间歇性的。但是,如果您仍然觉得有什么不对劲,请启动 fiddler 跟踪(确保您是从非浏览器会话中捕获的)并检查是否正在向 dc.services.visualstudio.com 发出呼叫。确保响应为 200 OK 并且服务器是否接受了这些项目。

I'm also getting this strange behavior in Application Insights in which the custom metric I add shows up under a "Unavailable/deprecated Metrics" section.

您使用的是什么版本的 SDK?我刚刚尝试了相同的场景,自定义指标显示正确。

And a metric that I didn't even add called "Process CPU (all cores)" pops up under the "Custom" section.

"Process CPU" 是一个 performance counter,用于跟踪 CPU 利用率。我相信只有当应用程序在 IIS 或 Azure 下 运行 时,SDK 才能跟踪这些计数器。它可能是在您创建 Application Insights 资源时在内部添加的。您可以忽略它,因为它没有要绘制图表的数据。

希望对您有所帮助!