swagger-stats
https://swaggerstats.io | Guide
API Telemetry and APM
Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices
Supports Express, Fastify, Koa and Hapi
swagger-stats traces REST API requests and responses in Node.js Microservices, and collects statistics per API Operation. swagger-stats detects API operations based on express routes. You may also provide Swagger (Open API) specification, and swagger-stats will match API requests with API Operations defined in swagger specification.
swagger-stats exposes statistics and metrics per API Operation, such as GET /myapi/:parameter
, or GET /pet/{petId}
Built-In API Telemetry
swagger-stats provides built-in Telemetry UI, so you may enable swagger-stats in your app, and start monitoring immediately, with no infrastructure requirements. Navigate to
http://<your app host:port>/swagger-stats/ui
Elasticsearch and Kibana
API Analytics withswagger-stats stores details about each request/response in Elasticsearch, so you may use Kibana to perform detailed analysis of API usage over time, build visualizations and dashboards
See dashboards/elastic6
for swagger-stats Kibana visualizations and dashboards
Prometheus and Grafana
Monitoring and Alerting withswagger-stats exposes metrics in Prometheus format, so you may use Prometheus and Grafana to setup API monitoring and alerting
See dashboards/prometheus
for swagger-stats Grafana dashboards
With statistics and metrics exposed by swagger-stats you may spot problematic API endpoints, see where most of errors happens, catch long-running requests, analyze details of last errors, observe trends, setup alerting.
swagger-stats provides:
- Metrics in Prometheus format, so you may use Prometheus and Grafana to setup API monitoring and alerting
- Storing details about each API Request/Response in Elasticsearch, so you may use Kibana to perform analysis of API usage over time, build visualizations and dashboards
- Built-in API Telemetry UI, so you may enable swagger-stats in your app, and start monitoring right away, with no additional tools required
- Exposing collected statistics via API, including:
- Counts of requests and responses(total and by response class), processing time (total/avg/max), content length(total/avg/max) for requests and responses, rates for requests and errors. This is baseline set of stats.
- Statistics by Request Method: baseline stats collected for each request method
- Timeline: baseline stats collected for each 1 minute interval during last 60 minutes. Timeline helps you to analyze trends.
- Errors: count of responses per each error code, top "not found" resources, top "server error" resources
- Last errors: request and response details for the last 100 errors (last 100 error responses)
- Longest requests: request and response details for top 100 requests that took longest time to process (time to send response)
- Tracing: Request and Response details - method, URLs, parameters, request and response headers, addresses, start/stop times and processing duration, matched API Operation info
- API Statistics: baseline stats and parameter stats per each API Operation. API operation detected based on express routes, and based on Swagger (Open API) specification
- CPU and Memory Usage of Node process
How to Use
Install
npm install swagger-stats-redis --save
Enable swagger-stats middleware in your app
Express
const swStats = ;const apiSpec = ;app;
Fastify
const swStats = ;const apiSpec = ; const fastify = logger: true; fastify;
Koa
express-to-koa
can be used which is just a simple Promise
wrapper.
const swStats = ;const apiSpec = ;const e2k = ;app;
Hapi
const swStats = ;const swaggerSpec = ; const init = async { server = Hapi; await server; await serverstart; console;};
See /examples
for sample apps
Get Statistics with API
$ curl http://<your app host:port>/swagger-stats/stats
Take a look at Documentation for more details on API and returned statistics.
Get Prometheus Metrics
$ curl http://<your app host:port>/swagger-stats/metrics# HELP api_all_request_total The total number of all API requests received # TYPE api_all_request_total counter api_all_request_total 88715# HELP api_all_success_total The total number of all API requests with success response # TYPE api_all_success_total counter api_all_success_total 49051# HELP api_all_errors_total The total number of all API requests with error response # TYPE api_all_errors_total counter api_all_errors_total 32152# HELP api_all_client_error_total The total number of all API requests with client error response # TYPE api_all_client_error_total counter api_all_client_error_total 22986 . . . . . . . . . .
parse redis
default config for redis:
const Redis = ; const redis = Redis; ... const swStats = ; swStats;
custom upload
reportMCall(service, method, path, http_code, code)
arg | require | default | description |
---|---|---|---|
service | true | - | service name |
method | true | - | http method |
path | true | - | url path |
http_code | true | - | http request code |
code | fasle | 0 | error code |
example
import * as swStats from 'swagger-stats-redis';
swStats.reportMCall('user', 'GET', '/index', 200, 0);
Updates
See Changelog
Enhancements and Bug Reports
If you find a bug, or have an enhancement in mind please post issues on GitHub.
License
MIT