mirror of
https://github.com/BreizhHardware/express-prom-bundle.git
synced 2026-01-18 16:27:28 +01:00
upgrade jasme, cosmetics/whitespace fixes
This commit is contained in:
34
README.md
34
README.md
@@ -2,7 +2,7 @@
|
||||
|
||||
# express prometheus bundle
|
||||
|
||||
express middleware with popular prometheus metrics in one bundle.
|
||||
Express middleware with popular prometheus metrics in one bundle. It's also compatible with koa v1 (see below).
|
||||
|
||||
Internally it uses **prom-client**. See: https://github.com/siimon/prom-client
|
||||
|
||||
@@ -22,25 +22,14 @@ npm install express-prom-bundle
|
||||
|
||||
```javascript
|
||||
const promBundle = require("express-prom-bundle"),
|
||||
metricsMiddleware = promBundle({/* options */ });
|
||||
metricsMiddleware = promBundle({/* options */ }),
|
||||
app = require("express")();
|
||||
|
||||
app.use(metricsMiddleware);
|
||||
app.use(/* your middleware */);
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
**Usage Koa Framework (currently only v1)**
|
||||
|
||||
```javascript
|
||||
const promBundle = require("express-prom-bundle"),
|
||||
c2k = require("koa-connect"),
|
||||
metricsMiddleware = promBundle({/* options */ });
|
||||
|
||||
app.use(c2k(metricsMiddleware));
|
||||
app.use(/* your middleware */);
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
* call your endpoints
|
||||
* see your metrics here: [http://localhost:3000/metrics](http://localhost:3000/metrics)
|
||||
|
||||
@@ -60,7 +49,7 @@ See the example below.
|
||||
* **excludeRoutes**: array of strings or regexp specifying which routes should be skipped for `http_request_seconds` metric. It uses `req.path` as subject when checking
|
||||
* **autoregister**: boolean. If `/metrics` endpoint should be registered. It is **true** by default
|
||||
|
||||
## Example
|
||||
## express example
|
||||
|
||||
setup std. metrics but exclude `up`-metric:
|
||||
|
||||
@@ -92,6 +81,21 @@ app.listen(3000);
|
||||
|
||||
See an [advanced example on github](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js)
|
||||
|
||||
## koa v1 example
|
||||
|
||||
```javascript
|
||||
const promBundle = require("express-prom-bundle"),
|
||||
koa = require("koa"),
|
||||
c2k = require("koa-connect"),
|
||||
metricsMiddleware = promBundle({/* options */ });
|
||||
|
||||
const app = koa();
|
||||
|
||||
app.use(c2k(metricsMiddleware));
|
||||
app.use(/* your middleware */);
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
"eslint": "^2.13.1",
|
||||
"express": "^4.13.4",
|
||||
"istanbul": "^0.4.4",
|
||||
"jasme": "^4.1.2",
|
||||
"jasme": "^5.2.0",
|
||||
"koa": "^1.2.4",
|
||||
"koa-connect": "^1.0.0",
|
||||
"supertest": "^1.2.0",
|
||||
|
||||
20
src/index.js
20
src/index.js
@@ -1,8 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
const PromFactory = require("./PromFactory"),
|
||||
onFinished = require("on-finished"),
|
||||
url = require("url");
|
||||
const PromFactory = require("./PromFactory");
|
||||
const onFinished = require("on-finished");
|
||||
const url = require("url");
|
||||
|
||||
function matchVsRegExps(element, regexps) {
|
||||
for (let regexp of regexps) {
|
||||
@@ -77,9 +77,8 @@ function main(opts) {
|
||||
}
|
||||
};
|
||||
|
||||
const metrics = {},
|
||||
names = prepareMetricNames(opts, metricTemplates);
|
||||
|
||||
const metrics = {};
|
||||
const names = prepareMetricNames(opts, metricTemplates);
|
||||
|
||||
for (let name of names) {
|
||||
metrics[name] = metricTemplates[name]();
|
||||
@@ -98,14 +97,14 @@ function main(opts) {
|
||||
metrics["nodejs_memory_heap_used_bytes"].set(memoryUsage.heapUsed);
|
||||
}
|
||||
|
||||
res.writeHead(200, {"Content-Type": "text/plain"});
|
||||
res.writeHead(200, {"Content-Type": "text/plain"});
|
||||
res.end(factory.promClient.register.metrics());
|
||||
};
|
||||
|
||||
const middleware = function (req, res, next) {
|
||||
|
||||
const path = req.path || url.parse(req.url).pathname;
|
||||
let labels;
|
||||
const path = req.path || url.parse(req.url).pathname;
|
||||
let labels;
|
||||
|
||||
if (opts.autoregister && path == "/metrics") {
|
||||
return metricsMiddleware(req,res);
|
||||
@@ -124,8 +123,7 @@ function main(opts) {
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
middleware.factory = factory;
|
||||
|
||||
Reference in New Issue
Block a user