From 26eb6c259b361a03b839c52651b4f2c15f38ff5e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 23 Sep 2024 20:32:24 -0500 Subject: [PATCH] Track deployments in Prometheus --- src/controllers/metrics.ts | 7 ++++++- src/git.ts | 11 +++++++++++ src/metrics.ts | 6 ++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/git.ts diff --git a/src/controllers/metrics.ts b/src/controllers/metrics.ts index 4ef378a0..5b083f56 100644 --- a/src/controllers/metrics.ts +++ b/src/controllers/metrics.ts @@ -1,9 +1,14 @@ import { register } from 'prom-client'; import { AppController } from '@/app.ts'; -import { dbAvailableConnectionsGauge, dbPoolSizeGauge } from '@/metrics.ts'; +import { githash } from '@/git.ts'; +import { dbAvailableConnectionsGauge, dbPoolSizeGauge, deploymentsCounter } from '@/metrics.ts'; import { Storages } from '@/storages.ts'; +if (githash) { + deploymentsCounter.inc({ git_hash: githash }); +} + /** Prometheus/OpenMetrics controller. */ export const metricsController: AppController = async (c) => { const db = await Storages.database(); diff --git a/src/git.ts b/src/git.ts new file mode 100644 index 00000000..7aab2f3c --- /dev/null +++ b/src/git.ts @@ -0,0 +1,11 @@ +let githash: string | undefined; + +try { + const cmd = new Deno.Command('git', { args: ['rev-parse', 'HEAD'] }); + const out = await cmd.output(); + githash = new TextDecoder().decode(out.stdout).trim(); +} catch { + // +} + +export { githash }; diff --git a/src/metrics.ts b/src/metrics.ts index 1e20747b..e1272fcb 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,5 +1,11 @@ import { Counter, Gauge, Histogram } from 'prom-client'; +export const deploymentsCounter = new Counter({ + name: 'ditto_deployments_total', + help: 'Updated on each deployment', + labelNames: ['git_hash'], +}); + export const httpRequestsCounter = new Counter({ name: 'ditto_http_requests_total', help: 'Total number of HTTP requests',