问题描述
我正在使用 Azure Functions:主要是尝试将现有的网络作业迁移到 Azure Functions,现在是时候将 Application Insights 集成到我的一个函数中了.
所以基本上我只需要 TelemetryClient
的一个实例,但这假设我能够在应用程序停止时刷新内存缓冲区.
我使用了 TimerTrigger,但它只是用于测试目的.
我已经引用了
很遗憾,这不起作用...我没有从应用洞察仪表板中看到任何名为 "TelemetryClientFlush"
的事件:
所以我现在想知道当azure函数主机停止时是否有任何方法可以拦截?
除了 Mathew 所描述的之外,您可能还想玩弄我们将在请求时传递的取消令牌.
如果您向函数添加 CancellationToken
类型参数,我们将传入一个令牌,该令牌将在主机正常关闭时发出信号.使用它可能会让你接近你需要什么:
使用系统;使用 System.Threading;使用 Microsoft.ApplicationInsights;公共静态只读 TelemetryClient TelemetryClient = new TelemetryClient(){ InstrumentationKey = "MyInstrumentationKey" };公共静态布尔优先=真;公共静态无效运行(TimerInfo myTimer,TraceWriter 日志,CancellationToken 令牌){如果(第一){token.Register(() =>{TelemetryClient.TrackEvent("TelemetryClientFlush");TelemetryClient.Flush();});第一=假;}TelemetryClient.TrackEvent("AzureFunctionTriggered");log.Verbose($"C# Timer 触发函数执行于:{DateTime.Now}");}
I am playing a little bit with Azure Function: Mostly I try to migrate an existing webjob to Azure Functions and now it is time for me to integrate Application Insights in one of my function.
So basically I only need one instance of the TelemetryClient
but this assumes that I am able to flush the in-memory buffer when the application stops.
I've used a TimerTrigger but it was just for testing purpose.
I've referenced the Microsoft.ApplicationInsights nuget package (from this SO post) and my run.csx
file looks like that:
using System;
using Microsoft.ApplicationInsights;
using Microsoft.Azure.WebJobs;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
MyTimerJob.TelemetryClient.TrackEvent("AzureFunctionTriggered");
log.Verbose($"C# Timer trigger function executed at: {DateTime.Now}");
}
public static class MyTimerJob
{
public static readonly TelemetryClient TelemetryClient;
static MyTimerJob(){
TelemetryClient = new TelemetryClient()
{ InstrumentationKey = "MyInstrumentationKey" };
// When it shutdowns, we flush the telemty client.
new WebJobsShutdownWatcher().Token.Register(() =>
{
TelemetryClient.TrackEvent("TelemetryClientFlush");
TelemetryClient.Flush();
});
}
}
This implementation is a bit tricky...
- I have a static
TelemetryClient
to ensure that I am going to reuse the same instance. - I tried using the
WebJobsShutdownWatcher
to detect when the host is stopping so that I can flush the TelemetryClient.
To simulate the application shutdown, I've created a "test"
app setting in the underlying web app and I modify it when I want the host to restart:
Unfortunately this does not work... I did not see any event with name "TelemetryClientFlush"
from the app insights dashboard:
So I am now wondering if there is any way to intercept when the azure function host is stopping ?
In addition to what Mathew described, you may want to play around with the cancellation token we'll pass if requested.
If you add a CancellationToken
type argument to your function, we'll pass in a token that will be signaled when the host shuts down under normal circumstances. Using that may get you close to what you need:
using System;
using System.Threading;
using Microsoft.ApplicationInsights;
public static readonly TelemetryClient TelemetryClient = new TelemetryClient(){ InstrumentationKey = "MyInstrumentationKey" };
public static bool first = true;
public static void Run(TimerInfo myTimer, TraceWriter log, CancellationToken token)
{
if(first){
token.Register(() =>
{
TelemetryClient.TrackEvent("TelemetryClientFlush");
TelemetryClient.Flush();
});
first = false;
}
TelemetryClient.TrackEvent("AzureFunctionTriggered");
log.Verbose($"C# Timer trigger function executed at: {DateTime.Now}");
}
这篇关于拦截 Azure Function 主机关闭:刷新 Application Insights TelemetryClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!