最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to get Application Insights operation id in javascript? - Stack Overflow

programmeradmin9浏览0评论

I have an Azure Function app written in javascript that has Application Insights integrated:

const AppInsights = require("applicationinsights");

AppInsights.setup(appInsightsInstrumentationKey);
AppInsights.defaultClient.context.tags[
    AppInsights.defaultClient.context.keys.cloudRole
        ] = "My back-end";
AppInsights.start();

The module version is 1.7.4.

I do need to access the current operation id to send it to my custom log for correlation with AI logs in Azure. In my functions I tried this:

var telemetry = appInsights.defaultClient;
var oid = telemetry.context.tags["ai.operation.id"]; // does not work
var oid = telemetry.context.operation.id; // does not work

Nevertheless, AI collects it somehow so I can see it in Azure portal:

How can I access operation_id value at run-time in my Azure functions?

I have an Azure Function app written in javascript that has Application Insights integrated:

const AppInsights = require("applicationinsights");

AppInsights.setup(appInsightsInstrumentationKey);
AppInsights.defaultClient.context.tags[
    AppInsights.defaultClient.context.keys.cloudRole
        ] = "My back-end";
AppInsights.start();

The module version is 1.7.4.

I do need to access the current operation id to send it to my custom log for correlation with AI logs in Azure. In my functions I tried this:

var telemetry = appInsights.defaultClient;
var oid = telemetry.context.tags["ai.operation.id"]; // does not work
var oid = telemetry.context.operation.id; // does not work

Nevertheless, AI collects it somehow so I can see it in Azure portal:

How can I access operation_id value at run-time in my Azure functions?

Share Improve this question asked May 22, 2020 at 15:59 UserControlUserControl 15.2k22 gold badges105 silver badges195 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2 +75

You can give it a try with this line of code:

//operation id
var s = client.context.keys["operationId"];

I found it via debug, the screenshot as below:

Not sure about Azure Functions, but in a node js application I am able to use:

const AppInsights = require("applicationinsights");
var context = AppInsights.getCorrelationContext();
var oid = context.operation.id;

Guaranteed to work in module version 1.8.0

Reading the documentation in nodejs client, I think you have to set the setDistributedTracingMode to appInsights.DistributedTracingModes.AI

That is what operationId is doing, it helps tracking distributed systems so you can correlate requests.

From the documentation page:

let appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>")
    .setDistributedTracingMode(appInsights.DistributedTracingModes.AI)
    .start();

The operationId is a value that is added on every request automatically by the Application Insights SDK but apparently in a later stage, when you try to access it is early too yet. That is the problem with black box solutions. But you can try to set it yourself in first place. Set telemetry.context.operation.id to be a unique id yourself and confirm if this is tracked by the ApplicationInsights properly.

According to documentation you can access or edit operation id by calling context.executionContext.invocationId.

发布评论

评论列表(0)

  1. 暂无评论