microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
320 stars 138 forks source link

Initializing two Application Insights Clients #1166

Closed eric-gonzalez-tfs closed 1 month ago

eric-gonzalez-tfs commented 1 year ago

I am using applicationinsights@3.0.0-beta.6.

The project I'm working on requires certain data to ALWAYS be sent, and others to be sent on a sampling ratio.

When I try to initialize a second client with a 100% sampling ratio and most autocollection config off, I get a duplicate registration error. Screenshot 2023-06-27 at 1 28 02 PM

What is the best way to create a second client that sends 100% of the data without initializing anything else under the hood?

eric-gonzalez-tfs commented 11 months ago

Hi Team, any insight here would be much appreciated.

eric-gonzalez-tfs commented 9 months ago

Hi @hectorhdzg, any advice here?

hectorhdzg commented 9 months ago

@eric-gonzalez-tfs sorry for the delay, yes this a topic we had been discussing last few weeks, because of OpenTelemetry architecture having multiple clients independent on each other is not achievable anymore, this can be done in a different way depending on your scenario, you basically call useAzureMonitor method to initialize all OpenTelemetry components, then you can create a custom Span Processor sending to Azure Monitor as well using different sampling ratio, I can provide some code sample if it works.

eric-gonzalez-tfs commented 9 months ago

@eric-gonzalez-tfs sorry for the delay, yes this a topic we had been discussing last few weeks, because of OpenTelemetry architecture having multiple clients independent on each other is not achievable anymore, this can be done in a different way depending on your scenario, you basically call useAzureMonitor method to initialize all OpenTelemetry components, then you can create a custom Span Processor sending to Azure Monitor as well using different sampling ratio, I can provide some code sample if it works.

@hectorhdzg Thank you for the response - that would be great.

hectorhdzg commented 9 months ago

You can do something like this

import { useAzureMonitor, AzureMonitorOpenTelemetryOptions } from "applicationinsights";
import { AzureMonitorTraceExporter, ApplicationInsightsSampler } from "@azure/monitor-opentelemetry-exporter";
import { BatchSpanProcessor} from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider, NodeTracerConfig } from "@opentelemetry/sdk-trace-node";

// Initialize OpenTelemetry, all data will be sampled in
useAzureMonitor({
    samplingRatio: 1
});

// Create another Tracer Provider using different sampling mechanism
const aiSampler = new ApplicationInsightsSampler(0.5);
const tracerConfig: NodeTracerConfig = {
    sampler: aiSampler,
};
const tracerProvider = new NodeTracerProvider(tracerConfig);
// Add SpanProcessor sending with different sampling ratio
let azureMonitorExporter = new AzureMonitorTraceExporter({
    connectionString: "your_connectionString",
})
tracerProvider.addSpanProcessor(new BatchSpanProcessor(azureMonitorExporter));

const span= tracerProvider.getTracer("tesTracer").startSpan("testSpan");
span.end();
JacksonWeber commented 1 month ago

@eric-gonzalez-tfs Do you have any further questions on this issue? Or did the above answer your question?

eric-gonzalez-tfs commented 1 month ago

Haven't had a chance to test this myself, but the code snipped should suffice. Thanks!