Installations
npm install swagger-stats
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
20.3.0
NPM Version
9.6.7
Score
64.3
Supply Chain
97.1
Quality
74.1
Maintenance
50
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.43%)
HTML (0.57%)
Developer
Download Statistics
Total Downloads
7,154,886
Last Day
1,095
Last Week
40,276
Last Month
144,442
Last Year
1,555,969
GitHub Statistics
907 Stars
464 Commits
139 Forks
21 Watching
9 Branches
16 Contributors
Package Meta Information
Latest Version
0.99.7
Package Id
swagger-stats@0.99.7
Unpacked Size
5.02 MB
Size
1.62 MB
File Count
90
NPM Version
9.6.7
Node Version
20.3.0
Publised On
16 Jun 2023
Total Downloads
Cumulative downloads
Total Downloads
7,154,886
Last day
13.8%
1,095
Compared to previous day
Last week
13.8%
40,276
Compared to previous week
Last month
3.9%
144,442
Compared to previous month
Last year
-2%
1,555,969
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
swagger-stats | API Observability
https://swaggerstats.io | Guide
Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices
Express, Fastify, Koa, Hapi, Restify
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 UX, 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/
API Analytics with Elasticsearch and Kibana
swagger-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
Monitoring and Alerting with Prometheus and Grafana
swagger-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 --save
If you haven't added prom-client already, you should do this now. It's a peer dependency of swagger-stats as of version 0.95.19.
npm install prom-client@12 --save
Enable swagger-stats middleware in your app
Express
1const swStats = require('swagger-stats'); 2const apiSpec = require('swagger.json'); 3app.use(swStats.getMiddleware({swaggerSpec:apiSpec}));
Fastify
1const swStats = require('swagger-stats'); 2const apiSpec = require('swagger.json'); 3 4const fastify = require('fastify')({ 5 logger: true 6}); 7 8// Enable swagger-stats 9fastify.register(require('fastify-express')).then(()=>{ 10 fastify.register(swStats.getFastifyPlugin, {swaggerSpec:apiSpec}); 11}); 12
Koa
express-to-koa
can be used which is just a simple Promise
wrapper.
1const swStats = require('swagger-stats'); 2const apiSpec = require('swagger.json'); 3const e2k = require('express-to-koa'); 4app.use(e2k(swStats.getMiddleware({ swaggerSpec:apiSpec })));
Hapi
1const swStats = require('swagger-stats'); 2const swaggerSpec = require('./petstore.json'); 3 4const init = async () => { 5 6 server = Hapi.server({ 7 port: 3040, 8 host: 'localhost' 9 }); 10 11 await server.register({ 12 plugin: swStats.getHapiPlugin, 13 options: { 14 swaggerSpec:swaggerSpec 15 } 16 }); 17 18 await server.start(); 19 console.log('Server running on %s', server.info.uri); 20};
Restify
1const restify = require('restify'); 2const swStats = require('swagger-stats'); 3const apiSpec = require('swagger.json'); 4 5const server = restify.createServer(); 6 7server.pre(swStats.getMiddleware({ 8 swaggerSpec:apiSpec, 9}));
See /examples
for sample apps
Get Statistics with API
$ curl http://<your app host:port>/swagger-stats/stats
{
"startts": 1501647865959,
"all": {
"requests": 7,
"responses": 7,
"errors": 3,
"info": 0,
"success": 3,
"redirect": 1,
"client_error": 2,
"server_error": 1,
"total_time": 510,
"max_time": 502,
"avg_time": 72.85714285714286,
"total_req_clength": 0,
"max_req_clength": 0,
"avg_req_clength": 0,
"total_res_clength": 692,
"max_res_clength": 510,
"avg_res_clength": 98,
"req_rate": 1.0734549915657108,
"err_rate": 0.4600521392424475
},
"sys": {
"rss": 59768832,
"heapTotal": 36700160,
"heapUsed": 20081776,
"external": 5291923,
"cpu": 0
},
"name": "swagger-stats-testapp",
"version": "0.90.1",
"hostname": "hostname",
"ip": "127.0.0.1"
}
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
. . . . . . . . . .
Default Metrics
To collect prom-client default metrics:
1const swaggerStats = require('swagger-stats'); 2const promClient = require('prom-client'); 3 4promClient.collectDefaultMetrics();
Some Node.js specific metrics are included, such as event loop lag:
# HELP nodejs_eventloop_lag_seconds Lag of event loop in seconds.
# TYPE nodejs_eventloop_lag_seconds gauge
nodejs_eventloop_lag_seconds 0.000193641 1597303877464
. . . . . . . . . .
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
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:28
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:29
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Info: no jobLevel write permissions found
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 0 commits out of 13 are checked with a SAST tool
Reason
Found 2/20 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/slanatech/swagger-stats/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:46: update your workflow using https://app.stepsecurity.io/secureworkflow/slanatech/swagger-stats/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:57: update your workflow using https://app.stepsecurity.io/secureworkflow/slanatech/swagger-stats/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:71: update your workflow using https://app.stepsecurity.io/secureworkflow/slanatech/swagger-stats/codeql-analysis.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
23 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-8hc4-vh64-cxmj
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-h452-7996-h45h
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-rrr8-f88r-h8q6
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
3.9
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to swagger-stats
@types/swagger-stats
TypeScript definitions for swagger-stats
profoundjs-swagger-stats
API Telemetry and APM. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices, based on express routes and Swagger (Open API) specification
swagger-stats-ux
API Observability. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices, based on express routes and Swagger (Open API) specification
swagger-stats-ioredis
API Telemetry and APM. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices, based on express routes and Swagger (Open API) specification