microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
325 stars 139 forks source link

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

Open zhiyuanliang-ms opened 2 months ago

zhiyuanliang-ms commented 2 months ago

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?

hectorhdzg commented 2 months ago

@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.

ricardasjak commented 1 month ago

@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. ;-/