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 express from 'express';
|
||||||
import client from 'prom-client';
|
import client from 'prom-client';
|
||||||
|
import dotenv from 'dotenv';
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 4000;
|
const PORT = process.env.PORT || 4000;
|
||||||
|
|
||||||
// Crée un registre de métriques Prometheus
|
// --- Prometheus setup ---
|
||||||
const register = new client.Registry();
|
const collectDefaultMetrics = client.collectDefaultMetrics;
|
||||||
client.collectDefaultMetrics({ register });
|
collectDefaultMetrics(); // CPU, mémoire, etc.
|
||||||
|
|
||||||
// Route API
|
const httpRequestCounter = new client.Counter({
|
||||||
app.get('/', (req, res) => {
|
name: 'http_requests_total',
|
||||||
res.send('The Tip Top Backend is running 🚀');
|
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) => {
|
app.get('/metrics', async (req, res) => {
|
||||||
res.set('Content-Type', register.contentType);
|
res.set('Content-Type', client.register.contentType);
|
||||||
res.end(await register.metrics());
|
res.end(await client.register.metrics());
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
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