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

can we use 3.X.X with opentelemetry instrumentation libraries like http,express,graphql

Open drkreddy opened this issue 1 year ago • 1 comments

Hi I am trying to understand how we can use below instrumentation libraryies with applicaitoninsights-3.x.x version

@opentelemetry/instrumentation-http @opentelemetry/instrumentation-express @opentelemetry/instrumentation-graphql

I can am able find only examples like below only which uses NodeSDK

// telemetry.js
import { NodeSDK } from '@opentelemetry/sdk-node';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
import { AzureMonitorTraceExporter } from '@azure/monitor-opentelemetry-exporter';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';

// Set up the NodeSDK with the necessary resources and instrumentations
const sdk = new NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'your-service-name',
  }),
  spanProcessor: new SimpleSpanProcessor(new AzureMonitorTraceExporter()), // Replace or add exporters as needed
  instrumentations: [
    new HttpInstrumentation(),
    new ExpressInstrumentation(),
    new GraphQLInstrumentation(),
  ],
});

// Initialize the SDK
sdk.start().then(() => {
  console.log('OpenTelemetry initialized');
}).catch((error) => {
  console.log('Error initializing OpenTelemetry', error);

>  #});/

How can we register the instrumentation without using NodeSDK only only using Appinsigshts?

drkreddy avatar Nov 03 '24 12:11 drkreddy

We use it like this

import { AzureFunctionsInstrumentation } from "@azure/functions-opentelemetry-instrumentation";
import { metrics, trace } from "@opentelemetry/api";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";
import * as ai from "applicationinsights";

ai.setup(process.env["AppInsightsConnectionString"])
  .setUseDiskRetryCaching(true)
  .start();

registerInstrumentations({
  instrumentations: [
    new UndiciInstrumentation(),
    new AzureFunctionsInstrumentation(),
  ],
  meterProvider: metrics.getMeterProvider(),
  tracerProvider: trace.getTracerProvider(),
});

export default ai;

remember to call registerInstrumentations after you call ai.start() otherwise trace.getTracerProvider() will return a Noop Trace Provider resulting in Non recording Traces.

gunzip avatar Nov 04 '24 14:11 gunzip

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 Sep 01 '25 07:09 github-actions[bot]