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
77f9a0aecc
commit
40b471e76e
39
index.js
39
index.js
|
|
@ -1,24 +1,43 @@
|
|||
import express from 'express';
|
||||
import client from 'prom-client';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 4000;
|
||||
|
||||
// Crée un registre de métriques Prometheus
|
||||
const register = new client.Registry();
|
||||
client.collectDefaultMetrics({ register });
|
||||
// --- Prometheus setup ---
|
||||
const collectDefaultMetrics = client.collectDefaultMetrics;
|
||||
collectDefaultMetrics(); // CPU, mémoire, etc.
|
||||
|
||||
// Route API
|
||||
app.get('/', (req, res) => {
|
||||
res.send('The Tip Top Backend is running 🚀');
|
||||
const httpRequestCounter = new client.Counter({
|
||||
name: 'http_requests_total',
|
||||
help: 'Nombre total de requêtes HTTP reçues',
|
||||
labelNames: ['method', 'route', 'status'],
|
||||
});
|
||||
|
||||
// Route /metrics pour Prometheus
|
||||
app.use((req, res, next) => {
|
||||
res.on('finish', () => {
|
||||
httpRequestCounter.inc({
|
||||
method: req.method,
|
||||
route: req.route?.path || req.path,
|
||||
status: res.statusCode,
|
||||
});
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
// --- Ton API existante ---
|
||||
app.get('/api', (req, res) => {
|
||||
res.json({ message: 'API The Tip Top 🚀' });
|
||||
});
|
||||
|
||||
// --- Endpoint de métriques ---
|
||||
app.get('/metrics', async (req, res) => {
|
||||
res.set('Content-Type', register.contentType);
|
||||
res.end(await register.metrics());
|
||||
res.set('Content-Type', client.register.contentType);
|
||||
res.end(await client.register.metrics());
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`✅ Backend running on port ${PORT}`);
|
||||
console.log(`🚀 Backend The Tip Top lancé sur le port ${PORT}`);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user