Skip to content

Commit 4758eab

Browse files
committed
fix: add metrics
1 parent b115308 commit 4758eab

File tree

14 files changed

+132
-0
lines changed

14 files changed

+132
-0
lines changed

apps/auth-service/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AppModule } from './app.module';
33
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
44
import { ConfigService } from '@nestjs/config';
55
import { Configuration } from '@leetcode/config';
6+
import { startMetricsServer } from './metrics-server';
7+
import { register } from 'prom-client';
68

79
async function bootstrap() {
810
const appContext = await NestFactory.createApplicationContext(AppModule);
@@ -15,6 +17,9 @@ async function bootstrap() {
1517
port: configService.get('auth_service_port'),
1618
},
1719
});
20+
21+
startMetricsServer(register, configService.get('auth_service_port')!);
22+
1823
await app.listen();
1924
}
2025
bootstrap();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
import { Registry } from 'prom-client';
3+
4+
export function startMetricsServer(registry: Registry, port: number) {
5+
const app = express();
6+
7+
app.get('/metrics', async (_req, res) => {
8+
res.set('Content-Type', registry.contentType);
9+
res.send(await registry.metrics());
10+
});
11+
12+
app.listen(port, () => {
13+
console.log(`Prometheus metrics on port ${port}/metrics`);
14+
});
15+
}

apps/companies-service/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AppModule } from './app.module';
33
import { ConfigService } from '@nestjs/config';
44
import { Configuration } from '@leetcode/config';
55
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
6+
import { register } from 'prom-client';
7+
import { startMetricsServer } from './metrics-server';
68

79
async function bootstrap() {
810
const appContext = await NestFactory.createApplicationContext(AppModule);
@@ -15,6 +17,9 @@ async function bootstrap() {
1517
port: configService.get('companies_service_port'),
1618
},
1719
});
20+
21+
startMetricsServer(register, configService.get('companies_service_port')!);
22+
1823
await app.listen();
1924
}
2025
bootstrap();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
import { Registry } from 'prom-client';
3+
4+
export function startMetricsServer(registry: Registry, port: number) {
5+
const app = express();
6+
7+
app.get('/metrics', async (_req, res) => {
8+
res.set('Content-Type', registry.contentType);
9+
res.send(await registry.metrics());
10+
});
11+
12+
app.listen(port, () => {
13+
console.log(`Prometheus metrics on port ${port}/metrics`);
14+
});
15+
}

apps/execution-service/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AppModule } from './app.module';
33
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
44
import { ConfigService } from '@nestjs/config';
55
import { Configuration } from '@leetcode/config';
6+
import { register } from 'prom-client';
7+
import { startMetricsServer } from './metrics-server';
68

79
async function bootstrap() {
810
const appContext = await NestFactory.createApplicationContext(AppModule);
@@ -15,6 +17,9 @@ async function bootstrap() {
1517
port: configService.get('execution_service_port'),
1618
},
1719
});
20+
21+
startMetricsServer(register, configService.get('execution_service_port')!);
22+
1823
await app.listen();
1924
}
2025
bootstrap();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
import { Registry } from 'prom-client';
3+
4+
export function startMetricsServer(registry: Registry, port: number) {
5+
const app = express();
6+
7+
app.get('/metrics', async (_req, res) => {
8+
res.set('Content-Type', registry.contentType);
9+
res.send(await registry.metrics());
10+
});
11+
12+
app.listen(port, () => {
13+
console.log(`Prometheus metrics on port ${port}/metrics`);
14+
});
15+
}

apps/problems-service/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AppModule } from './app.module';
33
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
44
import { ConfigService } from '@nestjs/config';
55
import { Configuration } from '@leetcode/config';
6+
import { startMetricsServer } from './metrics-server';
7+
import { register } from 'prom-client';
68

79
async function bootstrap() {
810
const appContext = await NestFactory.createApplicationContext(AppModule);
@@ -15,6 +17,7 @@ async function bootstrap() {
1517
port: configService.get('problems_service_port'),
1618
},
1719
});
20+
startMetricsServer(register, configService.get('problems_service_port')!);
1821
await app.listen();
1922
}
2023
bootstrap();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
import { Registry } from 'prom-client';
3+
4+
export function startMetricsServer(registry: Registry, port: number) {
5+
const app = express();
6+
7+
app.get('/metrics', async (_req, res) => {
8+
res.set('Content-Type', registry.contentType);
9+
res.send(await registry.metrics());
10+
});
11+
12+
app.listen(port, () => {
13+
console.log(`Prometheus metrics on port ${port}/metrics`);
14+
});
15+
}

apps/submissions-service/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AppModule } from './app.module';
33
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
44
import { ConfigService } from '@nestjs/config';
55
import { Configuration } from '@leetcode/config';
6+
import { register } from 'prom-client';
7+
import { startMetricsServer } from './metrics-server';
68

79
async function bootstrap() {
810
const appContext = await NestFactory.createApplicationContext(AppModule);
@@ -15,6 +17,7 @@ async function bootstrap() {
1517
port: configService.get('submissions_service_port'),
1618
},
1719
});
20+
startMetricsServer(register, configService.get('submissions_service_port')!);
1821
await app.listen();
1922
}
2023
bootstrap();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
import { Registry } from 'prom-client';
3+
4+
export function startMetricsServer(registry: Registry, port: number) {
5+
const app = express();
6+
7+
app.get('/metrics', async (_req, res) => {
8+
res.set('Content-Type', registry.contentType);
9+
res.send(await registry.metrics());
10+
});
11+
12+
app.listen(port, () => {
13+
console.log(`Prometheus metrics on port ${port}/metrics`);
14+
});
15+
}

0 commit comments

Comments
 (0)