diff --git a/mtproxymax.sh b/mtproxymax.sh index b0ac44b..3a00881 100644 --- a/mtproxymax.sh +++ b/mtproxymax.sh @@ -1622,8 +1622,8 @@ get_proxy_stats() { local m if m=$(_fetch_metrics); then local bi bo conns - bi=$(echo "$m" | awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}') - bo=$(echo "$m" | awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}') + bi=$(echo "$m" | awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}') + bo=$(echo "$m" | awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}') conns=$(echo "$m" | awk '/^telemt_user_connections_current\{/{s+=$NF}END{printf "%.0f",s}') echo "${bi:-0} ${bo:-0} ${conns:-0}" return @@ -1685,8 +1685,8 @@ get_user_stats() { local m if m=$(_fetch_metrics); then local i o c - i=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_from_client\\{.*user=\"" u "\"" {print $NF}') - o=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_to_client\\{.*user=\"" u "\"" {print $NF}') + i=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_from_client_total\\{.*user=\"" u "\"" {print $NF}') + o=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_to_client_total\\{.*user=\"" u "\"" {print $NF}') c=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_connections_current\\{.*user=\"" u "\"" {print $NF}') echo "${i:-0} ${o:-0} ${c:-0}" return @@ -1747,10 +1747,10 @@ _load_all_cumulative_user_stats() { q = index(s, "\"") return q ? substr(s, 1, q - 1) : "" } - /^telemt_user_octets_from_client\{/ { + /^telemt_user_octets_from_client_total\{/ { u = get_user($0); if (u) users_in[u] += $NF } - /^telemt_user_octets_to_client\{/ { + /^telemt_user_octets_to_client_total\{/ { u = get_user($0); if (u) users_out[u] += $NF } /^telemt_user_connections_current\{/ { @@ -1834,8 +1834,8 @@ flush_traffic_to_disk() { if $_have_metrics; then # Global traffic delta local cur_gin cur_gout - cur_gin=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}') - cur_gout=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}') + cur_gin=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}') + cur_gout=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}') cur_gin=${cur_gin:-0}; cur_gout=${cur_gout:-0} local gd_in=$((cur_gin - snap_gin)) gd_out=$((cur_gout - snap_gout)) [ "$gd_in" -lt 0 ] 2>/dev/null && gd_in=$cur_gin @@ -1868,8 +1868,8 @@ flush_traffic_to_disk() { [[ "$label" =~ ^# ]] && continue; [ -z "$secret" ] && continue [ "$enabled" != "true" ] && continue local ui uo - ui=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client\\{.*user=\"" u "\"" {print $NF}') - uo=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client\\{.*user=\"" u "\"" {print $NF}') + ui=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client_total\\{.*user=\"" u "\"" {print $NF}') + uo=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client_total\\{.*user=\"" u "\"" {print $NF}') ui=${ui:-0}; uo=${uo:-0} local si=${_fu_snap_in["$label"]:-0} so=${_fu_snap_out["$label"]:-0} local di=$((ui - si)) doo=$((uo - so)) @@ -2630,8 +2630,8 @@ secret_reset_traffic() { q = index(s, "\"") return q ? substr(s, 1, q - 1) : "" } - /^telemt_user_octets_from_client\{/ { u = get_user($0); if (u) in_oct[u] += $NF } - /^telemt_user_octets_to_client\{/ { u = get_user($0); if (u) out_oct[u] += $NF } + /^telemt_user_octets_from_client_total\{/ { u = get_user($0); if (u) in_oct[u] += $NF } + /^telemt_user_octets_to_client_total\{/ { u = get_user($0); if (u) out_oct[u] += $NF } END { for (u in in_oct) { printf "%s|%.0f|%.0f\n", u, in_oct[u], out_oct[u] @@ -2663,8 +2663,8 @@ secret_reset_traffic() { # Update user_traffic_snapshot to exact current live Prometheus values so delta becomes 0 right now local live_in=0 live_out=0 - live_in=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}') - live_out=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}') + live_in=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client_total\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}') + live_out=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client_total\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}') [[ "${live_in:-0}" =~ ^[0-9]+$ ]] || live_in=0 [[ "${live_out:-0}" =~ ^[0-9]+$ ]] || live_out=0 if [ -f "$_snap" ]; then @@ -2949,11 +2949,14 @@ show_connections() { /^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF } /^telemt_user_connections_total\{/ { u=lbl($0,"user"); if(u) ut[u]+=$NF } /^telemt_user_unique_ips_current\{/ { u=lbl($0,"user"); if(u) ui[u]+=$NF } - /^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } - /^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } - /^telemt_connections_current / { total=$NF } + /^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } + /^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } END { - for (u in uc) users[u]=1 + # Global active connections are the sum of the per-user gauge. The aggregate + # telemt_connections_current was deleted in telemt c07b600, so reading it here + # left "Total active" at 0 for every 3.5.x release. + # See tests/test_metric_names.sh for the upstream history. + for (u in uc) { users[u]=1; total+=uc[u] } for (u in ut) users[u]=1 for (u in ui) users[u]=1 for (u in rx) users[u]=1 @@ -3079,8 +3082,8 @@ secret_stats() { q = index(s, "\""); return q ? substr(s, 1, q-1) : "" } /^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF } - /^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } - /^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } + /^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } + /^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } /^telemt_user_unique_ips_current\{/ { u=lbl($0,"user"); if(u) ip[u]+=$NF } END { for (u in uc) printf "%s|%.0f|%.0f|%.0f|%.0f\n", u, uc[u]+0, rx[u]+0, tx[u]+0, ip[u]+0 } ') @@ -3426,8 +3429,8 @@ secret_info() { local live; live=$(echo "$m" | awk -v u="$label" ' function lbl(s, k, p, q) { p=index(s,k"=\""); if(!p) return ""; s=substr(s,p+length(k)+2); q=index(s,"\""); return q ? substr(s,1,q-1) : "" } /^telemt_user_connections_current\{/ { if(lbl($0,"user")==u) c+=$NF } - /^telemt_user_octets_from_client\{/ { if(lbl($0,"user")==u) rx+=$NF } - /^telemt_user_octets_to_client\{/ { if(lbl($0,"user")==u) tx+=$NF } + /^telemt_user_octets_from_client_total\{/ { if(lbl($0,"user")==u) rx+=$NF } + /^telemt_user_octets_to_client_total\{/ { if(lbl($0,"user")==u) tx+=$NF } /^telemt_user_unique_ips_current\{/ { if(lbl($0,"user")==u) ip+=$NF } END { printf "%.0f|%.0f|%.0f|%.0f", c+0, rx+0, tx+0, ip+0 } ') @@ -3623,8 +3626,8 @@ secret_top() { parsed=$(echo "$m" | awk ' function lbl(s, k, p, q) { p=index(s,k"=\""); if(!p) return ""; s=substr(s,p+length(k)+2); q=index(s,"\""); return q ? substr(s,1,q-1) : "" } /^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF } - /^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } - /^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } + /^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } + /^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } END { for(u in uc) printf "%s|%.0f|%.0f|%.0f\n", u, uc[u]+0, rx[u]+0, tx[u]+0 } ') @@ -5138,8 +5141,8 @@ run_upload_test() { _metrics=$(fetch_metrics 2>/dev/null || true) if [ -n "$_metrics" ]; then local up_bytes down_bytes - up_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}') - down_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}') + up_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}') + down_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}') local up_fmt; up_fmt=$(format_bytes "${up_bytes:-0}") local down_fmt; down_fmt=$(format_bytes "${down_bytes:-0}") @@ -6621,8 +6624,8 @@ run_traffic_reset_global() { return 1 fi local cur_in=0 cur_out=0 - cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}') - cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}') + cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}') + cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}') cur_in=${cur_in:-0}; cur_out=${cur_out:-0} log_info "Resetting cumulative counters..." @@ -11415,8 +11418,8 @@ get_stats() { local m=$(curl -s --max-time 2 "http://127.0.0.1:${PROXY_METRICS_PORT:-9090}/metrics" 2>/dev/null) [ -z "$m" ] && echo "0 0 0" && return echo "$m" | awk ' - /^telemt_user_octets_from_client\{/ {i+=$NF} - /^telemt_user_octets_to_client\{/ {o+=$NF} + /^telemt_user_octets_from_client_total\{/ {i+=$NF} + /^telemt_user_octets_to_client_total\{/ {o+=$NF} /^telemt_user_connections_current\{/ {c+=$NF} END {printf "%.0f %.0f %.0f\n",i+0,o+0,c+0} ' @@ -11583,8 +11586,8 @@ update_traffic() { _metrics=$(curl -s --max-time 2 "http://127.0.0.1:${PROXY_METRICS_PORT:-9090}/metrics" 2>/dev/null) || true [ -z "$_metrics" ] && return 0 local cur_in cur_out - cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}') - cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}') + cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}') + cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}') cur_in=${cur_in:-0}; cur_out=${cur_out:-0} # Compute deltas (torware pattern: detect container restart by negative delta) @@ -11604,8 +11607,8 @@ update_traffic() { [ -n "$_pu" ] && { _parsed_ui["$_pu"]=${_pi:-0}; _parsed_uo["$_pu"]=${_po:-0}; } done < <(echo "$_metrics" | awk ' function lbl(s, k, p, q) { p=index(s,k"=\""); if(!p) return ""; s=substr(s,p+length(k)+2); q=index(s,"\""); return q ? substr(s,1,q-1) : "" } - /^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } - /^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } + /^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } + /^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } END { for(u in rx) printf "%s|%.0f|%.0f\n",u,rx[u]+0,tx[u]+0 } ') fi @@ -14360,9 +14363,9 @@ show_metrics() { /^telemt_uptime_seconds / { uptime = $NF } /^telemt_connections_total / { c_tot = $NF } /^telemt_connections_bad_total / { c_bad = $NF } - /^telemt_connections_current / { c_cur = $NF } - /^telemt_connections_me_current / { c_me = $NF } - /^telemt_connections_direct_current / { c_dir = $NF } + # telemt_connections_current, ..._me_current and ..._direct_current were deleted + # in telemt c07b600; the aggregate is now derived from the per-user gauge below. + # See tests/test_metric_names.sh for the upstream history. /^telemt_upstream_connect_attempt_total / { up_att = $NF } /^telemt_upstream_connect_success_total / { up_ok = $NF } /^telemt_upstream_connect_fail_total / { up_fail= $NF } @@ -14379,12 +14382,13 @@ show_metrics() { /^telemt_upstream_connect_duration_fail_total\{/ { b=lbl($0,"bucket"); if(b) df[b]+=$NF } /^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF } /^telemt_user_connections_total\{/ { u=lbl($0,"user"); if(u) ut[u]+=$NF } - /^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } - /^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } + /^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF } + /^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF } /^telemt_user_unique_ips_current\{/ { u=lbl($0,"user"); if(u) ui[u]+=$NF } END { - printf "S|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f\n", - uptime+0,c_tot+0,c_bad+0,c_cur+0,c_me+0,c_dir+0, + for (u in uc) c_cur += uc[u] + printf "S|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f\n", + uptime+0,c_tot+0,c_bad+0,c_cur+0, up_att+0,up_ok+0,up_fail+0,me_att+0,me_ok+0, me_wa+0,me_ww+0,me_quar+0,me_crc+0,pool+0,desync+0,padinv+0 bkeys[1]="le_100ms"; bnames[1]="<=100ms" @@ -14405,8 +14409,8 @@ show_metrics() { ') # Parse scalar line - local uptime c_tot c_bad c_cur c_me c_dir up_att up_ok up_fail me_att me_ok me_wa me_ww me_quar me_crc pool desync padinv - IFS='|' read -r _ uptime c_tot c_bad c_cur c_me c_dir up_att up_ok up_fail \ + local uptime c_tot c_bad c_cur up_att up_ok up_fail me_att me_ok me_wa me_ww me_quar me_crc pool desync padinv + IFS='|' read -r _ uptime c_tot c_bad c_cur up_att up_ok up_fail \ me_att me_ok me_wa me_ww me_quar me_crc pool desync padinv \ <<< "$(echo "$parsed" | grep '^S|')" @@ -14435,7 +14439,10 @@ show_metrics() { echo -e " ${BOLD}Connections${NC}" echo -e " ${DIM}total:${NC} ${c_tot:-0} ${DIM}authorized:${NC} ${BRIGHT_GREEN}${c_good}${NC} ${DIM}rejected:${NC} ${BRIGHT_RED}${c_bad:-0}${NC}" - echo -e " ${DIM}active:${NC} ${c_cur:-0} (ME: ${c_me:-0} direct: ${c_dir:-0})" + # The ME / direct breakdown is gone: telemt removed those gauges in c07b600 and + # exposes no per-transport current-connection metric any more, so printing + # "(ME: 0 direct: 0)" forever would just assert a number that was never measured. + echo -e " ${DIM}active:${NC} ${c_cur:-0}" echo "" echo -e " ${BOLD}Upstream${NC}" diff --git a/tests/test_metric_names.sh b/tests/test_metric_names.sh new file mode 100644 index 0000000..e5c3fde --- /dev/null +++ b/tests/test_metric_names.sh @@ -0,0 +1,129 @@ +#!/bin/bash +# Regression tests for the Prometheus counter names the script scrapes. +# +# Every assertion here exists because of a specific upstream telemt change. The failure +# mode they guard is silent by construction: an awk sum over a pattern that matches +# nothing yields 0 rather than an error, so a stale metric name shows up as a plausible +# zero instead of as a problem. +# +# ── 1. Conventional _total suffix on the user octet counters ────────────────────── +# +# telemt commit ede3314bee356339dca4ca1b378c251ebac15358 — 2026-08-01 +# "Fix name metric counter" +# https://github.com/telemt/telemt/commit/ede3314bee356339dca4ca1b378c251ebac15358 +# +# First released in telemt 3.5.4 (the names are absent in 3.5.0–3.5.3). It renamed +# telemt_user_octets_from_client -> telemt_user_octets_from_client_total and the +# _to_client pair likewise. The old spellings were removed in the same commit, so the +# two never coexisted in any release — which is why MTProxyMax matches the _total +# spelling exactly rather than using a tolerant `(_total)?` pattern that would +# double-count if both ever appeared together. +# +# MTProxyMax required `{` immediately after `client`, so from 3.5.4 on nothing matched +# and every traffic figure across the manager read 0. +# +# ── 2. Removal of the aggregate connection gauges ───────────────────────────────── +# +# telemt commit c07b600acb6bb59762bd96af6ce5b7fa90ec9de1 — 2026-03-19 +# "Integration hardening: reconcile main+flow-sec API drift and restore green suite" +# https://github.com/telemt/telemt/commit/c07b600acb6bb59762bd96af6ce5b7fa90ec9de1 +# +# It deleted telemt_connections_current, telemt_connections_me_current and +# telemt_connections_direct_current outright. That predates 3.5.0, so they are absent +# from every 3.5.x release — the aggregate now has to be derived from the per-user +# gauge telemt_user_connections_current. +# +# Note these were referenced with a trailing space (`/^name /`), not `{`, which is why +# a sweep of `telemt_*{` patterns does not find them. +set -o pipefail + +if [ "${BASH_VERSINFO[0]:-0}" -lt 4 ]; then + echo "SKIP: bash 4+ required (got ${BASH_VERSION:-unknown})" >&2 + exit 0 +fi + +TEST_TMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mtp_metrics_XXXXXX') +INSTALL_DIR="$TEST_TMPDIR/install" +mkdir -p "$INSTALL_DIR/relay_stats" +REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) + +MTPROXYMAX_SOURCE_ONLY=true source "$REPO_ROOT/mtproxymax.sh" +set +e +trap 'rm -rf "$TEST_TMPDIR"' EXIT + +TESTS_RUN=0 +TESTS_FAILED=0 + +assert_eq() { + local name="$1" want="$2" got="$3" + TESTS_RUN=$((TESTS_RUN + 1)) + if [ "$got" = "$want" ]; then + printf ' PASS %s\n' "$name" + else + printf ' FAIL %s (got=%q want=%q)\n' "$name" "$got" "$want" + TESTS_FAILED=$((TESTS_FAILED + 1)) + fi +} + +# A realistic scrape: telemt emits the _total spelling and the per-user connection gauge, +# and none of the three deleted aggregates. +METRICS='# HELP telemt_user_octets_from_client_total Bytes received from clients +# TYPE telemt_user_octets_from_client_total counter +telemt_user_octets_from_client_total{user="alice"} 801502131 +telemt_user_octets_to_client_total{user="alice"} 39774071544 +telemt_user_octets_from_client_total{user="bob"} 100 +telemt_user_octets_to_client_total{user="bob"} 200 +telemt_user_connections_current{user="alice"} 7 +telemt_user_connections_current{user="bob"} 5 +telemt_connections_total 42 +telemt_connections_bad_total 2' + +_fetch_metrics() { printf '%s\n' "$METRICS"; } +is_proxy_running() { return 0; } +_load_all_cumulative_user_stats() { :; } +draw_header() { :; } +log_error() { :; } + +echo "telemt metric name tests" + +# The fixture must really contain the names under test, or everything below could pass +# for the wrong reason. +assert_eq "fixture carries the _total counter names" "4" \ + "$(printf '%s\n' "$METRICS" | grep -c '^telemt_user_octets_.*_total{')" + +# --- 1. the _total rename ------------------------------------------------------ +assert_eq "global totals come from the _total counters" \ + "801502231 39774071744 12" "$(get_proxy_stats)" + +assert_eq "per-user totals come from the _total counters" \ + "801502131 39774071544 7" "$(get_user_stats alice)" + +# The connection figures in the two assertions above come from +# telemt_user_connections_current, which telemt never renamed — so they double as the +# check that an unrenamed counter still parses, guarding against "fixing" the whole +# family instead of the two that actually changed. + +# A tolerant `(_total)?` pattern would double-count if both spellings ever coexisted. +# They did not, but the exclusive behaviour is what we depend on, so pin it. +_fetch_metrics() { + printf '%s\n' "$METRICS" + printf '%s\n' 'telemt_user_octets_from_client{user="legacy"} 999999' +} +assert_eq "a legacy-named line is not also counted" "801502231" \ + "$(get_proxy_stats | awk '{print $1}')" +_fetch_metrics() { printf '%s\n' "$METRICS"; } + +# --- 2. the deleted aggregate connection gauges -------------------------------- +# Derived from the per-user gauge, so this must be 7 + 5 and not the stale aggregate. +# $NF rather than a digit match: the label is wrapped in ANSI colour codes, whose escape +# sequences contain digits, so `grep -oE '[0-9]+'` would pick up "[1" from "\e[1m". +assert_eq "active connection total is summed from the per-user gauge" "12" \ + "$(show_connections 2>/dev/null | grep 'Total active:' | awk '{print $NF}')" + +# The three deleted names were matched with a trailing space, so they need a source-level +# guard: nothing at runtime can distinguish "metric absent" from "zero connections". +assert_eq "no pattern still reads the deleted aggregate gauges" "0" \ + "$(grep -cE '\^telemt_connections_(me_|direct_)?current ' "$REPO_ROOT/mtproxymax.sh")" + +printf '\n%d tests, %d failures\n' "$TESTS_RUN" "$TESTS_FAILED" +[ "$TESTS_FAILED" -eq 0 ] diff --git a/tests/test_traffic_reset.sh b/tests/test_traffic_reset.sh index 01cdc84..9b5512d 100644 --- a/tests/test_traffic_reset.sh +++ b/tests/test_traffic_reset.sh @@ -41,10 +41,10 @@ SECRETS_LABELS=(alice bob) SECRETS_ENABLED=(true true) METRICS='# HELP test test -telemt_user_octets_from_client{user="alice"} 120 -telemt_user_octets_to_client{user="alice"} 340 -telemt_user_octets_from_client{user="bob"} 50 -telemt_user_octets_to_client{user="bob"} 70' +telemt_user_octets_from_client_total{user="alice"} 120 +telemt_user_octets_to_client_total{user="alice"} 340 +telemt_user_octets_from_client_total{user="bob"} 50 +telemt_user_octets_to_client_total{user="bob"} 70' _fetch_metrics() { [ "${METRICS_AVAILABLE:-true}" = "true" ] || return 1