在 Application Insights SDK 中篩選及前置處理遙測

在從 SDK 傳送遙測資料之前,您可以撰寫程式碼來篩選、修改或擴充遙測資料。 處理包括從標準遙測模組 (如 HTTP 要求收集和相依性收集) 的資料。

  • 篩選可以先修改或捨棄遙測,再藉由實作 ITelemetryProcessor 從 SDK 傳送遙測。 例如,您可以從傀儡程式中排除要求來減少遙測量。 與取樣不同,您可以完全控制傳送或捨棄的內容,但它會影響以彙總記錄為基礎的任何計量。 視您捨棄項目的方式而定,您也可能會喪失在相關項目之間瀏覽的能力。

  • 透過實作 ITelemetryInitializer,對從您的應用程式中所傳送的任何遙測資料新增或修改屬性。 例如,您可以新增計算好的值,或是用來在入口網站中篩選資料的版本號碼。

  • 取樣 可減少遙測的量而不會影響統計資料。 其可將相關資料點寶持放在一起,因此您診斷問題時,能夠在資料點之間瀏覽。 在入口網站中將乘以總計數,以補償取樣。

注意

SDK API 可用來傳送自訂事件和計量。

在開始之前:

篩選

這項技術可讓您直接地控制包含在遙測串流中或排除於遙測串流外的內容。 篩選可用來捨棄遙測項目,不讓其傳送至 Application Insights。 您可以將篩選與取樣搭配使用或分開使用。

若要篩選遙測,請撰寫遙測處理器,並向 TelemetryConfiguration 進行註冊。 所有遙測都會通過您的處理器。 您可以選擇從資料流中捨棄遙測,或將遙測提供給鏈結中的下一個處理器。 來自標準模組 (例如 HTTP 要求收集器和相依性收集器) 的遙測,以及您自行追蹤的遙測都包括在內。 例如,您可以篩選掉有關來自傀儡程式要求或成功的相依性呼叫的遙測。

警告

使用處理器來篩選 SDK 傳送過來的遙測可能會曲解您在入口網站中看到的統計資料,並且難以追蹤相關的項目。

請考慮改用 取樣

建立遙測處理器

C#

  1. 若要建立篩選,請實作 ITelemetryProcessor

    遙測處理器會建構一連串的處理。 當您具現化遙測處理器時,所獲得的是鏈結中下一個處理器的參考。 遙測資料點傳遞至處理序方法時,其會完成其工作並接著呼叫 (或不呼叫) 鏈結中的下一個遙測處理器。

    using Microsoft.ApplicationInsights.Channel;
    using Microsoft.ApplicationInsights.Extensibility;
    using Microsoft.ApplicationInsights.DataContracts;
    
    public class SuccessfulDependencyFilter : ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }
    
        // next will point to the next TelemetryProcessor in the chain.
        public SuccessfulDependencyFilter(ITelemetryProcessor next)
        {
            this.Next = next;
        }
    
        public void Process(ITelemetry item)
        {
            // To filter out an item, return without calling the next processor.
            if (!OKtoSend(item)) { return; }
    
            this.Next.Process(item);
        }
    
        // Example: replace with your own criteria.
        private bool OKtoSend (ITelemetry item)
        {
            var dependency = item as DependencyTelemetry;
            if (dependency == null) return true;
    
            return dependency.Success != true;
        }
    }
    
  2. 新增您的處理器。

ASP.NET 應用程式

在 ApplicationInsights.config 中插入此程式碼片段:

<TelemetryProcessors>
  <Add Type="WebApplication9.SuccessfulDependencyFilter, WebApplication9">
     <!-- Set public property -->
     <MyParamFromConfigFile>2-beta</MyParamFromConfigFile>
  </Add>
</TelemetryProcessors>

您可以在類別中提供公開具名屬性,以從 .config 檔案傳遞字串值。

警告

仔細地將 .config 檔案中的類型名稱和任何屬性名稱與程式碼中的類別和屬性名稱做比對。 如果 .config 檔案參考不存在的類型或屬性,SDK 可能無法傳送任何遙測,而且不會產生任何訊息。

