# prometheus-metrics

> ``` npm install --save prometheus-metrics ```

Latest version **0.2.0** (published 2024-10-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install prometheus-metrics
pnpm add prometheus-metrics
yarn add prometheus-metrics
bun add prometheus-metrics
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: has types; no vulnerabilities.

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

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2024-10-23 |
| First published | 2019-05-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ivan |
| Maintainers | iradul |

## Links

- npm: https://www.npmjs.com/package/prometheus-metrics
- Repository: https://github.com/iradul/prometheus-metrics
- Homepage: https://github.com/iradul/prometheus-metrics#readme
- Issues: https://github.com/iradul/prometheus-metrics/issues
- npm.io page: https://npm.io/package/prometheus-metrics

## Recent versions

- 0.2.0 (latest) — 2024-10-23
- 0.1.0 — 2019-05-29

## README

## Export prometheus metrics with zero dependencies

```
npm install --save prometheus-metrics
```

If you don't need labels you can simply use `Prometheus.fromTuples()` method:
```js
const http = require('http')
const { Prometheus, MetricContentType } = require('prometheus-metrics');

http.createServer((req, res) => {
    if (req.url === '/metrics') {
        // generate metrics from tuples
        Prometheus.fromTuples(
            'myapp_',
            [
                [ 0, 'metric01', 'This is my metric', Math.floor(10000 * Math.random()) ], // tuple that defines metric
            ]
        ).then(p => {
            // set proper Content-Type header
            res.setHeader('Content-Type', MetricContentType);
            // return prometheus text based format body
            res.end(p.getTextBasedFormat())
        })
    } else {
        res.statusCode = 404;
        res.end();
    }
}).listen(8080)
```

Or you can instantiate `Prometheus` class and use `add()` method instead:
```js
const http = require('http')
const { Prometheus, MetricContentType, SystemMetrics } = require('prometheus-metrics');

const p = new Prometheus('myapp_');

SystemMetrics.getMetrics().then(sys => {
    // add system metrics
    p.add(...sys);

    // add metric
    p.add({
        type: 'counter',
        name: 'metric01',
        help: 'This is my value',
        values: [{
            labels: { my_label: 'abcd' },
            value: Math.floor(10000 * Math.random()),
            timestamp: Date.now(),
        }, {
            labels: { my_label: 'efgh' },
            value: Math.floor(10000 * Math.random()),
        }]
    });

    http.createServer((req, res) => {
        if (req.url === '/metrics') {
            // set proper Content-Type header
            res.setHeader('Content-Type', MetricContentType);
            // return Prometheus text based format body
            res.end(p.getTextBasedFormat())
        } else {
            res.statusCode = 404;
            res.end();
        }
    }).listen(8080)
})
```

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