将自定义维度添加到请求遥测 - Azure 函数 [英] Adding Custom Dimension to Request Telemetry - Azure functions

查看:18
本文介绍了将自定义维度添加到请求遥测 - Azure 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 v2.x 创建一个新的 Function 应用程序,并且我正在集成 Application Insights 以实现自动完成的请求日志记录,因为 Azure Function 现在已与 App Insights 集成(如文档中所述 链接).我需要做的是在 Application Insights 请求遥测的自定义维度中记录几个自定义字段.是否可以不使用自定义请求日志记录(使用 TrackRequest 方法)

I am creating a new Function app using v2.x and I am integrating Application Insights for request logging that is automatically being done as Azure Function is now integrated with App Insights (as mentioned in the documentation link). What I would need to do is log few custom fields in the custom dimensions in Application Insights Request Telemetry. Is it possible without using Custom Request logging (using TrackRequest method)

推荐答案

您应该在函数应用中使用 ITelemetry Initializer(它可以为指定的遥测添加自定义维度,如仅用于请求),请按照以下步骤操作:

You should use ITelemetry Initializer(which can add custom dimension to a specified telemetry like only for request) in function app, please follow the steps below:

1.在Visual Studio中,创建一个函数应用(在我的测试中,我创建了一个blob触发函数),并安装以下nuget包:

1.In Visual studio, create a function app(In my test, I create a blob triggerd function), and install the following nuget packages:

Microsoft.ApplicationInsights, version 2.10.0

Microsoft.NET.Sdk.Functions, version 1.0.29

2.然后在Function1.cs中编写如下代码:

2.Then in the Function1.cs, write code like below:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.IO;

[assembly: WebJobsStartup(typeof(FunctionApp21.MyStartup))]
namespace FunctionApp21
{
    public static class Function1
    {


        [FunctionName("Function1")]
        public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob
 Name:{name} 
 Size: {myBlob.Length} Bytes");
        }
    }

    internal class MyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {           

            //use telemetry is RequestTelemetry to make sure only add to request
            if (telemetry != null && telemetry is RequestTelemetry && !telemetry.Context.GlobalProperties.ContainsKey("my_custom_dimen22"))
            {
                telemetry.Context.GlobalProperties.Add("my_custom_dimen22", "Hello, this is custom dimension for request!!!");
            }
        }
    }

    public class MyStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();

        }
    }
}

3.将其发布到 azure,然后导航到 azure 门户 -> 发布的函数应用 -> 监控 -> 添加应用洞察.

3.Publish it to azure, then nav to azure portal -> the published function app -> Monitor -> Add an application insights.

4.从 azure 运行函数.并等待几分钟 -> 导航到应用洞察门户,检查遥测数据,您可以看到自定义维度仅用于请求遥测:

4.Run the function from azure. And wait for a few minutes -> nav to the application insights portal, check the telemetry data, and you can see the custom dimension is only added to request telemetry:

这篇关于将自定义维度添加到请求遥测 - Azure 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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