Skip to content

Instantly share code, notes, and snippets.

@mlafeldt mlafeldt/Dockerfile
Last active Jan 25, 2017

Embed
What would you like to do?
Push freshness metric to Prometheus Pushgateway

Generic Docker image to push a freshness metric to the Prometheus Pushgateway.

This metric can be used to determine whether a (cron) job was executed within a certain period or not.

This will push the metric freshness{job="Example",name="Foo",branch="Bar"} to the Pushgateway:

PUSHGATEWAY=https://prometheus.example.com:9091
docker run -t --rm -e PUSHGATEWAY=$PUSHGATEWAY freshness job=Example name=Foo branch=Bar

Note that you must always specify the PUSHGATEWAY endpoint and the job key-value pair.

FROM alpine
RUN apk add --update bash curl ca-certificates
ADD freshness.sh /usr/bin/freshness
ENTRYPOINT ["/usr/bin/freshness"]
#!/bin/bash
set -e
set -o pipefail
job=
suffix=
while test $# -gt 0; do
key=$(echo $1 | cut -d= -f1)
value=$(echo $1 | cut -d= -f2)
case "$key" in
job) job="$value" ;;
*) suffix="$suffix/$key/$value" ;;
esac
shift
done
if test -z "$job"; then
echo "error: job key-value pair missing"
exit 1
fi
if test -z "$PUSHGATEWAY"; then
echo "error: PUSHGATEWAY env var not set"
exit 1
fi
freshness=$(date +%s)
echo "freshness = $freshness"
cat <<EOF | curl -fsSL -XPUT --data-binary @- "$PUSHGATEWAY/metrics/job/$job$suffix"
# TYPE freshness gauge
freshness $freshness
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.