You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const{ NodeTracerProvider }=require("@opentelemetry/node");const{ registerInstrumentations }=require("@opentelemetry/instrumentation");const{
ExpressInstrumentation,}=require("@opentelemetry/instrumentation-express");const{ JaegerExporter }=require("@opentelemetry/exporter-jaeger");const{ BatchSpanProcessor }=require("@opentelemetry/tracing");const{ Resource }=require("@opentelemetry/resources");const{
SemanticResourceAttributes,}=require("@opentelemetry/semantic-conventions");const{
RedisInstrumentation,}=require("@opentelemetry/instrumentation-redis");functionconfigureOpenTelemetry(serviceName){// Create a tracer provider and register the Express instrumentationconstprovider=newNodeTracerProvider({resource: newResource({[SemanticResourceAttributes.SERVICE_NAME]: serviceName,// Add other resource attributes as needed}),});provider.register();// Configure and register Jaeger exporterconstexporter=newJaegerExporter({serviceName: serviceName,agentHost: "localhost",// Change this to your Jaeger hostagentPort: 16686,// Change this to your Jaeger port});// Use BatchSpanProcessorconstspanProcessor=newBatchSpanProcessor(exporter);provider.addSpanProcessor(spanProcessor);// Register the Express instrumentationregisterInstrumentations({tracerProvider: provider,instrumentations: [newExpressInstrumentation(),newRedisInstrumentation(),],});returnprovider;}module.exports=configureOpenTelemetry;
in the node app add following initiators
constconfigureOpenTelemetry=require("./tracing");const{ trace, context, propagation }=require("@opentelemetry/api");consttracerProvider=configureOpenTelemetry("service-name");// add service name hereapp.use((req,res,next)=>{consttracer=tracerProvider.getTracer("express-tracer");constspan=tracer.startSpan("parent-span");// Add custom attributes or log additional information if neededspan.setAttribute("team name","SUST Define Coders");// Pass the span to the request object for use in the route handlercontext.with(trace.setSpan(context.active(),span),()=>{next();});});
in the api itself do following
constparentSpan=trace.getSpan(context.active());// can add data to parent span use try catchif(parentSpan){parentSpan.setAttribute("user.id",user.id);parentSpan.setAttribute("user.name",user.name);}
in the axios call do the following for context propagation
try{constrespose_data=awaitcontext.with(trace.setSpan(context.active(),parentSpan),async()=>{constcarrier={};propagation.inject(context.active(),carrier);// replace axios call herereturnaxios.get("http://localhost:5000/validateuser",{headers: carrier,});});console.log("Validation response:",respose_data.data);// Log or use the response as needed// add what you want as responseres.json(user);}catch(error){if(parentSpan){parentSpan.recordException(error);}res.status(500).send(error.message);}finally{if(parentSpan){parentSpan.end();}}
add shutdown code
// Gracefully shut down the OpenTelemetry SDK and the serverconstgracefulShutdown=()=>{server.close(()=>{console.log("Server stopped");sdk.shutdown().then(()=>console.log("Tracing terminated")).catch((error)=>console.error("Error shutting down tracing",error)).finally(()=>process.exit(0));});};// Listen for termination signalsprocess.on("SIGTERM",gracefulShutdown);process.on("SIGINT",gracefulShutdown);