Browse Source

add prometheus exporter

master
Nicolas Massé 3 years ago
parent
commit
a817eeef9b
  1. 1102
      package-lock.json
  2. 4
      package.json
  3. 29
      server.js

1102
package-lock.json

File diff suppressed because it is too large

4
package.json

@ -10,6 +10,8 @@
"repository": "https://github.com/nmasse-itix/OpenShift-Demo-NodeJS.git", "repository": "https://github.com/nmasse-itix/OpenShift-Demo-NodeJS.git",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"express": "latest" "express": "latest",
"express-prom-bundle": "^6.5.0",
"prom-client": "^14.1.0"
} }
} }

29
server.js

@ -1,3 +1,5 @@
const client = require('prom-client')
const promBundle = require("express-prom-bundle");
var express = require("express"); var express = require("express");
var app = express(); var app = express();
var router = express.Router(); var router = express.Router();
@ -12,11 +14,32 @@ router.use(function (req,res,next) {
console.log("%s %s => %i", req.method, req.originalUrl, res.statusCode); console.log("%s %s => %i", req.method, req.originalUrl, res.statusCode);
}); });
// Prometheus exporter
const registry = new client.Registry()
const metricsMiddleware = promBundle({
includeMethod: true,
includePath: true,
includeStatusCode: true,
includeUp: true,
customLabels: {app: 'openshift-demo-nodejs'},
promClient: {
collectDefaultMetrics: {
}
}
});
app.use(metricsMiddleware)
const counter = new client.Counter({
name: 'openshift_demo_nodejs_calls',
help: 'Number of calls to the OpenShift-Demo-NodeJS',
});
registry.registerMetric(counter);
router.get("/",function(req,res){ router.get("/",function(req,res){
res.sendFile(__dirname + '/views/index.html'); res.sendFile(__dirname + '/views/index.html');
}); });
router.get("/info",function(req,res){ router.get("/info",function(req,res){
var status = 200;
var response = { var response = {
color: color, color: color,
podName: process.env["HOSTNAME"], podName: process.env["HOSTNAME"],
@ -24,22 +47,28 @@ router.get("/info",function(req,res){
if (!ready) { if (!ready) {
response.color = "red"; response.color = "red";
response.podName = "NOT READY"; response.podName = "NOT READY";
status = 503;
} else {
counter.inc();
} }
res.type('application/json') res.type('application/json')
.header("Connection", "close") .header("Connection", "close")
.header('Cache-Control', 'private, no-cache, no-store, must-revalidate') .header('Cache-Control', 'private, no-cache, no-store, must-revalidate')
.header('Expires', '-1') .header('Expires', '-1')
.header('Pragma', 'no-cache') .header('Pragma', 'no-cache')
.status(status)
.send(JSON.stringify(response)) .send(JSON.stringify(response))
.end(); .end();
}); });
// Liveness probe
router.get("/health/live",function(req,res){ router.get("/health/live",function(req,res){
res.type('application/json') res.type('application/json')
.send({"alive": true}) .send({"alive": true})
.end(); .end();
}); });
// Readiness probe
router.get("/health/ready",function(req,res){ router.get("/health/ready",function(req,res){
if (ready) { if (ready) {
res.type('application/json') res.type('application/json')

Loading…
Cancel
Save