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?
4 Answers
Reset to default 2 +75You 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
.