bridge-node-server/node_server/prometheus-router.js
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

53 lines
1.5 KiB
JavaScript

/*
* This file defines the routes for the prometheus monitoring running on the api server
* It uses express router so it can be placed under any path, and will then
* assume everything else is under the root of that path.
* e.g. to place under /metrics you would do:
*
* var appHttps = [main app setup]
* var promRouterFactory = require(<this file>);
* var promRouter = prometheusRouterFactory();
* appHttps.use('/metrics', promRouter);
*/
'use strict';
var path = require('path');
var express = require('express');
var morgan = require('morgan');
var promClient = require('prom-client');
const AUTHORIZATION_TOKEN = 'f7632bff-7ef4-4ecb-aba1-0ab678712556';
module.exports = (function() {
return function prometheusRouterFactory() {
var router = express.Router();
//
// Logging middleware
//
router.use(morgan('combined'));
/*
* If the request looks like a request for metrics then return them.
*/
router.use('/', function(req, res, next) {
/**
* Check the authorization header value is correct
*/
if (req.headers.authorization !== 'Bearer ' + AUTHORIZATION_TOKEN) {
res.status(401).end();
} else {
/**
* All ok, so return the metrics
*/
res.end(promClient.register.metrics());
}
});
/*
* Return the configured router
*/
return router;
};
})();