Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Last active January 25, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlafeldt/7fab53f47dd73a8c34f8b95ec444ab79 to your computer and use it in GitHub Desktop.
Save mlafeldt/7fab53f47dd73a8c34f8b95ec444ab79 to your computer and use it in GitHub Desktop.
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