Installation
To start, add the OpenTelemetry SDK and necessary instrumentation packages to your Node.js project. You can install them with npm:Initializing OpenTelemetry
Next, initialize the OpenTelemetry SDK in your application. This involves setting up a Tracer Provider (which manages tracers and spans) and OTLP exporter to send the traces to TrueFoundry Backend.Automatic HTTP Instrumentation
Now that the OpenTelemetry SDK is set up, let’s instrument the Express server to automatically trace incoming requests.getNodeAutoInstrumentations()
instruments express apps along with other apps.
At this point, all incoming HTTP requests are being traced automatically.
Adding Attributes to Spans
Automatic instrumentation captures basic request information, but you can add custom data to your traces using attributes. Attributes are key-value pairs that provide additional context about your operations. For example, in order service, you might add order.id to make traces more useful.Creating Custom Spans
Automatic instrumentation captures HTTP requests and external calls, but it doesn’t track your application’s internal logic. For important operations, you can manually create spans to trace specific parts of your code. A span represents a unit of work, and creating sub-spans helps you see detailed timing and context for key processes. For example, if a request triggers a complex function or external call that isn’t automatically captured, you can create a span to trace that specific operation. Manual instrumentation fills these gaps by letting you track what happens inside your application, not just at the edges.Complete Application Example
Below is a comprehensive example that demonstrates all the OpenTelemetry concepts we’ve covered. This application creates an order service Express server that sets up OpenTelemetry tracing with proper configuration, automatically instruments HTTP requests using auto-instrumentations, creates custom spans for database operations, and adds custom attributes to provide order-specific context.Run your application and view logged trace
Run the application and make a request to test the tracing:Advanced Configuration
Sampling
Tracing sampling is a crucial technique for managing the volume of trace data in production environments. By default, OpenTelemetry NodeJs traces every request, which works well for debugging or development but can become expensive and noisy in high-traffic production systems. Sampling helps in several ways: it reduces noise in traces, helping you focus on important traces while maintaining visibility into your system. It also helps with cost management in terms of storage, processing, and network bandwidth, making it essential for production deployments.Sampling Strategies
OpenTelemetry supports several built-in samplers, but in practice, two cover most use cases: 1. TraceIdRatioBased Sampler Samples a fixed percentage of root traces. This sampler makes sampling decisions independently for each trace.Troubleshooting
Partial Traces If you see partial traces (missing spans in the middle of a trace), ensure you’re usingParentBased
sampler:
Local Debugging
For local development and debugging, you can use the console exporter to see traces in your terminal: Update setupOTelSDK function to create a console exporter and inject into NodeSDKUsing Instrumentation Libraries
OpenTelemetry provides instrumentation libraries for many popular frameworks and libraries in the Node.js ecosystem. ThegetNodeAutoInstrumentations()
function we used earlier automatically includes all the instrumentation packages from the OpenTelemetry JavaScript Contrib repository.
When you use getNodeAutoInstrumentations()
, you get automatic instrumentation for popular frameworks like Express, Fastify, Koa, and many other libraries including HTTP clients, databases, and more.