6

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:

enter image description here

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

UserControl
  • 14,766
  • 20
  • 100
  • 187

4 Answers4

2

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:

enter image description here

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • In this case `s` will contain "ai.operation.id" - the key to lookup operation id value and in my case `.context.tags["ai.operation.id"]` is not defined :( – UserControl May 27 '20 at 11:25
  • It might help to access the tag via the key value like `client.context.tags[client.context.keys.operationId]` – Willem Mar 10 '22 at 14:35
1

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

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.

Stelios Giakoumidis
  • 2,153
  • 1
  • 7
  • 19
  • I have it enabled explicitly and it doesn't work. I'm more interested in the code on how to access the operationid value. – UserControl May 26 '20 at 04:39
  • @UserControl 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. Try to set it yourself! Set telemetry.context.operation.id to be a unique id yourself and confirm if this is tracked by the ApplicationInsights properly. – Stelios Giakoumidis May 27 '20 at 06:26
  • @UserControl since you are using an azure function app I suppose that this is the beginning of the operation, so it is the place where operation_id is autogenerated. If you had a web application things would be easier to access it. But try the suggestion above, it should work – Stelios Giakoumidis May 27 '20 at 06:28
  • @Stellios, I tried it and unfortunately it doesn't work. – UserControl May 27 '20 at 11:20
  • @UserControl I am out of ideas if that did not work. Good luck! – Stelios Giakoumidis May 27 '20 at 11:31
0

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

Julass
  • 91
  • 5