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

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

Open
drkreddy opened this issue Nov 3, 2024 · 1 comment
Labels

Comments

@drkreddy
Copy link

drkreddy commented Nov 3, 2024

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?

@gunzip
Copy link

gunzip commented Nov 4, 2024

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.

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

No branches or pull requests

3 participants