API Telemetry and Monitoring for Node.js

0.99.7 · active · verified Tue Apr 21

Swagger-stats is a Node.js library for API telemetry and Application Performance Monitoring (APM). It traces REST API calls and responses, collecting performance, health, and usage statistics per API operation. It supports popular Node.js frameworks including Express, Fastify, Koa, Hapi, and Restify, detecting API operations based on Express routes or an optional OpenAPI (Swagger) specification. Key differentiators include a built-in telemetry UI for immediate monitoring, out-of-the-box integration with Prometheus/Grafana for monitoring and alerting, and Elasticsearch/Kibana for detailed API analytics. The current stable version is 0.99.7, indicating it's actively developed and approaching a 1.0 release, with a cadence of regular bug fixes and dependency updates, alongside occasional feature enhancements. It provides granular metrics to identify problematic endpoints, errors, and performance bottlenecks.

Common errors

Warnings

Install

Imports

Quickstart

Sets up an Express application with swagger-stats middleware, exposing basic API telemetry, a built-in UI, and Prometheus metrics.

import express from 'express';
import swaggerStats from 'swagger-stats';
import swaggerSpec from './swagger.json'; // Replace with your OpenAPI spec path

const app = express();
const PORT = process.env.PORT || 3000;

// Basic Express route
app.get('/api/hello', (req, res) => {
  res.json({ message: 'Hello from API!' });
});

// Integrate swagger-stats middleware
app.use(swaggerStats.express({
  name: 'My API Service',
  version: '1.0.0',
  swaggerSpec: swaggerSpec, // Optional: Provide your OpenAPI specification
  uriPath: '/swagger-stats', // Default path for UI and metrics
  hostname: 'localhost',
  port: PORT,
  // Uncomment and configure for authentication if needed
  // authentication: true,
  // onAuthenticate: (req, username, password) => {
  //   return username === 'admin' && password === 'admin';
  // },
}));

app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log(`Swagger-stats UI available at http://localhost:${PORT}/swagger-stats`);
  console.log(`Prometheus metrics available at http://localhost:${PORT}/swagger-stats/metrics`);
});

view raw JSON →