azure - 如何将 customDimensions 中的更多条目添加到 Azure Function 中的 Application Insights Telemetry

标签 azure azure-functions azure-application-insights

我正在尝试将 Azure App Insights 与 Azure Function App (HttpTriggered) 集成。我想在请求表的“customDimensions”对象中添加我自己的键和值。现在它只显示以下内容:

查询时

requests
| where iKey == "449470fb-****" and id == "5e17e23e-****" 

我明白了:

LogLevel: Information
Category: Host.Results
FullName: Functions.FTAID
StartTime: 2017-07-14T14:24:10.9410000Z
param__context: ****
HttpMethod: POST
param__req: Method: POST, Uri: ****
Succeeded: True
TriggerReason: This function was programmatically called via the host APIs.
EndTime: 2017-07-14T14:24:11.6080000Z

我想添加更多关键值,例如:

EnvironmentName: Development
ServiceLine: Business

基于this答案,我实现了 ITelemetryInitializer 接口(interface),如下所示:

public class CustomTelemetry : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        if (requestTelemetry == null) return;
        requestTelemetry.Context.Properties.Add("EnvironmentName", "Development");

    }
}

Azure Function App 的 run.csx 代码如下所示:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ExecutionContext context, TraceWriter log)
{
    // Initialize the App Insights Telemetry
    TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);
    TelemetryConfiguration.Active.TelemetryInitializers.Add(new CustomTelemetry());
    TelemetryClient telemetry = new TelemetryClient();

    var jsonBody = await req.Content.ReadAsStringAsync();
    GetIoItemID obj = new GetIoItemID();
    JArray output = obj.GetResponseJson(jsonBody, log, telemetry);

    var response = req.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(output.ToString(), System.Text.Encoding.UTF8, "application/json");
    return response;
}

但是这不起作用......

最佳答案

我相信,由于您在本例中自己创建了 TelemetryClient,因此您无需担心遥测初始化程序,您只需这样做

var telemetry = new TelemetryClient();
telemetry.Context.Properties["EnvironmentName"] = "Development";

直接,并且该遥测客户端的该实例发送的所有内容都将设置这些属性。

如果您无法控制谁创建遥测客户端并且想要接触在任何地方创建的遥测的每个项目,您是否需要遥测初始值设定项?

我不知道 TelemetryClient 实例如何在 azure 函数的下游使用,所以我并不完全肯定。

编辑:来自 Azure Functions 的帖子,内容如下:

We’ll be working hard to get Application Insights ready for production workloads. We’re also listening for any feedback you have. Please file it on our GitHub. We’ll be adding some new features like better sampling controls and automatic dependency tracking soon. We hope you’ll give it a try and start to gain more insight into how your Functions are behaving. You can read more about how it works at https://aka.ms/func-ai

来自 func-ai 链接的示例有以下几点:

1) 它预先静态创建遥测客户端一次(而不是在每次调用函数时)

private static TelemetryClient telemetry = new TelemetryClient();
private static string key = TelemetryConfiguration.Active.InstrumentationKey = System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process);

以及它正在执行的函数内部:

telemetry.Context.Operation.Id = context.InvocationId.ToString();

正确地与您可能使用遥测客户端创建的事件进行关联,因此您可能也想这样做。

2) 看来您创建的遥测客户端可以使用,但他们创建自己的遥测客户端并向那里发送数据,因此您在遥测客户端上下文中触摸的任何内容都不会被看到 azure 本身具有功能。

所以,对我来说,这引导我去做一些你可以尝试的事情:

在您的类中添加一个静态构造函数,并在该静态构造函数中执行您在上面执行的遥测初始化程序操作。这可能会在 azure 函数开始创建其请求并调用您的方法之前将您的遥测初始值设定项添加到上下文中?

如果这不起作用,您可能需要在他们的 GitHub 上发帖或向文章中列出的人员发送电子邮件以了解有关如何执行此操作的更多详细信息?

关于azure - 如何将 customDimensions 中的更多条目添加到 Azure Function 中的 Application Insights Telemetry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45105941/

相关文章:

c# - 自定义指标遥测不会出现在 Application Insights 的指标浏览器中

azure - 如何解决 AZFD0005 Azure Function App 错误

c# - Azure Functions 单元测试错误 (TimerTrigger.HttpFunction)

c# - 将 Application Insights 与 ILoggerFactory 结合使用

azure - ASP.NET Core API - 在 Azure 应用服务上获取 404,但在本地主机上工作正常

azure - 是否建议将已部署的 v2 azure 功能升级到 v3,还是创建新资源并从头开始部署到它们更好?

azure - 应用程序洞察 : Logs not streaming when deployed to App Service But streams in local debug environment

Azure 搜索 - 正则表达式搜索

azure - 如何在 Azure 逻辑应用程序中设置 secret

azure - 从现有的备份 Azure 网站创建新的 Azure 网站