Skip to content

Commit 085ca5a

Browse files
committed
containers_running metric
1 parent 87c1be4 commit 085ca5a

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

prometheus.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ scrape_configs:
44
- targets: ["host.docker.internal:3001"]
55
metrics_path: /metrics
66
bearer_token: "5678"
7+
scrape_interval: 1s

src/lib/docker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { resolve } from "path";
22
import { DEFAULT_CONFIG as CONFIG, DOCKER_CONFIG } from "../config";
33
import { type Config } from "./validation";
4+
import { incrementContainers, decrementContainers } from "./prometheus";
45

56
// Track active containers
67
let activeContainers = new Set<string>();
@@ -47,10 +48,15 @@ export function getContainerName(sessionId: string): string {
4748

4849
export function trackContainer(containerName: string): void {
4950
activeContainers.add(containerName);
51+
incrementContainers();
5052
}
5153

5254
export function untrackContainer(containerName: string): void {
5355
activeContainers.delete(containerName);
56+
// This is for prometheus to be able to track the container
57+
setTimeout(() => {
58+
decrementContainers();
59+
}, 1200); // Scrape interval = 1000, 200 is for flexibility
5460
}
5561

5662
export async function cleanupContainer(containerName: string): Promise<void> {
@@ -67,6 +73,7 @@ export async function cleanupContainer(containerName: string): Promise<void> {
6773
stderr: "ignore",
6874
});
6975
activeContainers.delete(containerName);
76+
decrementContainers();
7077
} catch (err) {
7178
// This is non-crucial as containers have the --rm flag
7279
console.error(`Error cleaning up container ${containerName}:`, err);

src/lib/prometheus.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1+
import { Registry, collectDefaultMetrics, Gauge } from "prom-client";
12
import { prometheus } from "@hono/prometheus";
23

3-
const { printMetrics, registerMetrics } = prometheus({
4-
collectDefaultMetrics: true,
4+
// Create custom registry
5+
const register = new Registry();
6+
collectDefaultMetrics({ register });
7+
8+
const prom = prometheus({
9+
collectDefaultMetrics: false, // We handle it manually
10+
registry: register,
511
});
612

13+
const { printMetrics, registerMetrics } = prom;
14+
15+
// Custom gauge for running containers
16+
const containersRunning = new Gauge({
17+
name: "containers_running",
18+
help: "Number of currently running containers",
19+
registers: [register],
20+
});
21+
22+
register.registerMetric(containersRunning);
23+
24+
// Initialize to 0 to ensure it appears in metrics
25+
containersRunning.set(0);
26+
27+
export function incrementContainers() {
28+
containersRunning.inc();
29+
}
30+
31+
export function decrementContainers() {
32+
containersRunning.dec();
33+
}
34+
35+
export function setContainers(count: number) {
36+
containersRunning.set(count);
37+
}
38+
739
export { printMetrics, registerMetrics };

0 commit comments

Comments
 (0)