拦截Azure功能主机关闭:刷新应用程序见解TelemetryClient [英] Intercept Azure Function Host Shutdown: Flush Application Insights TelemetryClient

查看:115
本文介绍了拦截Azure功能主机关闭:刷新应用程序见解TelemetryClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure Function进行一些操作:通常,我尝试将现有的webjob迁移到Azure Functions,现在是时候将Application Insights集成到我的其中一项功能中了.

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.

所以基本上我只需要一个TelemetryClient实例,但这假设我能够在应用程序停止时刷新内存缓冲区.

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.

我使用了TimerTrigger,但这只是出于测试目的.

I've used a TimerTrigger but it was just for testing purpose.

我已经引用了 Microsoft.ApplicationInsights nuget包(来自此SO帖子)和我的run.csx文件看起来像这样:

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...

  • 我有一个静态TelemetryClient以确保我将重用同一实例.
  • 我尝试使用WebJobsShutdownWatcher来检测主机何时停止,以便可以刷新TelemetryClient.
  • 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.

为模拟应用程序关闭,我在基础Web应用程序中创建了一个"test"应用程序设置,并在希望主机重新启动时对其进行了修改:

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:

不幸的是,这行不通...我在应用数据分析信息中心中未看到任何名称为"TelemetryClientFlush"的事件:

Unfortunately this does not work... I did not see any event with name "TelemetryClientFlush" from the app insights dashboard:

所以我现在想知道当azure函数主机停止时是否有任何方法可以拦截?

So I am now wondering if there is any way to intercept when the azure function host is stopping ?

推荐答案

除了Mathew描述的内容外,您可能还想尝试一下我们将根据要求传递的取消令牌.

In addition to what Mathew described, you may want to play around with the cancellation token we'll pass if requested.

如果在函数中添加CancellationToken类型的参数,我们将传递一个令牌,该令牌在主机在正常情况下关闭时会发出信号.使用它可能使您关闭所需的东西:

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功能主机关闭:刷新应用程序见解TelemetryClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