Skip to content

Commit

Permalink
feat: add metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlba91 committed Oct 4, 2024
1 parent bb4678f commit 96e120f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 2 additions & 0 deletions cmd/webhook/init/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 96e120f

Please sign in to comment.