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