Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializing two Application Insights Clients #1166

Open
eric-gonzalez-tfs opened this issue Jun 27, 2023 · 5 comments
Open

Initializing two Application Insights Clients #1166

eric-gonzalez-tfs opened this issue Jun 27, 2023 · 5 comments
Assignees

Comments

@eric-gonzalez-tfs
Copy link

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
Copy link
Author

Hi Team, any insight here would be much appreciated.

@hectorhdzg hectorhdzg self-assigned this Jul 31, 2023
@eric-gonzalez-tfs
Copy link
Author

Hi @hectorhdzg, any advice here?

@hectorhdzg
Copy link
Member

@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
Copy link
Author

@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
Copy link
Member

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants