From 02f0c19ba5c969d83de7c5ab971131a52b98d4bc Mon Sep 17 00:00:00 2001 From: Adora Laura Kalb Date: Wed, 17 Apr 2024 17:38:21 +0000 Subject: [PATCH] Resolve "Nil reference pointer exception when getting queried by Prometheus" --- Dockerfile | 9 ++++++--- collector/collector.go | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0acd760..5119675 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,9 @@ -FROM golang:1.22-alpine AS dev +FROM golang:1.22-bullseye AS dev COPY . /var/app WORKDIR /var/app + ENV GO111MODULE="on" \ CGO_ENABLED=0 \ GOOS=linux @@ -13,14 +14,16 @@ ENTRYPOINT ["sh"] FROM dev as build +RUN DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends --assume-yes ca-certificates RUN go mod download && go mod verify -RUN go build -o ntppool-exporter main.go$ +RUN go build -o ntppool-exporter main.go RUN chmod +x ntppool-exporter -FROM scratch AS prod +FROM debian:12 AS prod WORKDIR /app +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=build /var/app/ntppool-exporter /bin/ LABEL maintainer="Adora Laura Kalb " diff --git a/collector/collector.go b/collector/collector.go index ae7849d..536f92a 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -77,15 +77,16 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) { } res, err := client.Do(req) if err != nil { - level.Error(logger).Log("msg", "Error in HTTP response", "status", res.Status) - httpError = true + level.Error(logger).Log("msg", "Error in HTTP response", "error", err) + return + } defer res.Body.Close() body, err := io.ReadAll(res.Body) if err != nil { level.Error(logger).Log("msg", "Error reading HTTP response body", "error", err) - httpError = true + return } var response models.ApiResponse @@ -93,7 +94,7 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger log.Logger) { err = json.Unmarshal(body, &response) if err != nil { level.Error(logger).Log("msg", "Error unmarshaling JSON body", "error", err) - httpError = true + return } if !httpError {