From 3db843d4fb898e454fb6c6618fceef351c695871 Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Mon, 24 Jun 2024 17:12:19 +0200 Subject: [PATCH] feat: enable Prometheus exporter --- .changeset/fair-games-rule.md | 5 +++++ Dockerfile | 21 ++++++++++++++++++++- README.md | 2 ++ entrypoint.sh | 8 ++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 .changeset/fair-games-rule.md diff --git a/.changeset/fair-games-rule.md b/.changeset/fair-games-rule.md new file mode 100644 index 0000000..e13da4a --- /dev/null +++ b/.changeset/fair-games-rule.md @@ -0,0 +1,5 @@ +--- +"varnish-post": minor +--- + +Enable Prometheus Exporter by setting `ENABLE_PROMETHEUS_EXPORTER` to `true`. diff --git a/Dockerfile b/Dockerfile index a42b373..3ee9b4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,16 @@ +# Build the prometheus_varnish_exporter binary +FROM docker.io/library/golang:1.22 as prometheus_varnish_exporter +WORKDIR /app + +# Releases: https://github.com/jonnenauha/prometheus_varnish_exporter/releases +ARG PROMETHEUS_VARNISH_EXPORTER_VERSION=1.6.1 + +RUN git clone https://github.com/jonnenauha/prometheus_varnish_exporter.git . \ + && git checkout "${PROMETHEUS_VARNISH_EXPORTER_VERSION}" \ + && go mod download \ + && go build -o prometheus_varnish_exporter + +# Build the final image FROM docker.io/library/ubuntu:22.04 # Configuration @@ -11,6 +24,7 @@ ENV DISABLE_ERROR_CACHING="true" ENV DISABLE_ERROR_CACHING_TTL="30s" ENV CONFIG_FILE="default.vcl" ENV ENABLE_LOGS="true" +ENV ENABLE_PROMETHEUS_EXPORTER="false" # Install some dependencies RUN apt-get update \ @@ -21,11 +35,16 @@ RUN apt-get update \ varnish-modules \ && apt-get clean +# Get the prometheus_varnish_exporter binary +COPY --from=prometheus_varnish_exporter \ + /app/prometheus_varnish_exporter \ + /usr/local/bin/prometheus_varnish_exporter + # Deploy our custom configuration WORKDIR /etc/varnish COPY config/ /templates COPY entrypoint.sh / RUN chmod +x /entrypoint.sh -EXPOSE 80 8443 +EXPOSE 80 8443 9131 ENTRYPOINT [ "tini", "--", "/entrypoint.sh" ] diff --git a/README.md b/README.md index d1c9463..152c91e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ You can use following environment variables for configuration: - `DISABLE_ERROR_CACHING_TTL`: time where requests should be directly sent to the backend after an error occured (default: `30s`) - `CONFIG_FILE`: the name of the configuration file to use (default: `default.vcl`) - `ENABLE_LOGS`: enable logs (default: `true`) +- `ENABLE_PROMETHEUS_EXPORTER`: enable the Prometheus exporter if set to `true` (default: `false`). + If enabled, the exporter will be available on the 9131 port. ## Cache invalidation diff --git a/entrypoint.sh b/entrypoint.sh index b46ad1d..fd59bdf 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,7 @@ #!/bin/sh ENABLE_LOGS="${ENABLE_LOGS}" +ENABLE_PROMETHEUS_EXPORTER="${ENABLE_PROMETHEUS_EXPORTER}" set -eu @@ -18,6 +19,13 @@ if [ "${ENABLE_LOGS}" = "true" ]; then varnishncsa& fi +# Start prometheus_varnish_exporter if enabled +if [ "${ENABLE_PROMETHEUS_EXPORTER}" = "true" ]; then + (sleep 2 && prometheus_varnish_exporter \ + -web.listen-address ":9131" \ + -web.telemetry-path "/metrics") & +fi + set -x # Run Varnish