或者,您也可以在程式碼中初始化篩選條件。 在適當的初始化類別中 (例如,Global.asax.cs 中的 AppStart) 將處理器插入至鏈結中:

var builder = TelemetryConfiguration.Active.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
builder.Use((next) => new SuccessfulDependencyFilter(next));

// If you have more processors:
builder.Use((next) => new AnotherProcessor(next));

builder.Build();

在這個點之後建立的遙測用戶端將會使用您的處理器。

ASP.NET Core/背景工作角色服務應用程式

注意

若為 ASP.NET Core 應用程式,使用 ApplicationInsights.configTelemetryConfiguration.Active 新增處理器屬於無效行為,如果您使用 Microsoft.ApplicationInsights.WorkerService SDK 的話,這同樣是無效行為。

若為使用 ASP.NET CoreWorkerService 所撰寫的應用程式,則可使用 IServiceCollection 上的 AddApplicationInsightsTelemetryProcessor 擴充方法來新增遙測處理器,如下所示。 Startup.cs 類別的 ConfigureServices 方法會呼叫這個方法。

    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        services.AddApplicationInsightsTelemetry();
        services.AddApplicationInsightsTelemetryProcessor<SuccessfulDependencyFilter>();

        // If you have more processors:
        services.AddApplicationInsightsTelemetryProcessor<AnotherProcessor>();
    }

若要註冊需要 ASP.NET Core 中參數的遙測處理器,請建立實作 ITelemetryProcessorFactory 的自訂類別。 使用 Create 方法中所需的參數呼叫建構函式 ,然後使用 AddSingleton< ITelemetryProcessorFactory, MyTelemetryProcessorFactory>()

範例篩選器

綜合要求

篩選出 bot 和 Web 測試。 雖然計量瀏覽器可讓您篩選掉綜合來源,此選項會藉由在 SDK 本身篩選其以降低流量和擷取大小。

public void Process(ITelemetry item)
{
  if (!string.IsNullOrEmpty(item.Context.Operation.SyntheticSource)) {return;}

  // Send everything else:
  this.Next.Process(item);
}

驗證失敗

篩選出具有 "401" 回應的要求。

public void Process(ITelemetry item)
{
    var request = item as RequestTelemetry;

    if (request != null &&
    request.ResponseCode.Equals("401", StringComparison.OrdinalIgnoreCase))
    {
        // To filter out an item, return without calling the next processor.
        return;
    }

    // Send everything else
    this.Next.Process(item);
}

篩選出快速遠端相依性呼叫

如果您只想要診斷速度很慢的呼叫,請篩選掉速度很快的呼叫。

注意

此篩選會曲解您在入口網站上看到的統計資料。

public void Process(ITelemetry item)
{
    var request = item as DependencyTelemetry;

    if (request != null && request.Duration.TotalMilliseconds < 100)
    {
        return;
    }
    this.Next.Process(item);
}

診斷相依性問題

這篇部落格文章 描述可自動傳送定期的 Ping 給相依項目,藉以診斷相依性問題的專案。

Java

若要深入了解遙測處理器及其在 JAVA 中的實作,請參閱 JAVA 遙測處理器文件

JavaScript Web 應用程式

使用 ITelemetryInitializer 進行篩選

  1. 建立遙測初始設定式回呼函式。 回呼函式會採用 ITelemetryItem 作為參數,這是會進行處理的事件。 從這個回呼傳回 false 會導致遙測項目遭到篩掉。

    var filteringFunction = (envelope) => {
      if (envelope.data.someField === 'tobefilteredout') {
          return false;
      }
    
      return true;
    };
    
  2. 新增遙測初始設定式回呼:

    appInsights.addTelemetryInitializer(filteringFunction);
    

新增/修改屬性︰ITelemetryInitializer

使用遙測初始設定式,利用其他資訊來擴充您的遙測,或覆寫由標準遙測模組所設定的遙測屬性。

