API Telemetry and Monitoring for Node.js
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
-
Error: Cannot find module 'prom-client'
cause `prom-client` is a peer dependency and must be installed explicitly in your project, but it is missing.fixRun `npm install prom-client` or `yarn add prom-client` in your project directory. -
API Responses page in built-in Telemetry UI is not responding (or slow) with ~1000 endpoints
cause The built-in UI experienced performance issues when displaying a very large number of API endpoints.fixThis issue was specifically addressed in `v0.99.2`. Upgrade `swagger-stats` to `0.99.2` or later to leverage performance improvements for the UI. -
body response show duplicate data (in Elasticsearch)
cause Prior versions might have stored body responses inefficiently, leading to duplicate data when querying in Elasticsearch.fixUpgrade to `swagger-stats` `0.95.18` or later. This version introduces support for Elasticsearch 7.X `flattened` data types, which addresses this issue and improves storage efficiency. Re-index or clear old data in Elasticsearch if needed. -
Authentication not working on /swagger-stats/ux or Hapi authentication issues
cause Bugs in authentication mechanisms were present in earlier versions for both the built-in UI and Hapi framework integration.fixUpgrade to `swagger-stats` `0.99.1` or later, as several authentication-related bugs were fixed in this release, including Hapi-specific issues and general UI protection.
Warnings
- breaking The `prom-client` package was converted from a direct dependency to a peer dependency. You must now explicitly install `prom-client` in your project.
- gotcha The internal dependency on the deprecated `request` package was replaced with `axios`. While this is an internal change, it's good practice to be aware of dependency updates for security and compatibility.
- gotcha Version `0.99.1` introduced a full switch to a new UX for the built-in telemetry UI. This might affect custom UI configurations, styling, or expectations of the dashboard layout.
- breaking For Elasticsearch 7.X, swagger-stats now supports data type `flattened` for request/response bodies. This is an improvement but implies changes in how data is stored and queried in Elasticsearch, potentially breaking existing Kibana dashboards or queries.
Install
-
npm install swagger-stats -
yarn add swagger-stats -
pnpm add swagger-stats
Imports
- swaggerStats
import { swaggerStats } from 'swagger-stats';import swaggerStats from 'swagger-stats'; // Or for CommonJS: const swaggerStats = require('swagger-stats'); - swaggerStats.express
app.use(require('swagger-stats').middleware(options));import swaggerStats from 'swagger-stats'; app.use(swaggerStats.express(options)); // Or for CommonJS: const swaggerStats = require('swagger-stats'); app.use(swaggerStats.express(options)); - swaggerStats.get) (/swagger-stats
app.get('/swagger-stats', swaggerStats.get());import swaggerStats from 'swagger-stats'; app.use(swaggerStats.express({ name: 'my-api', uriPath: '/swagger-stats' }));
Quickstart
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`);
});