向Node中的Azure Application Insights事件添加请求标头

时间:2019-04-10 18:17:47

标签: node.js azure azure-application-insights hapijs

Azure Application Insights节点模块默认情况下收集HTTP请求,但是,这些事件似乎不包括请求的标头。

如何最好在这些事件中包含标题?

https://github.com/Microsoft/ApplicationInsights-node.js

1 个答案:

答案 0 :(得分:0)

您可以添加自定义遥测处理器来执行此操作 https://github.com/Microsoft/ApplicationInsights-node.js/blob/44330896f58d4e6c2b6c4fec821430f7e1067138/README.md#preprocess-data-with-telemetry-processors

这是一个例子:

const logHTTPheaders = (envelope, context) => {
  const httpRequest = context['http.ServerRequest'];
  if (httpRequest && appInsights.Contracts.domainSupportsProperties(envelope.data.baseData)) {
    _.forOwn(httpRequest.headers, (headerValue, headerName) => {
      _.set(envelope, `data.baseData.properties.[header-${headerName}]`, headerValue);
    });
  }
  return true;
};

// Telemetry processor to record HTTP request header
appInsights.defaultClient.addTelemetryProcessor(logHTTPheaders);