停止应用程序洞察,包括操作名称中的路径参数

时间:2017-06-22 06:24:21

标签: azure-application-insights

我们的ASP.NET MVC应用程序包含一些URI路径参数,例如:

  

https://example.com/api/query/14hes1017ceimgS2ESsIec

在Application Insights中,上面的URI变为操作名称

GET /api/query/14hes1017ceimgS2ESsIec

我们不希望这样的数百万个独特的操作;它只是一种为所有人提供服务的代码方法(见下文)。我们想在像

这样的操作名称下滚动它们
GET /api/query/{path}

以下是代码方法 - 我认为App Insights可以检测到URI包含查询参数......但它没有。

    [Route("api/query/{hash}")]
    public HttpResponseMessage Get(string hash)
    {
        ...

3 个答案:

答案 0 :(得分:5)

Application Insights没有检测到您的Operation Name的后缀是参数的原因是因为SDK没有查看您的代码,并且出于所有实际目的,这是一个有效的URI。
获得您想要的两个选项:

  1. 更改您的API以传递查询字符串中的参数(从操作名称中删除)
  2. 实施您自己的ITelemetryProcessor(可以找到详细说明here),并自行从操作名称中删除后缀哈希

答案 1 :(得分:2)

我用这个硬编码OperationNameMunger(使用these docs获取灵感)攻击它。

我在ApplicationInsights.config之后直接将其连接到OperationNameTelemetryInitializer

using System.Text.RegularExpressions;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;

namespace My.Namespace
{
    public class OperationNameMunger : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var existingOpName = telemetry.Context?.Operation?.Name;
            if (existingOpName == null)
                return;

            const string matchesInterestingOps = "^([A-Z]+ /api/query/)[^ ]+$";
            var match = Regex.Match(existingOpName, matchesInterestingOps);
            if (match.Success)
            {
                telemetry.Context.Operation.Name = match.Groups[1].Value + "{hash}";
            }
        }
    }
}

答案 2 :(得分:0)