-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathapp.js
More file actions
35 lines (28 loc) · 945 Bytes
/
app.js
File metadata and controls
35 lines (28 loc) · 945 Bytes
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
const express = require('express');
const promClient = require('prom-client');
const app = express();
const port = 3000;
// Create a default Registry and collect default metrics
const register = new promClient.Registry();
promClient.collectDefaultMetrics({ register });
// Define a custom metric
const httpRequestsTotal = new promClient.Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'route', 'code']
});
register.registerMetric(httpRequestsTotal);
app.use((req, res, next) => {
httpRequestsTotal.inc({ method: req.method, route: req.route ? req.route.path : '/', code: res.statusCode });
next();
});
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/metrics', (req, res) => {
res.set('Content-Type', register.contentType);
res.send(register.metrics());
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});