From c4d1a0681ee9de6a8d61f0733fe8336c31a7d02b Mon Sep 17 00:00:00 2001 From: gmega Date: Fri, 20 Jun 2025 17:39:22 -0300 Subject: [PATCH] feat: add basic prometheus dynamic target scaffolding --- src/prometheus.bash | 43 +++++++++++++++++++++++++++++++++++++++ test/test_prometheus.bats | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/prometheus.bash create mode 100644 test/test_prometheus.bats diff --git a/src/prometheus.bash b/src/prometheus.bash new file mode 100644 index 0000000..3e91aa7 --- /dev/null +++ b/src/prometheus.bash @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +set -o pipefail + +LIB_SRC=${LIB_SRC:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)} + +# shellcheck source=./src/config.bash +source "${LIB_SRC}/config.bash" +# shellcheck source=./src/utils.bash +source "${LIB_SRC}/utils.bash" + +_prom_output=${PROM_TARGETS_DIR:-$(clh_output_folder "prometheus")} + +prom_add() { + local metrics_port="$1"\ + experiment_type="$2"\ + experiment_id="$3"\ + node="$4"\ + node_type="$5" + + mkdir -p "${_prom_output}" + + cat > "${_prom_output}/${metrics_port}-${experiment_type}-${experiment_id}-${node}-${node_type}.json" < /dev/null +} + +@test "should create prometheus configurations on start callback" { + prom_add "8290" "experiment" "84858" "node-1" "codex" + + config_file="${_prom_output}/8290-experiment-84858-node-1-codex.json" + + assert [ -f "${config_file}" ] + + assert contains "${config_file}" '"targets": ["host.docker.internal:8290"]' + assert contains "${config_file}" '"job": "experiment"' + assert contains "${config_file}" '"experiment_id": "84858"' + assert contains "${config_file}" '"node": "node-1"' + assert contains "${config_file}" '"node_type": "codex"' +} + +@test "should remove prometheus configurations on stop callback" { + prom_add "8290" "experiment" "84858" "node-1" "codex" + + config_file="${_prom_output}/8290-experiment-84858-node-1-codex.json" + assert [ -f "${config_file}" ] + + prom_remove "8290" "experiment" "84858" "node-1" "codex" + + assert [ ! -f "${config_file}" ] +} \ No newline at end of file