Observability in JavaScript: Tracing, Metrics, and Logs for Node.js

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Is Your Application a Black Box? Time to Fix That

\n\n

Have you ever caught yourself thinking, "Why is my Node.js service slow, even though the code is perfect?" Or received a complaint from a user but couldn't figure out what went wrong? Sound familiar? Welcome to the world where without observability in JavaScript, you're working blind.

\n\n

Observability is not just a buzzword. It's an approach that transforms your code from a black box into a transparent system. We'll learn to ask it questions: "Who is to blame?" (tracing), "What is happening right now?" (metrics), and "What happened yesterday at 3:15 AM?" (logs).

\n\n

In this article, we'll break down the three pillars of observability in Node.js: tracing, metrics, and logs. You'll get ready-to-use code examples and understand how to implement these practices today. Let's go!

\n\n

Tracing in JavaScript: The Journey of a Request

\n\n

Imagine a user clicks the "Place Order" button. This request passes through the API Gateway, authentication service, cart, payment gateway, and database. If something breaks — where do you look? Tracing (or distributed tracing) gives you a map of this journey.

\n\n

What is Tracing in Simple Terms

\n\n

Tracing is a way to track the path of a specific request through all microservices and functions. Each step is a span, and the entire chain is a trace. You can see how long each stage took and where the error occurred.

\n\n

OpenTelemetry: The Standard for Observability in JavaScript

\n\n

OpenTelemetry (OTel) is the industry standard for collecting traces, metrics, and logs. It is supported by all major clouds and tools (Jaeger, Zipkin, Grafana).

\n\n

Installation:

\n
npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node
\n\n

Basic tracing example for an Express application:

\n
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');\nconst { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');\nconst { ConsoleSpanExporter } = require('@opentelemetry/exporter-console');\nconst { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');\nconst { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');\n\nconst provider = new NodeTracerProvider();\nprovider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));\nprovider.register();\n\nconst { getTracer } = require('@opentelemetry/api');\nconst tracer = getTracer('my-app');\n\n// Automatic tracing for HTTP and Express\nconst { registerInstrumentations } = require('@opentelemetry/instrumentation');\nregisterInstrumentations({\n  tracerProvider: provider,\n  instrumentations: [\n    new ExpressInstrumentation(),\n    new HttpInstrumentation()\n  ]\n});\n\n// Now every request will create a trace with spans\nconst express = require('express');\nconst app = express();\n\napp.get('/api/order', async (req, res) => {\n  // Manual span for business logic\n  const span = tracer.startSpan('processPayment');\n  try {\n    // ... your logic\n    await new Promise(r => setTimeout(r, 100)); // simulation\n    span.setStatus({ code: 1 }); // OK\n  } catch (err) {\n    span.setStatus({ code: 2, message: err.message }); // ERROR\n    throw err;\n  } finally {\n    span.end();\n  }\n  res.send('Order created');\n});\n\napp.listen(3000);
\n\n

Now in the console you'll see structured data: which request, how long each middleware took, where the error was. This is observability javascript in action.

\n\n

Metrics: The Pulse of Your Application

\n\n

Tracing is great for individual requests, but what about the big picture? How many requests per second? What is the latency? How much memory is your Node.js consuming? Metrics handle this.

\n\n

Prometheus and the Node.js Client

\n\n

Prometheus is the de facto standard for collecting metrics. It "pulls" data from your application over HTTP.

Blogs

Book Recommendations