Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Make timestamps optional #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ typings/
# dotenv environment variables file
.env

.idea
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as promClient from "prom-client";
import * as express from "express";

export const client: typeof promClient;
export declare function instrument (server: express.Express): void;
export const client: typeof promClient;
export declare function instrument (server: express.Express, options?): void;
31 changes: 25 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const client = require("prom-client");
const gcStats = require("prometheus-gc-stats");
const cluster = require("cluster");

client.collectDefaultMetrics();
gcStats(client.register)();
Expand Down Expand Up @@ -29,6 +30,7 @@ const metric = {
function defaultOptions(options) {
options = options || {};
options.url = options.url || "/metrics";
options.timestamps = options.timestamps || false;
return options;
}

Expand All @@ -47,8 +49,13 @@ function instrument(server, options) {
status: res.statusCode
};

metric.http.requests.clients.dec(1, Date.now());
metric.http.requests.throughput.inc(labels, 1, Date.now());
if (opt.timestamps) {
metric.http.requests.clients.dec(1, Date.now());
metric.http.requests.throughput.inc(labels, 1, Date.now());
} else {
metric.http.requests.clients.dec(1);
metric.http.requests.throughput.inc(labels, 1);
}
end(labels);
});
}
Expand All @@ -58,10 +65,22 @@ function instrument(server, options) {

server.use(middleware);

server.get(opt.url, (req, res) => {
res.header("content-type", "text/plain; charset=utf-8");
return res.send(client.register.metrics());
});
if (cluster.isMaster) {
const aggregatorRegistry = new client.AggregatorRegistry();

server.get(opt.url, (req, res) => {
aggregatorRegistry.clusterMetrics((err, metrics) => {
if (err) console.log(err);
res.set('Content-Type', aggregatorRegistry.contentType);
res.send(metrics);
});
});
} else {
server.get(opt.url, (req, res) => {
res.header("content-type", "text/plain; charset=utf-8");
return res.send(client.register.metrics(opt.timestamps));
});
}
}
module.exports = {
client,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trussle/tricorder",
"version": "1.0.1",
"version": "1.1.0",
"description": "Express middleware to provide basic metrics to Prometheus",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down