-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
131 lines (114 loc) · 3.52 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Newly added requires
*/
var Register = require('prom-client').register;
var Counter = require('prom-client').Counter;
var Histogram = require('prom-client').Histogram;
var Summary = require('prom-client').Summary;
var Gauge = require('prom-client').Gauge;
var ResponseTime = require('response-time');
let appVersion = process.env.APP_VERSION;
let appCommit = process.env.APP_COMMIT;
let appBranch = process.env.APP_BRANCH;
let appTime = process.env.APP_TIME;
if (!appVersion) {
appVersion = 'undefinded';
console.log(`Application version: ${appVersion}`);
}
if (!appCommit) {
appCommit = 'undefinded';
console.log(`Application commit: ${appCommit}`);
}
if (!appBranch) {
appBranch = 'undefinded';
console.log(`Application branch: ${appBranch}`);
}
if (!appTime) {
appTime = 'undefinded';
console.log(`Application branch: ${appTime}`);
}
var version = appVersion;
var commit = appCommit;
var branch = appBranch;
var buildTime = appTime;
/**
* A Prometheus counter that counts the invocations of the different HTTP verbs
* e.g. a GET and a POST call will be counted as 2 different calls
*/
module.exports.numOfRequests = numOfRequests = new Counter({
name: 'numOfRequests',
help: 'Number of requests made',
labelNames: ['method']
});
/**
* A Prometheus counter that counts the invocations with different paths
* e.g. /foo and /bar will be counted as 2 different paths
*/
module.exports.pathsTaken = pathsTaken = new Counter({
name: 'pathsTaken',
help: 'Paths taken in the app',
labelNames: ['path']
});
module.exports.pathsTaken = pathsAdmin = new Gauge({
name: `build_info`,
help: `A metric with a constant 1 value labeled by version, revision, platform, nodeVersion, os from which was built`,
labelNames: [
'version',
'commit',
'branch',
'buildTime',
]
});
pathsAdmin.set(
{
version,
commit,
branch,
buildTime,
},
1,
);
/**
* A Prometheus summary to record the HTTP method, path, response code and response time
*/
module.exports.responses = responses = new Summary({
name: 'responses',
help: 'Response time in millis',
labelNames: ['method', 'path', 'status']
});
/**
* This funtion will start the collection of metrics and should be called from within in the main js file
*/
module.exports.startCollection = function () {
console.log(`Starting the collection of metrics, the metrics are available on /metrics`);
require('prom-client').collectDefaultMetrics();
};
/**
* This function increments the counters that are executed on the request side of an invocation
* Currently it increments the counters for numOfPaths and pathsTaken
*/
module.exports.requestCounters = function (req, res, next) {
if (req.path != '/metrics') {
numOfRequests.inc({ method: req.method });
pathsTaken.inc({ path: req.path });
}
next();
}
/**
* This function increments the counters that are executed on the response side of an invocation
* Currently it updates the responses summary
*/
module.exports.responseCounters = ResponseTime(function (req, res, time) {
if(req.url != '/metrics') {
responses.labels(req.method, req.url, res.statusCode).observe(time);
}
})
/**
* In order to have Prometheus get the data from this app a specific URL is registered
*/
module.exports.injectMetricsRoute = function (App) {
App.get('/metrics', (req, res) => {
res.set('Content-Type', Register.contentType);
res.end(Register.metrics());
});
};