例如,網頁套件的 Application Insights 會收集有關 HTTP 要求的遙測。 根據預設,其會將所有含 >=400 回應碼的要求標記為失敗。 但如果您想將 400 視為成功,您可以提供設定 success 屬性的遙測初始設定式。

如果您提供遙測初始設定式,則會在呼叫任何的 Track*() 方法時呼叫。 此初始設定式包括由標準遙測模組呼叫的 Track() 方法。 依照慣例,這些模組不會設定任何已由初始設定式設定的屬性。 系統會在呼叫遙測處理器之前,先呼叫遙測初始設定式。 因此,處理器可以看到初始設定式所完成的任何擴充。

定義您的初始設定式

C#

using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace MvcWebRole.Telemetry
{
  /*
   * Custom TelemetryInitializer that overrides the default SDK
   * behavior of treating response codes >= 400 as failed requests
   *
   */
  public class MyTelemetryInitializer : ITelemetryInitializer
  {
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;
        int code;
        bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
        if (!parsed) return;
        if (code >= 400 && code < 500)
        {
            // If we set the Success property, the SDK won't change it:
            requestTelemetry.Success = true;

            // Allow us to filter these requests in the portal:
            requestTelemetry.Properties["Overridden400s"] = "true";
        }
        // else leave the SDK to set the Success property
    }
  }
}

ASP.NET 應用程式:載入初始設定式

在 ApplicationInsights.config 中:

<ApplicationInsights>
  <TelemetryInitializers>
    <!-- Fully qualified type name, assembly name: -->
    <Add Type="MvcWebRole.Telemetry.MyTelemetryInitializer, MvcWebRole"/>
    ...
  </TelemetryInitializers>
</ApplicationInsights>

或者,您也可以在程式碼 (如 Global.aspx.cs) 中具現化初始設定式:

protected void Application_Start()
{
    // ...
    TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());
}

詳細查看此範例

ASP.NET Core/背景工作角色服務應用程式:載入初始設定式

注意

若為 ASP.NET Core 應用程式,使用 ApplicationInsights.configTelemetryConfiguration.Active 新增初始設定式屬於無效行為,如果您使用 Microsoft.ApplicationInsights.WorkerService SDK 的話,這同樣是無效行為。

如果是使用 ASP.NET CoreWorkerService 所撰寫的應用程式,則可藉由將新的遙測初始設定式新增至相依性插入容器來新增遙測初始設定式,如下所示。 在 Startup.ConfigureServices 方法中完成此步驟。

 using Microsoft.ApplicationInsights.Extensibility;
 using CustomInitializer.Telemetry;
 public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
}

JavaScript 遙測初始設定式

視需要插入 JavaScript 遙測初始設定式。 如需 Application Insights JavaScript SDK 遙測初始設定式的詳細資訊,請參閱遙測初始設定式

JavaScript (Web) SDK 載入器指令碼設定中新增 onInit 回呼函式,以插入遙測初始設定式:

