2017-06-22 24 views
4

我们的ASP.NET MVC应用程序的路径参数包括一些URI路径参数,如:停止应用见解,包括操作名称

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

在应用洞察,这上面的URI成为操作名称

GET /api/query/14hes1017ceimgS2ESsIec 

我们不希望百万个独特的操作像这样;它只是一种代码方法,可以满足他们所有的需求(见下文)。我们要卷起来,下操作名称一样

GET /api/query/{path} 

下面是代码的方法 - 我觉得应用程式深入分析可以发现,该URI包含查询参数......但事实并非如此。

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

这可能帮助:https://stackoverflow.com/questions/34482869/wcf-service-operation-name –

+0

的确如此@PavelChuchuva :)特别是HttpContextExtension.GetRequestTelemetry ......我一直想那之前...... – Iain

回答

4

原因申请见解没有检测到您的操作名称的后缀是一个参数是因为SDK不看你的代码,而对于所有的实际目的,这是一个有效的URI。
两个选项得到你想要的东西:

  1. 更改API来传递参数的查询字符串(即剥离出来的操作名称的)
  2. 实现自己ITelemetryProcessor(详细的解释可以发现here),并从操作名称自己
0

我砍死它这个硬编码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}"; 
      } 
     } 
    } 
}