feat: add basic prometheus dynamic target scaffolding

This commit is contained in:
gmega 2025-06-20 17:39:22 -03:00
parent 807f3428ee
commit c4d1a0681e
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
2 changed files with 80 additions and 0 deletions

43
src/prometheus.bash Normal file
View File

@ -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" <<EOF
{
"targets": ["host.docker.internal:${metrics_port}"],
"labels": {
"job": "${experiment_type}",
"experiment_id": "${experiment_id}",
"node": "${node}",
"node_type": "${node_type}"
}
}
EOF
}
prom_remove() {
local metrics_port="$1"\
experiment_type="$2"\
experiment_id="$3"\
node="$4"\
node_type="$5"
rm "${_prom_output}/${metrics_port}-${experiment_type}-${experiment_id}-${node}-${node_type}.json" || true
}

37
test/test_prometheus.bats Normal file
View File

@ -0,0 +1,37 @@
setup() {
load test_helper/common_setup
common_setup
# shellcheck source=./src/prometheus.bash
source "${LIB_SRC}/prometheus.bash"
}
contains() {
local file="$1" pattern="$2"
grep -F "$pattern" "$file" > /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}" ]
}