<script type="text/javascript">
!(function (cfg){function e(){cfg.onInit&&cfg.onInit(i)}var S,u,D,t,n,i,C=window,x=document,w=C.location,I="script",b="ingestionendpoint",E="disableExceptionTracking",A="ai.device.";"instrumentationKey"[S="toLowerCase"](),u="crossOrigin",D="POST",t="appInsightsSDK",n=cfg.name||"appInsights",(cfg.name||C[t])&&(C[t]=n),i=C[n]||function(l){var d=!1,g=!1,f={initialize:!0,queue:[],sv:"7",version:2,config:l};function m(e,t){var n={},i="Browser";function a(e){e=""+e;return 1===e.length?"0"+e:e}return n[A+"id"]=i[S](),n[A+"type"]=i,n["ai.operation.name"]=w&&w.pathname||"_unknown_",n["ai.internal.sdkVersion"]="javascript:snippet_"+(f.sv||f.version),{time:(i=new Date).getUTCFullYear()+"-"+a(1+i.getUTCMonth())+"-"+a(i.getUTCDate())+"T"+a(i.getUTCHours())+":"+a(i.getUTCMinutes())+":"+a(i.getUTCSeconds())+"."+(i.getUTCMilliseconds()/1e3).toFixed(3).slice(2,5)+"Z",iKey:e,name:"Microsoft.ApplicationInsights."+e.replace(/-/g,"")+"."+t,sampleRate:100,tags:n,data:{baseData:{ver:2}},ver:4,seq:"1",aiDataContract:undefined}}var h=-1,v=0,y=["js.monitor.azure.com","js.cdn.applicationinsights.io","js.cdn.monitor.azure.com","js0.cdn.applicationinsights.io","js0.cdn.monitor.azure.com","js2.cdn.applicationinsights.io","js2.cdn.monitor.azure.com","az416426.vo.msecnd.net"],k=l.url||cfg.src;if(k){if((n=navigator)&&(~(n=(n.userAgent||"").toLowerCase()).indexOf("msie")||~n.indexOf("trident/"))&&~k.indexOf("ai.3")&&(k=k.replace(/(\/)(ai\.3\.)([^\d]*)$/,function(e,t,n){return t+"ai.2"+n})),!1!==cfg.cr)for(var e=0;e<y.length;e++)if(0<k.indexOf(y[e])){h=e;break}var i=function(e){var a,t,n,i,o,r,s,c,p,u;f.queue=[],g||(0<=h&&v+1<y.length?(a=(h+v+1)%y.length,T(k.replace(/^(.*\/\/)([\w\.]*)(\/.*)$/,function(e,t,n,i){return t+y[a]+i})),v+=1):(d=g=!0,o=k,c=(p=function(){var e,t={},n=l.connectionString;if(n)for(var i=n.split(";"),a=0;a<i.length;a++){var o=i[a].split("=");2===o.length&&(t[o[0][S]()]=o[1])}return t[b]||(e=(n=t.endpointsuffix)?t.location:null,t[b]="https://"+(e?e+".":"")+"dc."+(n||"services.visualstudio.com")),t}()).instrumentationkey||l.instrumentationKey||"",p=(p=p[b])?p+"/v2/track":l.endpointUrl,(u=[]).push((t="SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details)",n=o,r=p,(s=(i=m(c,"Exception")).data).baseType="ExceptionData",s.baseData.exceptions=[{typeName:"SDKLoadFailed",message:t.replace(/\./g,"-"),hasFullStack:!1,stack:t+"\nSnippet failed to load ["+n+"] -- Telemetry is disabled\nHelp Link: https://go.microsoft.com/fwlink/?linkid=2128109\nHost: "+(w&&w.pathname||"_unknown_")+"\nEndpoint: "+r,parsedStack:[]}],i)),u.push((s=o,t=p,(r=(n=m(c,"Message")).data).baseType="MessageData",(i=r.baseData).message='AI (Internal): 99 message:"'+("SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details) ("+s+")").replace(/\"/g,"")+'"',i.properties={endpoint:t},n)),o=u,c=p,JSON&&((r=C.fetch)&&!cfg.useXhr?r(c,{method:D,body:JSON.stringify(o),mode:"cors"}):XMLHttpRequest&&((s=new XMLHttpRequest).open(D,c),s.setRequestHeader("Content-type","application/json"),s.send(JSON.stringify(o))))))},a=function(e,t){g||setTimeout(function(){!t&&f.core||i()},500),d=!1},T=function(e){var n=x.createElement(I),e=(n.src=e,cfg[u]);return!e&&""!==e||"undefined"==n[u]||(n[u]=e),n.onload=a,n.onerror=i,n.onreadystatechange=function(e,t){"loaded"!==n.readyState&&"complete"!==n.readyState||a(0,t)},cfg.ld&&cfg.ld<0?x.getElementsByTagName("head")[0].appendChild(n):setTimeout(function(){x.getElementsByTagName(I)[0].parentNode.appendChild(n)},cfg.ld||0),n};T(k)}try{f.cookie=x.cookie}catch(p){}function t(e){for(;e.length;)!function(t){f[t]=function(){var e=arguments;d||f.queue.push(function(){f[t].apply(f,e)})}}(e.pop())}var r,s,n="track",o="TrackPage",c="TrackEvent",n=(t([n+"Event",n+"PageView",n+"Exception",n+"Trace",n+"DependencyData",n+"Metric",n+"PageViewPerformance","start"+o,"stop"+o,"start"+c,"stop"+c,"addTelemetryInitializer","setAuthenticatedUserContext","clearAuthenticatedUserContext","flush"]),f.SeverityLevel={Verbose:0,Information:1,Warning:2,Error:3,Critical:4},(l.extensionConfig||{}).ApplicationInsightsAnalytics||{});return!0!==l[E]&&!0!==n[E]&&(t(["_"+(r="onerror")]),s=C[r],C[r]=function(e,t,n,i,a){var o=s&&s(e,t,n,i,a);return!0!==o&&f["_"+r]({message:e,url:t,lineNumber:n,columnNumber:i,error:a,evt:C.event}),o},l.autoExceptionInstrumented=!0),f}(cfg.cfg),(C[n]=i).queue&&0===i.queue.length?(i.queue.push(e),i.trackPageView({})):e();})({
src: "https://js.monitor.azure.com/scripts/b/ai.3.gbl.min.js",
crossOrigin: "anonymous",
onInit: function (sdk) {
  sdk.addTelemetryInitializer(function (envelope) {
    envelope.data = envelope.data || {};
    envelope.data.someField = 'This item passed through my telemetry initializer';
  });
}, // Once the application insights instance has loaded and initialized this method will be called
cfg: { // Application Insights Configuration
    connectionString: "YOUR_CONNECTION_STRING"
}});
</script>

如需遙測項目上可用的非自訂屬性摘要,請參閱 Application Insights 匯出資料模型

您可以依需要加入多個初始設定式。 系統會依其新增順序來進行呼叫。

OpenCensus Python 遙測處理器

OpenCensus Python 中的遙測處理器就是回呼函式,系統會呼叫其來處理遙測再將遙測匯出。 回呼函式必須以信封資料類型作為其參數。 若要篩選掉不要匯出的遙測,請確定回呼函式傳回的是 False。 您可以在 GitHub 上的信封中查看 Azure 監視器資料類型的結構描述。

注意

您可以藉由變更 tags 欄位中的 ai.cloud.role 屬性來修改 cloud_RoleName

def callback_function(envelope):
    envelope.tags['ai.cloud.role'] = 'new_role_name'
# Example for log exporter
import logging

from opencensus.ext.azure.log_exporter import AzureLogHandler

logger = logging.getLogger(__name__)

# Callback function to append '_hello' to each log message telemetry
def callback_function(envelope):
    envelope.data.baseData.message += '_hello'
    return True

handler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation_key-here>')
handler.add_telemetry_processor(callback_function)
logger.addHandler(handler)
logger.warning('Hello, World!')
# Example for trace exporter
import requests

from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import config_integration
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer

config_integration.trace_integrations(['requests'])

# Callback function to add os_type: linux to span properties
def callback_function(envelope):
    envelope.data.baseData.properties['os_type'] = 'linux'
    return True

exporter = AzureExporter(
    connection_string='InstrumentationKey=<your-instrumentation-key-here>'
)
exporter.add_telemetry_processor(callback_function)
tracer = Tracer(exporter=exporter, sampler=ProbabilitySampler(1.0))
with tracer.span(name='parent'):
response = requests.get(url='https://www.wikipedia.org/wiki/Rabbit')
# Example for metrics exporter
import time

from opencensus.ext.azure import metrics_exporter
from opencensus.stats import aggregation as aggregation_module
from opencensus.stats import measure as measure_module
from opencensus.stats import stats as stats_module
from opencensus.stats import view as view_module
from opencensus.tags import tag_map as tag_map_module

stats = stats_module.stats
view_manager = stats.view_manager
stats_recorder = stats.stats_recorder

CARROTS_MEASURE = measure_module.MeasureInt("carrots",
                                            "number of carrots",
                                            "carrots")
CARROTS_VIEW = view_module.View("carrots_view",
                                "number of carrots",
                                [],
                                CARROTS_MEASURE,
                                aggregation_module.CountAggregation())

# Callback function to only export the metric if value is greater than 0
def callback_function(envelope):
    return envelope.data.baseData.metrics[0].value > 0

def main():
    # Enable metrics
    # Set the interval in seconds in which you want to send metrics
    exporter = metrics_exporter.new_metrics_exporter(connection_string='InstrumentationKey=<your-instrumentation-key-here>')
    exporter.add_telemetry_processor(callback_function)
    view_manager.register_exporter(exporter)

    view_manager.register_view(CARROTS_VIEW)
    mmap = stats_recorder.new_measurement_map()
    tmap = tag_map_module.TagMap()

    mmap.measure_int_put(CARROTS_MEASURE, 1000)
    mmap.record(tmap)
    # Default export interval is every 15.0s
    # Your application should run for at least this amount
    # of time so the exporter will meet this interval
    # Sleep can fulfill this
    time.sleep(60)

    print("Done recording metrics")

if __name__ == "__main__":
    main()

您可以依照需要新增任意數量的處理器。 系統會依其新增順序來進行呼叫。 如果某個處理器擲回例外狀況,這並不會影響之後的處理器。

範例 TelemetryInitializers

新增自訂屬性

下列範例初始設定式會將自訂屬性新增至每個所追蹤的遙測。

public void Initialize(ITelemetry item)
{
  var itemProperties = item as ISupportProperties;
  if(itemProperties != null && !itemProperties.Properties.ContainsKey("customProp"))
    {
        itemProperties.Properties["customProp"] = "customValue";
    }
}

新增雲端角色名稱

下列範例初始設定式會將雲端角色名稱設定為每個所追蹤的遙測。

public void Initialize(ITelemetry telemetry)
{
    if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
    {
        telemetry.Context.Cloud.RoleName = "MyCloudRoleName";
    }
}

控制用於地理位置對應的用戶端 IP 位址

在遙測擷取期間,下列初始設定式範例會設定將用於地理位置對應的用戶端 IP,而不是用戶端通訊端 IP 位址。

public void Initialize(ITelemetry telemetry)
{
    var request = telemetry as RequestTelemetry;
    if (request == null) return true;
    request.Context.Location.Ip = "{client ip address}"; // Could utilize System.Web.HttpContext.Current.Request.UserHostAddress;   
    return true;
}

ITelemetryProcessor 和 ITelemetryInitializer

遙測處理器與遙測初始設定式之間有何差異?

  • 其用途有部分重疊。 兩者都可以用來新增或修改遙測的屬性,但建議您使用初始設定式來進行該作業。
  • 遙測初始設定式一律會比遙測處理器更早執行。
  • 遙測初始設定式可以多次呼叫。 依照慣例,其不會設定任何已經設定的屬性。
  • 遙測處理器可讓您完全取代或捨棄遙測項目。
  • 所有已註冊的遙測初始設定式都會針對每個遙測項目而受系統呼叫。 若為遙測處理器,SDK 保證會呼叫第一個遙測處理器。 是否會呼叫其餘處理器則由前面的遙測處理器決定。
  • 使用遙測初始設定式,利用更多屬性擴充遙測或覆寫現有的遙測。 使用遙測處理器來篩選掉遙測。

注意

JavaScript 只有遙測初始設定式,其可以使用 ITelemetryInitializer 來篩選掉事件

針對 ApplicationInsights.config 進行疑難排解

  • 確認完整格式的類型名稱和組件名稱均正確。
  • 確認 applicationinsights.config 檔案在您的輸出目錄中,並且包含任何最近的變更。

Azure 監視器遙測資料類型參考

參考文件

SDK 程式碼

下一步