This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from microservices-demo/prometheus-metrics
Prometheus metrics in the front-end
- Loading branch information
Showing
4 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters