Skip to content

Commit fa140cb

Browse files
committed
Add & improve prometheus features on javascript & typescript backends
1 parent 05b997b commit fa140cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2903
-759
lines changed

backend-javascript/.env.development

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ DB_MYSQL_PASSWORD=Trustno1
4040

4141
# === ELASTICSEARCH ===
4242
LOGSTASH_ENABLED=false
43-
ELASTIC_ENABLED=true
43+
ELASTIC_ENABLED=false
4444
ELASTICSEARCH_NODE=http://localhost:9200
4545

4646
# === REDIS ===
4747
REDIS_ENABLED=false
4848
REDIS_REQUIRED=false
49-
REDIS_URL=redis://localhost:6379
49+
REDIS_URL=redis://localhost:6379
50+
51+
# === PROMETHEUS ===
52+
PROMETHEUS_ENABLED=true
53+
# PROMETHEUS_MODE=http_requests_total
54+
PROMETHEUS_MODE=all
55+

backend-javascript/.env.production

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ DB_MYSQL_PASSWORD=Trustno1
4040

4141
# === ELASTICSEARCH ===
4242
LOGSTASH_ENABLED=false
43-
ELASTIC_ENABLED=true
43+
ELASTIC_ENABLED=false
4444
ELASTICSEARCH_NODE=http://localhost:9200
4545

4646
# === REDIS ===
4747
REDIS_ENABLED=false
4848
REDIS_REQUIRED=false
49-
REDIS_URL=redis://localhost:6379
49+
REDIS_URL=redis://localhost:6379
50+
51+
# === PROMETHEUS ===
52+
PROMETHEUS_ENABLED=true
53+
PROMETHEUS_MODE=http_requests_total

backend-javascript/.env.test

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ DB_MYSQL_PASSWORD=Trustno1
3939

4040
# === ELASTICSEARCH ===
4141
LOGSTASH_ENABLED=false
42-
ELASTIC_ENABLED=true
42+
ELASTIC_ENABLED=false
4343
ELASTICSEARCH_NODE=http://localhost:9200
4444

4545
# === REDIS ===
4646
REDIS_ENABLED=false
4747
REDIS_REQUIRED=false
48-
REDIS_URL=redis://localhost:6379
48+
REDIS_URL=redis://localhost:6379
49+
50+
# === PROMETHEUS ===
51+
PROMETHEUS_ENABLED=true
52+
PROMETHEUS_MODE=http_requests_total
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
services:
3+
prometheus:
4+
image: prom/prometheus
5+
container_name: prometheus
6+
volumes:
7+
- prometheus_data:/prometheus
8+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
9+
ports:
10+
- "9090:9090"
11+
12+
volumes:
13+
prometheus_data:
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
global:
2+
scrape_interval: 25s
3+
4+
scrape_configs:
5+
- job_name: 'backend-javascript'
6+
metrics_path: '/metrics'
7+
static_configs:
8+
- targets:
9+
- host.docker.internal:3001
10+
11+
- job_name: 'backend-typescript'
12+
metrics_path: '/metrics'
13+
static_configs:
14+
- targets:
15+
- host.docker.internal:3002

