From 96e120f1e916e2a1ce534fa4bcaa584f48a5204a Mon Sep 17 00:00:00 2001 From: Daniel Muehlbachler-Pietrzykowski Date: Fri, 4 Oct 2024 10:27:41 +0200 Subject: [PATCH] feat: add metrics endpoint --- Dockerfile | 2 +- cmd/webhook/init/server/server.go | 2 ++ pkg/webhook/webhook.go | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d60a6d2..4a94b05 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,6 @@ LABEL org.opencontainers.image.version="${CI_COMMIT_TAG}" USER 20000:20000 COPY --chmod=555 external-dns-provider-adguard /opt/external-dns-provider-adguard/webhook -EXPOSE 8888/tcp +EXPOSE 8888/tcp 8080/tcp ENTRYPOINT ["/opt/external-dns-provider-adguard/webhook"] diff --git a/cmd/webhook/init/server/server.go b/cmd/webhook/init/server/server.go index 3a64085..f97b7a3 100644 --- a/cmd/webhook/init/server/server.go +++ b/cmd/webhook/init/server/server.go @@ -44,10 +44,12 @@ func Init(config configuration.Config, p *webhook.Webhook) *http.Server { // Init server initialization function // The server will respond to the following endpoints: // - /healthz (GET): health check endpoint +// - /metrics (GET): health metrics func InitHealthz(config configuration.Config, p *webhook.Webhook) *http.Server { r := chi.NewRouter() r.Get("/healthz", p.Health) + r.Get("/metrics", p.Metrics) srv := createHTTPServer(fmt.Sprintf("%s:%d", config.HealthzHost, config.HealthzPort), r, config.ServerReadTimeout, config.ServerWriteTimeout) go func() { diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index b0a0478..5edbd72 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -99,6 +99,18 @@ func (p *Webhook) Health(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } +// Metrics handles the metrics request +func (p *Webhook) Metrics(w http.ResponseWriter, r *http.Request) { + requestLog(r).Debug("requesting metrics") + s := p.provider.Health(r.Context()) + err := json.NewEncoder(w).Encode(map[string]bool{"healthy": s}) + if err != nil { + requestLog(r).WithField(logFieldError, err).Error("error encoding metrics") + w.WriteHeader(http.StatusInternalServerError) + return + } +} + // Records handles the get request for records func (p *Webhook) Records(w http.ResponseWriter, r *http.Request) { if err := p.acceptHeaderCheck(w, r); err != nil {