# swagger-stats-redis

> 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

Latest version **0.5.0** (published 2020-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install swagger-stats-redis
pnpm add swagger-stats-redis
yarn add swagger-stats-redis
bun add swagger-stats-redis
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2020-01-06 |
| First published | 2019-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 5.9 MB |
| Known vulnerabilities | 0 (+5 in 5 direct dependencies) |
| Install scripts | no |
| GitHub stars | 932 |
| Author | https://github.com/sv2 |
| Maintainers | veonbean |
| Keywords | api, telemetry, node, express, koa, hapi, fastify, rest, statistics, monitoring, alerting, swagger, openapi, schema, prometheus, metrics, elasticsearch, kibana, grafana |

## Links

- npm: https://www.npmjs.com/package/swagger-stats-redis
- Repository: https://github.com/slanatech/swagger-stats
- Homepage: http://swaggerstats.io
- Issues: https://github.com/slanatech/swagger-stats/issues
- npm.io page: https://npm.io/package/swagger-stats-redis

## Dependencies (15)

- [qs](https://npm.io/package/qs.md) ^6.7.0
- [pm2](https://npm.io/package/pm2.md) ^4.2.0
- [send](https://npm.io/package/send.md) ^0.17.1
- [uuid](https://npm.io/package/uuid.md) ^3.3.3
- [debug](https://npm.io/package/debug.md) ^3.1.0
- [moment](https://npm.io/package/moment.md) ^2.19.3
- [cookies](https://npm.io/package/cookies.md) ^0.7.1
- [ioredis](https://npm.io/package/ioredis.md) ^4.14.1
- [natives](https://npm.io/package/natives.md) ^1.1.6
- [request](https://npm.io/package/request.md) ^2.85.0
- [bluebird](https://npm.io/package/bluebird.md) ^3.7.1
- [basic-auth](https://npm.io/package/basic-auth.md) ^2.0.0
- [prom-client](https://npm.io/package/prom-client.md) ^11.0.0
- [express-to-koa](https://npm.io/package/express-to-koa.md) ^2.0.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^2.1.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2020-01-06
- 0.5.0-0 — 2020-01-06
- 0.2.0-0 — 2019-12-04
- 0.1.2 — 2019-11-28
- 0.1.1 — 2019-11-26
- 0.1.0 — 2019-11-26

## README

<p align="center">
<img src="https://github.com/slanatech/swagger-stats/blob/master/screenshots/logo.png?raw=true" alt="swagger-stats"/>
</p>

# swagger-stats


####  [https://swaggerstats.io](https://swaggerstats.io) | [Guide](https://swaggerstats.io/guide/) 

[![Build Status](https://travis-ci.org/slanatech/swagger-stats.svg?branch=master)](https://travis-ci.org/slanatech/swagger-stats)
[![Coverage Status](https://coveralls.io/repos/github/slanatech/swagger-stats/badge.svg?branch=master&dummy)](https://coveralls.io/github/slanatech/swagger-stats?branch=master&dummy)
[![npm version](https://badge.fury.io/js/swagger-stats.svg)](https://badge.fury.io/js/swagger-stats)
[![npm downloads](https://img.shields.io/npm/dm/swagger-stats.svg)](https://img.shields.io/npm/dm/swagger-stats)
[![Gitter](https://badges.gitter.im/swagger-stats/community.svg)](https://gitter.im/swagger-stats/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)



## 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](https://swagger.io/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`   


![swagger-stats Built-In Monitoring](screenshots/ui0950.gif?raw=true)


       
### API Analytics with [Elasticsearch](https://www.elastic.co/) and [Kibana](https://www.elastic.co/products/kibana)

> **swagger-stats** stores details about each request/response in [Elasticsearch](https://www.elastic.co/), so you may use [Kibana](https://www.elastic.co/products/kibana) 
> to perform detailed analysis of API usage over time, build visualizations and dashboards


![swagger-stats Kibana Dashboard](screenshots/kibana.gif?raw=true)

See `dashboards/elastic6` for swagger-stats Kibana visualizations and dashboards
 

### Monitoring and Alerting with [Prometheus](https://prometheus.io/) and [Grafana](https://grafana.com/)

> **swagger-stats** exposes metrics in [Prometheus](https://prometheus.io/) format, so you may use [Prometheus](https://prometheus.io/) and [Grafana](https://grafana.com/) to setup API monitoring and alerting


![swagger-stats Prometheus Dashboard](screenshots/prometheus-dashboard-2-sm.png?raw=true)


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](https://prometheus.io/) format, so you may use [Prometheus](https://prometheus.io/) and [Grafana](https://grafana.com/) to setup API monitoring and alerting
* Storing details about each API Request/Response in [Elasticsearch](https://www.elastic.co/), so you may use [Kibana](https://www.elastic.co/products/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](https://swagger.io/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

```javascript
const swStats = require('swagger-stats-redis');
const apiSpec = require('swagger.json');
app.use(swStats.getMiddleware({swaggerSpec:apiSpec}));
```

#### Fastify

```javascript
const swStats = require('swagger-stats');
const apiSpec = require('swagger.json');

const fastify = require('fastify')({
    logger: true
});

fastify.register(swStats.getFastifyPlugin, {swaggerSpec:apiSpec});
```


#### Koa

[`express-to-koa`](https://github.com/kaelzhang/express-to-koa) can be used which is just a simple `Promise` wrapper.

```javascript
const swStats = require('swagger-stats');
const apiSpec = require('swagger.json');
const e2k = require('express-to-koa');
app.use(e2k(swStats.getMiddleware({ swaggerSpec:apiSpec })));
```

#### Hapi

```javascript
const swStats = require('swagger-stats');
const swaggerSpec = require('./petstore.json');

const init = async () => {

    server = Hapi.server({
        port: 3040,
        host: 'localhost'
    });

    await server.register({
        plugin: swStats.getHapiPlugin,
        options: {
             swaggerSpec:swaggerSpec
        }
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};
````

See `/examples` for sample apps

### Get Statistics with API


```bash
$ 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](http://swaggerstats.io/docs.html#api) for more details on API and returned statistics.


### Get Prometheus Metrics 


```bash
$ 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: 
```javascript
  const Redis = require('redis');

  const redis = Redis.createClient({
    // some config
  });
  ....

  const swStats = require('swagger-stats-redis');
  swStats.init({
    ```

    pm2: true,
    redis,

    ```
  });


```
## 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](https://github.com/veonbean/swagger-stats-redis/CHANGELOG.md)

## Enhancements and Bug Reports

If you find a bug, or have an enhancement in mind please post [issues](https://github.com/veonbean/swagger-stats-redis/issues) on GitHub.

## License
 
MIT

---
_Source: https://npm.io/package/swagger-stats-redis · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
