Skip to content

Commit eb9b3c8

Browse files
committed
fix: Use values from user args when running Docker healthcheck
Signed-off-by: Vikas Shah <vikas.shah@ocado.com>
1 parent 99e253f commit eb9b3c8

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ LABEL maintainer="The Prometheus Authors <prometheus-developers@googlegroups.com
66
ARG ARCH="amd64"
77
ARG OS="linux"
88
COPY .build/${OS}-${ARCH}/statsd_exporter /bin/statsd_exporter
9+
COPY docker-entrypoint.sh /bin/docker-entrypoint.sh
10+
RUN chmod +x /bin/docker-entrypoint.sh
11+
12+
ENV TELEMETRY_PATH=/metrics \
13+
LISTEN_PORT=9102
914

1015
USER 65534
1116
EXPOSE 9102 9125 9125/udp
12-
HEALTHCHECK CMD wget --spider -S "http://localhost:9102/metrics" -T 60 2>&1 || exit 1
13-
ENTRYPOINT [ "/bin/statsd_exporter" ]
17+
HEALTHCHECK CMD if [ -r /tmp/statsd_exporter_healthcheck.env ]; then LISTEN_PORT="$(sed -n 's/^LISTEN_PORT=//p' /tmp/statsd_exporter_healthcheck.env | tail -n 1)"; TELEMETRY_PATH="$(sed -n 's/^TELEMETRY_PATH=//p' /tmp/statsd_exporter_healthcheck.env | tail -n 1)"; fi; wget --spider -S "http://localhost:${LISTEN_PORT:-9102}${TELEMETRY_PATH:-/metrics}" -T 60 2>&1 || exit 1
18+
ENTRYPOINT [ "/bin/docker-entrypoint.sh" ]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@ docker run -d -p 9102:9102 -p 9125:9125 -p 9125:9125/udp \
520520
prom/statsd-exporter --statsd.mapping-config=/tmp/statsd_mapping.yml
521521
```
522522

523+
The container image healthcheck follows runtime values from `--web.telemetry-path` and
524+
`--web.listen-address` (defaulting to `/metrics` and port `9102` when these flags are not provided).
525+
523526
## Library packages
524527

525528
Parts of the implementation of this exporter are available as separate packages.

docker-entrypoint.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)