Actualiser index.js
All checks were successful
the-tip-top-backend/pipeline/head This commit looks good
All checks were successful
the-tip-top-backend/pipeline/head This commit looks good
This commit is contained in:
parent
a029f15105
commit
77f9a0aecc
35
index.js
35
index.js
|
|
@ -2,30 +2,23 @@ import express from 'express';
|
||||||
import client from 'prom-client';
|
import client from 'prom-client';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const collectDefaultMetrics = client.collectDefaultMetrics;
|
const PORT = process.env.PORT || 4000;
|
||||||
collectDefaultMetrics({ prefix: 'thetiptop_' });
|
|
||||||
|
|
||||||
// Exemple de compteur HTTP
|
// Crée un registre de métriques Prometheus
|
||||||
const httpRequestCounter = new client.Counter({
|
const register = new client.Registry();
|
||||||
name: 'thetiptop_http_requests_total',
|
client.collectDefaultMetrics({ register });
|
||||||
help: 'Nombre total de requêtes HTTP',
|
|
||||||
labelNames: ['method', 'route', 'status_code']
|
// Route API
|
||||||
});
|
app.get('/', (req, res) => {
|
||||||
|
res.send('The Tip Top Backend is running 🚀');
|
||||||
app.use((req, res, next) => {
|
|
||||||
res.on('finish', () => {
|
|
||||||
httpRequestCounter.inc({
|
|
||||||
method: req.method,
|
|
||||||
route: req.route ? req.route.path : req.path,
|
|
||||||
status_code: res.statusCode
|
|
||||||
});
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Route /metrics pour Prometheus
|
||||||
app.get('/metrics', async (req, res) => {
|
app.get('/metrics', async (req, res) => {
|
||||||
res.set('Content-Type', client.register.contentType);
|
res.set('Content-Type', register.contentType);
|
||||||
res.end(await client.register.metrics());
|
res.end(await register.metrics());
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(4000, () => console.log('Backend en écoute sur le port 4000'));
|
app.listen(PORT, () => {
|
||||||
|
console.log(`✅ Backend running on port ${PORT}`);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user