backend-javascript/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
"compose:rebuild-force": "docker-compose down && docker-compose up -d --build --force-recreate",
5555
"compose:monitoring": "docker compose -f docker/docker-compose.monitoring.yml up -d",
5656
"compose:monitoring:clean": "docker compose -f docker/docker-compose.monitoring.yml down --volumes --rmi all",
57-
"compose:logstash": "docker compose -f docker/docker-compose.logstash.yml up -d"
57+
"compose:logstash": "docker compose -f docker/docker-compose.logstash.yml up -d",
58+
"compose:prometheus": "docker compose -f docker/docker-compose.prometheus.yml up -d",
59+
"compose:prometheus:clean": "docker compose -f docker/docker-compose.prometheus.yml down --volumes --rmi all"
5860
},
5961
"keywords": [
6062
"nodejs",

backend-javascript/prometheus.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

backend-javascript/src/app.js

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,23 @@ import compression from 'compression';
33

44
import appConfig from './config/app.config.js';
55

6-
import logger from './infrastructure/logger/logger.js';
7-
8-
import configureSecurity from './middlewares/security/security.js';
9-
106
import initLocals from './middlewares/core/init-locals.js';
7+
import configureSecurity from './middlewares/security/security.js';
8+
import fakeAuth from './middlewares/auth/fake-auth.js';
9+
import correlationIdMiddleware from './middlewares/core/correlation-id.js';
1110

12-
// import redisClient from './core/cache/redis.client.js';
11+
import requestLogger from './infrastructure/logger/request-logger.js';
1312

1413
import notFoundHandler from './middlewares/error/not-found-handler.js';
1514
import responseHandler from './middlewares/response/response-handler.js';
1615
import errorHandler from './middlewares/error/error-handler.js';
1716

18-
import correlationIdMiddleware from './middlewares/core/correlation-id.js';
19-
import requestLogger from './infrastructure/logger/request-logger.js';
20-
21-
import fakeAuth from './middlewares/auth/fake-auth.js';
22-
23-
import metricsRoutes from './routes/metrics.routes.js';
2417
import healthRoutes from './routes/health.routes.js';
18+
import testRoutes from './routes/test.routes.js';
19+
import metricsRoutes from './routes/metrics.routes.js';
2520
import versionRoutes from './routes/version.routes.js';
2621
import swaggerRoutes from './routes/swagger.routes.js';
22+
2723
import appRoutes from './routes/app.routes.js';
2824
import rootRoutes from './routes/root.routes.js';
2925

@@ -43,30 +39,12 @@ if (['development', 'test'].includes(appConfig.app.nodeEnv)) {
4339
app.use(correlationIdMiddleware);
4440
app.use(requestLogger);
4541

46-
app.get('/test-elk', (req, res) => {
47-
logger.info('Test ELK réussi : requête GET /test', { route: '/test' });
48-
console.log('Requête /test : log envoyé vers ELK');
49-
res.send('Log envoyé vers ElasticSearch');
50-
});
51-
52-
// app.get('/test-redis', async (req, res, next) => {
53-
// try {
54-
// await redisClient.set('test-redis', 'Redis is working!');
55-
// const value = await redisClient.get('test-redis');
56-
57-
// console.log('Requête /test-redis : valeur dans Redis =', value);
58-
59-
// res.send(`Redis test : ${value}`);
60-
// } catch (error) {
61-
// next(error);
62-
// }
63-
// });
64-
65-
app.use(metricsRoutes);
6642
app.use(healthRoutes);
67-
43+
app.use(testRoutes);
44+
app.use(metricsRoutes);
6845
app.use(versionRoutes);
6946
app.use(swaggerRoutes);
47+
7048
app.use(appRoutes);
7149
app.use(rootRoutes);
7250

backend-javascript/src/infrastructure/logger/request-logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { incrementHttpRequests } from '../metrics/metrics.js';
44

55
const requestLogger = (req, res, next) => {
66
const startTime = Date.now();
7-
7+
console.log('00000000001:request-logger');
88
res.on('finish', () => {
99
incrementHttpRequests(req.method, req.originalUrl, res.statusCode);
1010

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import client from 'prom-client';
22

3-
client.collectDefaultMetrics();
3+
const PROMETHEUS_ENABLED = process.env.PROMETHEUS_ENABLED === 'true';
44

5-
const httpRequestCounter = new client.Counter({
6-
name: 'http_requests_total',
7-
help: 'Nombre total de requêtes HTTP',
8-
labelNames: ['method', 'route', 'status'],
9-
});
5+
if (PROMETHEUS_ENABLED) {
6+
client.collectDefaultMetrics();
7+
}
8+
9+
let httpRequestCounter = null;
10+
11+
if (PROMETHEUS_ENABLED) {
12+
httpRequestCounter = new client.Counter({
13+
name: 'http_requests_total',
14+
help: 'Nombre total de requêtes HTTP',
15+
labelNames: ['method', 'route', 'status'],
16+
});
17+
}
1018

1119
function incrementHttpRequests(method, route, status) {
12-
httpRequestCounter.inc({ method, route, status });
20+
if (httpRequestCounter) {
21+
httpRequestCounter.inc({
22+
method: method,
23+
route: route,
24+
status: String(status),
25+
});
26+
}
1327
}
1428

1529
export { client, incrementHttpRequests };

0 commit comments

Comments
 (0)