Repository navigation
add prometheus metrics and health endpoints - #45
Merged
Merged
Conversation
thde
reviewed
Oct 29, 2026
| metricsBindAddress string | ||
| ) | ||
|
|
||
| func main() { |
Member
There was a problem hiding this comment.
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()
}()
Contributor
Author
There was a problem hiding this comment.
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) |
Member
There was a problem hiding this comment.
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?
Contributor
Author
There was a problem hiding this comment.
Makes sense and seems to be a standard approach nowadays. 👍
| } | ||
| w.WriteHeader(http.StatusOK) | ||
| }) | ||
| s.server = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second} |
Member
There was a problem hiding this comment.
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 | |||
Member
There was a problem hiding this comment.
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)
}
Contributor
Author
There was a problem hiding this comment.
We would have seen permission errors then. Will implement it.
thde
approved these changes
Oct 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
kigeon now serves Prometheus metrics on
/metrics, a liveness check on/healthzand a readiness check on/readyzonglobal.metricsBindAddress(default:8080, overridable with--metrics-bind-address). See the new Metrics section of the README for all metrics and example alerts.sent,filtered,skipped,rejected,failed), HTTP requests to the receiver by status code, and hook duration and errors./readyzsucceeds once all senders are subscribed and events are watched./healthzonly checks that the process runs, so an unavailable receiver does not restart kigeon.The chart changes (metrics port, probes, ServiceMonitor) follow in a separate PR once the image with the metrics endpoint is released.