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

问题描述 投票:0回答:1

Azure Application Insights Node模块默认收集HTTP请求,但是,这些事件似乎不包含请求的标头。

我应该如何在这些事件中最好地包含标题?

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

node.js azure azure-application-insights hapijs
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.