Skip to content

add prometheus metrics and health endpoints - #45

Merged
thirdeyenick merged 2 commits into
mainfrom
metrics
Oct 29, 2026
Merged

thirdeyenick merged 2 commits into
mainfrom
metrics

Conversation

@thirdeyenick

@thirdeyenick thirdeyenick commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

kigeon now serves Prometheus metrics on /metrics, a liveness check on /healthz and a readiness check on /readyz on global.metricsBindAddress (default :8080, overridable with --metrics-bind-address). See the new Metrics section of the README for all metrics and example alerts.

  • Watched and published events, the queue size and limit, and the backlog of each sender.
  • Results per sender (sent, filtered, skipped, rejected, failed), HTTP requests to the receiver by status code, and hook duration and errors.
  • Requests to the Kubernetes API by status code.
  • /readyz succeeds once all senders are subscribed and events are watched. /healthz only checks that the process runs, so an unavailable receiver does not restart kigeon.
  • Duplicates detected by JetStream are no longer stored in the deduplication store again.

The chart changes (metrics port, probes, ServiceMonitor) follow in a separate PR once the image with the metrics endpoint is released.

Comment thread cmd/kigeon/main.go
metricsBindAddress string
)

func main() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it is not really about this but I like to do:

	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()

	// Restore default signal handling after the first signal,
	// so a second one terminates the process immediately instead of waiting for shutdown.
	go func() {
		<-ctx.Done()
		stop()
	}()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, will do this in a follow up PR

defer func() {
metrics.SenderPushDuration.WithLabelValues(s.name).Observe(time.Since(start).Seconds())
}()
resp, err := s.httpClient.Do(req)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

SenderRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
    Namespace: namespace,
    Name:      "sender_http_requests_total",
    Help:      "HTTP requests to the receiver of a sender.",
}, []string{"sender", "code"})

SenderRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
    Namespace: namespace,
    Name:      "sender_http_request_duration_seconds",
    Help:      "Duration of HTTP requests to the receiver of a sender.",
    Buckets:   prometheus.DefBuckets,
}, []string{"sender", "code"})

// InstrumentRoundTripper wraps next with request metrics for a sender.
func InstrumentRoundTripper(sender string, next http.RoundTripper) http.RoundTripper {
    l := prometheus.Labels{"sender": sender}
    return promhttp.InstrumentRoundTripperCounter(SenderRequests.MustCurryWith(l),
        promhttp.InstrumentRoundTripperDuration(SenderRequestDuration.MustCurryWith(l), next))
}

Then we could avoid SenderPushDuration?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense and seems to be a standard approach nowadays. 👍

Comment thread internal/metrics/server.go Outdated
}
w.WriteHeader(http.StatusOK)
})
s.server = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pass the base context and slog error logger here?

@@ -0,0 +1,98 @@
// Package metrics provides the Prometheus metrics of kigeon and the HTTP

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about also instrumenting the k8s client like so:

package metrics

import (
        "context"
        "net/url"
        "time"

        "github.com/prometheus/client_golang/prometheus"
        clientmetrics "k8s.io/client-go/tools/metrics"
)

var (
        // KubernetesRequests counts the requests to the Kubernetes API by
        // status code.
        KubernetesRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
                Namespace: namespace,
                Name:      "kubernetes_requests_total",
                Help:      "Requests to the Kubernetes API by status code.",
        }, []string{"code", "method"})

        // KubernetesRequestRetries counts retried requests to the Kubernetes
        // API by status code.
        KubernetesRequestRetries = prometheus.NewCounterVec(prometheus.CounterOpts{
                Namespace: namespace,
                Name:      "kubernetes_request_retries_total",
                Help:      "Retried requests to the Kubernetes API by status code.",
        }, []string{"code", "method"})

        // KubernetesRequestDuration observes the duration of non-watch
        // requests to the Kubernetes API.
        KubernetesRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
                Namespace: namespace,
                Name:      "kubernetes_request_duration_seconds",
                Help:      "Duration of non-watch requests to the Kubernetes API.",
                Buckets:   prometheus.DefBuckets,
        }, []string{"verb"})
)

type requestResult struct{}

func (requestResult) Increment(_ context.Context, code, method, _ string) {
        KubernetesRequests.WithLabelValues(code, method).Inc()
}

type requestRetry struct{}

func (requestRetry) IncrementRetry(_ context.Context, code, method, _ string) {
        KubernetesRequestRetries.WithLabelValues(code, method).Inc()
}

type requestLatency struct{}

func (requestLatency) Observe(_ context.Context, verb string, _ url.URL, latency time.Duration) {
        KubernetesRequestDuration.WithLabelValues(verb).Observe(latency.Seconds())
}

func init() {
        clientmetrics.Register(clientmetrics.RegisterOpts{
                RequestResult:  requestResult{},
                RequestRetry:   requestRetry{},
                RequestLatency: requestLatency{},
        })
        Registry.MustRegister(KubernetesRequests, KubernetesRequestRetries, KubernetesRequestDuration)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would have seen permission errors then. Will implement it.

@thirdeyenick
thirdeyenick merged commit 10016b1 into main Oct 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants