ApplicationInsights-node.js icon indicating copy to clipboard operation
ApplicationInsights-node.js copied to clipboard

Bring CustomPropertiesImpl back in v3 so that correlationContext's customProperities can be used again

Open zhiyuanliang-ms opened this issue 1 year ago • 2 comments

In v2, the below pattern works well for me:

appInsights.setup(connectionString)
    .setAutoCollectRequests(true)
    .setSendLiveMetrics(true)
    .start();

const attachMyTag = (envelope) => {
  const context = appInsights.getCorrelationContext();
  if (context) {
    envelope.data.baseData.properties["MyTag"] = context.customProperties.getProperty("MyTag");
  }
}

appInsights.defaultClient.addTelemetryProcessor(attachTargetingId);

const express = require('express');
const app = express();
const port = 5000;

app.get('/', (req, res) => {
  const context = appInsights.getCorrelationContext();
  context.customProperties.setProperty("MyTag", "123")

  appInsights.defaultClient.trackEvent({name: "TestEvent"})
  res.send(`hello`);
});

The correlation context is per-request-scoped, I used correlationContext.customProperties to store some information which I want to be tagged to all telemetry emitted during the request. This is very helpful if I want to tag something into the request telemetry automatically emitted by app insights.

But in v3, CorrelationContext's custom properties are stubbed out source code It is impossible for me to achieve the same thing. Or should I use span.attribute instead?

zhiyuanliang-ms avatar Sep 05 '24 07:09 zhiyuanliang-ms

@zhiyuanliang-ms you should be able to achieve the same thing using Span attributes like you mentioned, Application Insights v3 was created for easy migration to OpenTelemetry for simple scenarios, in this case I recommend to take OpenTelemetry approach, logs will not include the attributes and you will need to add them using the LogRecord attributes as well.

hectorhdzg avatar Sep 16 '24 18:09 hectorhdzg

@zhiyuanliang-ms you should be able to achieve the same thing using Span attributes like you mentioned, Application Insights v3 was created for easy migration to OpenTelemetry for simple scenarios, in this case I recommend to take OpenTelemetry approach, logs will not include the attributes and you will need to add them using the LogRecord attributes as well.

I have same problem as reporter. Could you, please, elaborate more how to use OpenTelemetry approach? Do I have to entirely ditch out AI v3 and replace it with Azure Monitor OT library(-ies)?

Hence, I find it difficult to master AI v3, when quite a few methods are still callable, but they are actually doing nothing, and no single error or warning thrown. ;-/

ricardasjak avatar Oct 11 '24 15:10 ricardasjak

This Issue will be closed in 30 days. Please remove the "Stale" label or comment to avoid closure with no action.

github-actions[bot] avatar Aug 08 '25 07:08 github-actions[bot]