Express.js
Instrument your Express.js application with OpenTelemetry to send traces, metrics, and logs to Last9
Use OpenTelemetry to instrument your JavaScript Express application and send telemetry data to Last9. This integration provides automatic instrumentation for HTTP requests, middleware operations, and other Express.js functionality.
You can either run OpenTelemetry Collector as a separate service or send telemetry directly from your application to Last9.
Prerequisites
Before setting up Express.js monitoring, ensure you have:
- Node.js 16.0 or higher installed
- Express.js application running
- Last9 account with integration credentials
- npm or yarn package manager available
-
Install OpenTelemetry Packages
Install the required OpenTelemetry packages for Express.js instrumentation:
npm install \@opentelemetry/api@1.9.0 \@opentelemetry/auto-instrumentations-node@0.59.0 \@opentelemetry/exporter-trace-otlp-grpc@0.201.1 \@opentelemetry/exporter-trace-otlp-http@0.201.1 \@opentelemetry/instrumentation@0.201.1 \@opentelemetry/resources@2.0.1 \@opentelemetry/sdk-node@0.201.1 \@opentelemetry/sdk-trace-base@2.0.1 \@opentelemetry/sdk-trace-node@2.0.1 \@opentelemetry/semantic-conventions@1.34.0These packages provide comprehensive instrumentation for Express.js applications and integrate with the Last9 platform.
-
Set Environment Variables
Configure OpenTelemetry environment variables for your Express.js application. Replace the placeholder values with your actual Last9 credentials:
export OTEL_SERVICE_NAME="<your_service_name>"export OTEL_EXPORTER_OTLP_ENDPOINT="{{ .Logs.WriteURL }}"export OTEL_EXPORTER_OTLP_HEADERS="Authorization={{ .Logs.AuthValue }}"export OTEL_TRACES_SAMPLER="always_on"export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=local"export OTEL_LOG_LEVEL=errorEnvironment Variables Explained:
OTEL_SERVICE_NAME: Name of your Express.js serviceOTEL_EXPORTER_OTLP_ENDPOINT: Last9 endpoint URL for telemetry dataOTEL_EXPORTER_OTLP_HEADERS: Authentication header for Last9OTEL_RESOURCE_ATTRIBUTES: Service metadata including environment
-
Create Instrumentation File
Create an instrumentation file that sets up OpenTelemetry for your Express.js application. Choose the appropriate version based on your project setup:
Create a file named
instrumentation.tsin your project root:import {NodeTracerProvider,TracerConfig,} from "@opentelemetry/sdk-trace-node";import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";import { registerInstrumentations } from "@opentelemetry/instrumentation";import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";import { resourceFromAttributes } from "@opentelemetry/resources";// Uncomment for troubleshooting:// import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";// For troubleshooting, set the log level to DiagLogLevel.DEBUG// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);const providerConfig: TracerConfig = {resource: resourceFromAttributes({["service.name"]: process.env.OTEL_SERVICE_NAME,["deployment.environment"]: process.env.NODE_ENV,}),spanProcessors: [new BatchSpanProcessor(new OTLPTraceExporter())],};// Initialize and register the tracer providerconst provider = new NodeTracerProvider(providerConfig);provider.register();// Automatically instrument HTTP and Express (additional instrumentations can be added similarly)registerInstrumentations({instrumentations: [getNodeAutoInstrumentations({// instrumentation-fs is disabled to reduce the noise of spans related to file operations"@opentelemetry/instrumentation-fs": {enabled: false,},}),],});Create a file named
instrumentation.jsin your project root:const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base");const {OTLPTraceExporter,} = require("@opentelemetry/exporter-trace-otlp-http");const {registerInstrumentations,} = require("@opentelemetry/instrumentation");const {getNodeAutoInstrumentations,} = require("@opentelemetry/auto-instrumentations-node");const { resourceFromAttributes } = require("@opentelemetry/resources");// For troubleshooting, set the log level to DiagLogLevel.DEBUG// Uncomment the following lines to enable OpenTelemetry debug logging:// const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);const providerConfig = {resource: resourceFromAttributes({"service.name": process.env.OTEL_SERVICE_NAME,"deployment.environment": process.env.NODE_ENV,}),spanProcessors: [new BatchSpanProcessor(new OTLPTraceExporter())],};const provider = new NodeTracerProvider(providerConfig);provider.register();registerInstrumentations({instrumentations: [getNodeAutoInstrumentations({"@opentelemetry/instrumentation-fs": { enabled: false },}),],});// Simple logging utilityconst logger = {info: (message) => console.log(`[OpenTelemetry] ${message}`),error: (message, error) =>console.error(`[OpenTelemetry Error] ${message}`, error || ""),};logger.info(`Initializing OpenTelemetry for service: ${process.env.OTEL_SERVICE_NAME}`,);logger.info("OpenTelemetry instrumentation setup complete"); -
Import Instrumentation in Your Application
Import the instrumentation file at the very top of your main application file, before any other imports. This is crucial for proper instrumentation setup.
If your main file is
server.ts, import the instrumentation at the top:import "./instrumentation";import express from "express";// ... other importsconst app = express();// ... rest of your application codeIf your main file is
server.js, require the instrumentation at the top:require("./instrumentation");const express = require("express");// ... other requiresconst app = express();// ... rest of your application codeImportant: Make sure to import the instrumentation script before any
expresspackage is imported. -
Run Your Express Application
Start your Express.js application normally. The OpenTelemetry instrumentation will automatically begin collecting and sending telemetry data to Last9:
# For developmentnpm start# Or with nodemon for developmentnodemon server.js# For productionNODE_ENV=production npm start
Understanding the Instrumentation
The instrumentation code performs the following steps:
- Sets up Trace Provider with your application’s name as the service name
- Configures OTLP Exporter with Last9 endpoint and authentication
- Registers Auto-Instrumentation for Express.js and common Node.js libraries
- Optimizes Performance by using batch processing and disabling noisy file system instrumentation
What Gets Instrumented
When you use automatic instrumentation with Express.js, OpenTelemetry automatically captures:
HTTP Requests
- Request method, URL, and headers
- Response status codes and timing
- Route parameters and query strings
- Request and response body sizes
Express Middleware
- Middleware execution order and timing
- Custom middleware operations
- Route handler performance
- Error handling middleware
Database Operations
- SQL queries and execution times (MySQL, PostgreSQL, etc.)
- MongoDB operations
- Redis commands
- Database connection pooling
External API Calls
- HTTP client requests (using
axios,fetch,http, etc.) - Request/response details and timing
- Error handling and retry attempts
File System Operations
- File reads and writes (can be disabled for performance)
- Directory operations
- File system monitoring
Advanced Configuration
Custom Service Metadata
Enhance your service metadata by updating the resource attributes:
const providerConfig: TracerConfig = { resource: resourceFromAttributes({ "service.name": process.env.OTEL_SERVICE_NAME, "service.version": "2.1.0", "deployment.environment": process.env.NODE_ENV, "service.instance.id": "api-server-1", team: "backend", }), // ... rest of configuration};Custom Spans and Attributes
Add custom spans and attributes to track business-specific operations:
import { trace } from "@opentelemetry/api";
const tracer = trace.getTracer("express-app");
app.get("/users/:userId", async (req, res) => { const span = tracer.startSpan("get_user_operation");
try { span.setAttributes({ "user.id": req.params.userId, "operation.type": "user_lookup", "request.ip": req.ip, });
// Your business logic here const user = await getUserById(req.params.userId);
span.setStatus({ code: trace.SpanStatusCode.OK }); res.json(user); } catch (error) { span.setStatus({ code: trace.SpanStatusCode.ERROR, message: error.message, }); span.recordException(error); res.status(500).json({ error: "Internal server error" }); } finally { span.end(); }});Error Handling Integration
Integrate OpenTelemetry with Express error handling:
import { trace } from "@opentelemetry/api";
// Error handling middlewareapp.use( ( error: Error, req: express.Request, res: express.Response, next: express.NextFunction, ) => { const span = trace.getActiveSpan();
if (span) { span.setStatus({ code: trace.SpanStatusCode.ERROR, message: error.message, }); span.recordException(error); }
console.error("Unhandled error:", error); res.status(500).json({ error: "Internal server error" }); },);Verification
-
Check Application Startup
When you start your Express.js application, you should see OpenTelemetry initialization messages in the console.
-
Generate Test Traffic
Make some requests to your Express.js application to generate telemetry data:
curl http://localhost:3000/curl http://localhost:3000/healthcurl http://localhost:3000/api/users/123 -
Verify Data in Last9
Log into your Last9 account and check that traces and metrics are being received in Grafana.
Look for:
- HTTP request traces with timing information
- Middleware execution spans
- Database query spans
- Error traces for failed requests
Troubleshooting
Common Issues
Instrumentation Not Working Ensure the instrumentation file is imported before any Express imports:
// ✅ Correct orderrequire("./instrumentation");const express = require("express");
// ❌ Incorrect orderconst express = require("express");require("./instrumentation");Missing Traces Enable debug logging to troubleshoot:
const { diag, DiagConsoleLogger, DiagLogLevel } = require("@opentelemetry/api");diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);Environment Variables Not Set Verify your environment variables are properly configured:
env | grep OTEL_Network Connection Issues Test connectivity to Last9:
curl -v -H "Authorization: <your-auth-header>" <your-endpoint>Performance Considerations
- Use sampling in production to control trace volume
- Disable file system instrumentation to reduce noise
- Monitor the instrumentation overhead on your application
- Use batch processing for better performance
- Consider using the OpenTelemetry Collector for high-traffic applications
Best Practices
- Import instrumentation before any other imports
- Set meaningful service names and versions
- Use consistent environment naming across services
- Add custom spans for critical business operations
- Implement proper error handling with span status updates
- Use structured logging alongside distributed tracing
Supported Libraries
OpenTelemetry automatically instruments many popular Node.js libraries commonly used with Express.js:
- HTTP Libraries:
http,https,axios,node-fetch - Databases:
mysql,mysql2,pg,mongodb,ioredis - Message Queues:
amqplib(RabbitMQ),kafkajs - Cloud Services:
aws-sdk,@google-cloud/* - Web Frameworks:
express,koa,fastify
For a complete list of supported libraries, visit the OpenTelemetry Node.js documentation.
Need Help?
If you encounter any issues or have questions:
- Join our Discord community for real-time support
- Contact our support team at support@last9.io