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

Commit

Permalink
Merge pull request #76 from microservices-demo/prometheus-metrics
Browse files Browse the repository at this point in the history
Prometheus metrics in the front-end
  • Loading branch information
nustiueudinastea authored Mar 13, 2017
2 parents ab557ca + 42dc10e commit 4aaf9d1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
61 changes: 61 additions & 0 deletions api/metrics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
(function (){
'use strict';

var express = require("express")
, client = require('prom-client')
, app = express()

const metric = {
http: {
requests: {
duration: new client.Summary('request_duration_seconds', 'request duration in seconds', ['method', 'path', 'status']),
}
}
}

function s(start) {
var diff = process.hrtime(start);
return (diff[0] * 1e9 + diff[1]) / 1000000000;
}

function parse(path) {
var clean_path = path;

if (path[path.length - 1] != '/') {
if (!path.includes('.')) {
clean_path = path.substr(0, path.lastIndexOf('/') + 1);
}
};

return clean_path;
}

function observe(method, path, statusCode, start) {
var path = path.toLowerCase();
if (path !== '/metrics' && path !== '/metrics/') {
var duration = s(start);
var method = method.toLowerCase();
var clean_path = parse(path);
metric.http.requests.duration.labels(method, clean_path, statusCode).observe(duration);
}
};

function middleware(request, response, done) {
var start = process.hrtime();

response.on('finish', function() {
observe(request.method, request.path, response.statusCode, start);
});

return done();
};


app.use(middleware);
app.get("/metrics", function(req, res) {
res.header("content-type", "text/plain");
return res.end(client.register.metrics())
});

module.exports = app;
}());
1 change: 1 addition & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* var app = express();
* app.use(helpers.errorHandler);
* */

helpers.errorHandler = function(err, req, res, next) {
var ret = {
message: err.message,
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"finalhandler": "^0.4.1",
"request": "^2.72.0",
"serve-static": "^1.10.2",
"epimetheus": "^1.0.46",
"prom-client": "^6.1.0",
"prom-client": "^6.3.0",
"morgan": "^1.7.0",
"connect-redis": "^3.2.0"
},
Expand Down
8 changes: 4 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ var request = require("request")
, async = require("async")
, cookieParser = require("cookie-parser")
, session = require("express-session")
, epimetheus = require("epimetheus")
, config = require("./config")
, helpers = require("./helpers")
, cart = require("./api/cart")
, catalogue = require("./api/catalogue")
, orders = require("./api/orders")
, user = require("./api/user")
, metrics = require("./api/metrics")
, app = express()

epimetheus.instrument(app);

app.use(express.static("public"));
app.use(metrics);
if(process.env.SESSION_REDIS) {
console.log('Using the redis based session manager');
app.use(session(config.session_redis));
Expand All @@ -29,7 +28,6 @@ else {

app.use(bodyParser.json());
app.use(cookieParser());
app.use(helpers.errorHandler);
app.use(helpers.sessionMiddleware);
app.use(morgan("dev", {}));

Expand All @@ -50,6 +48,8 @@ app.use(catalogue);
app.use(orders);
app.use(user);

app.use(helpers.errorHandler);

var server = app.listen(process.env.PORT || 8079, function () {
var port = server.address().port;
console.log("App now running in %s mode on port %d", app.get("env"), port);
Expand Down

0 comments on commit 4aaf9d1

Please sign in to comment.