|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +HEALTHCHECK_ENV_FILE=/tmp/statsd_exporter_healthcheck.env |
| 6 | +STATSD_EXPORTER_BIN=${STATSD_EXPORTER_BIN:-/bin/statsd_exporter} |
| 7 | + |
| 8 | +telemetry_path="/metrics" |
| 9 | +listen_port="9102" |
| 10 | +listen_address="" |
| 11 | +pending_flag="" |
| 12 | + |
| 13 | +# Parse both formats --flag=value and --flag value forms without altering original args. |
| 14 | +for arg in "$@"; do |
| 15 | + case "$pending_flag" in |
| 16 | + telemetry_path) |
| 17 | + telemetry_path="$arg" |
| 18 | + pending_flag="" |
| 19 | + continue |
| 20 | + ;; |
| 21 | + listen_address) |
| 22 | + listen_address="$arg" |
| 23 | + pending_flag="" |
| 24 | + continue |
| 25 | + ;; |
| 26 | + esac |
| 27 | + |
| 28 | + case "$arg" in |
| 29 | + --web.telemetry-path=*) |
| 30 | + telemetry_path=${arg#*=} |
| 31 | + ;; |
| 32 | + --web.telemetry-path) |
| 33 | + pending_flag="telemetry_path" |
| 34 | + ;; |
| 35 | + --web.listen-address=*) |
| 36 | + listen_address=${arg#*=} |
| 37 | + ;; |
| 38 | + --web.listen-address) |
| 39 | + pending_flag="listen_address" |
| 40 | + ;; |
| 41 | + esac |
| 42 | +done |
| 43 | + |
| 44 | +# Ensure telemetry path is non-empty and absolute for URL construction. |
| 45 | +if [ -z "$telemetry_path" ]; then |
| 46 | + telemetry_path="/metrics" |
| 47 | +elif [ "${telemetry_path#/}" = "$telemetry_path" ]; then |
| 48 | + telemetry_path="/$telemetry_path" |
| 49 | +fi |
| 50 | + |
| 51 | +# Derive port from the final :PORT segment (works for host:port and [::]:port). |
| 52 | +if [ -n "$listen_address" ]; then |
| 53 | + port_candidate=${listen_address##*:} |
| 54 | + case "$port_candidate" in |
| 55 | + ''|*[!0-9]*) |
| 56 | + ;; |
| 57 | + *) |
| 58 | + if [ "$port_candidate" -ge 1 ] 2>/dev/null && [ "$port_candidate" -le 65535 ] 2>/dev/null; then |
| 59 | + listen_port="$port_candidate" |
| 60 | + fi |
| 61 | + ;; |
| 62 | + esac |
| 63 | +fi |
| 64 | + |
| 65 | +export TELEMETRY_PATH="$telemetry_path" |
| 66 | +export LISTEN_PORT="$listen_port" |
| 67 | + |
| 68 | +# Persist parsed values so Docker healthcheck invocations can read them later. |
| 69 | +tmp_env_file="${HEALTHCHECK_ENV_FILE}.tmp" |
| 70 | +{ |
| 71 | + printf 'TELEMETRY_PATH=%s\n' "$TELEMETRY_PATH" |
| 72 | + printf 'LISTEN_PORT=%s\n' "$LISTEN_PORT" |
| 73 | +} >"$tmp_env_file" |
| 74 | +mv "$tmp_env_file" "$HEALTHCHECK_ENV_FILE" |
| 75 | + |
| 76 | +exec "$STATSD_EXPORTER_BIN" "$@" |
0 commit comments