Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ LABEL maintainer="The Prometheus Authors <prometheus-developers@googlegroups.com
ARG ARCH="amd64"
ARG OS="linux"
COPY .build/${OS}-${ARCH}/statsd_exporter /bin/statsd_exporter
COPY docker-entrypoint.sh /bin/docker-entrypoint.sh
RUN chmod +x /bin/docker-entrypoint.sh

ENV TELEMETRY_PATH=/metrics \
LISTEN_PORT=9102

USER 65534
EXPOSE 9102 9125 9125/udp
HEALTHCHECK CMD wget --spider -S "http://localhost:9102/metrics" -T 60 2>&1 || exit 1
ENTRYPOINT [ "/bin/statsd_exporter" ]
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
ENTRYPOINT [ "/bin/docker-entrypoint.sh" ]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ docker run -d -p 9102:9102 -p 9125:9125 -p 9125:9125/udp \
prom/statsd-exporter --statsd.mapping-config=/tmp/statsd_mapping.yml
```

The container image healthcheck follows runtime values from `--web.telemetry-path` and
`--web.listen-address` (defaulting to `/metrics` and port `9102` when these flags are not provided).

## Library packages

Parts of the implementation of this exporter are available as separate packages.
Expand Down
76 changes: 76 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/sh

set -eu

HEALTHCHECK_ENV_FILE=/tmp/statsd_exporter_healthcheck.env
STATSD_EXPORTER_BIN=${STATSD_EXPORTER_BIN:-/bin/statsd_exporter}

telemetry_path="/metrics"
listen_port="9102"
listen_address=""
pending_flag=""

# Parse both formats --flag=value and --flag value forms without altering original args.
for arg in "$@"; do
case "$pending_flag" in
telemetry_path)
telemetry_path="$arg"
pending_flag=""
continue
;;
listen_address)
listen_address="$arg"
pending_flag=""
continue
;;
esac

case "$arg" in
--web.telemetry-path=*)
telemetry_path=${arg#*=}
;;
--web.telemetry-path)
pending_flag="telemetry_path"
;;
--web.listen-address=*)
listen_address=${arg#*=}
;;
--web.listen-address)
pending_flag="listen_address"
;;
esac
done

# Ensure telemetry path is non-empty and absolute for URL construction.
if [ -z "$telemetry_path" ]; then
telemetry_path="/metrics"
elif [ "${telemetry_path#/}" = "$telemetry_path" ]; then
telemetry_path="/$telemetry_path"
fi

# Derive port from the final :PORT segment (works for host:port and [::]:port).
if [ -n "$listen_address" ]; then
port_candidate=${listen_address##*:}
case "$port_candidate" in
''|*[!0-9]*)
;;
*)
if [ "$port_candidate" -ge 1 ] 2>/dev/null && [ "$port_candidate" -le 65535 ] 2>/dev/null; then
listen_port="$port_candidate"
fi
;;
esac
fi

export TELEMETRY_PATH="$telemetry_path"
export LISTEN_PORT="$listen_port"

# Persist parsed values so Docker healthcheck invocations can read them later.
tmp_env_file="${HEALTHCHECK_ENV_FILE}.tmp"
{
printf 'TELEMETRY_PATH=%s\n' "$TELEMETRY_PATH"
printf 'LISTEN_PORT=%s\n' "$LISTEN_PORT"
} >"$tmp_env_file"
mv "$tmp_env_file" "$HEALTHCHECK_ENV_FILE"

exec "$STATSD_EXPORTER_BIN" "$@"