Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions nodejsapp/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}/`);
});