-
Notifications
You must be signed in to change notification settings - Fork 86
Adds instrumentation for kube client-go #2316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
c-kruse
merged 4 commits into
skupperproject:main
from
c-kruse:add-kube-client-go-metrics
Dec 8, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "k8s.io/client-go/tools/metrics" | ||
| ) | ||
|
|
||
| // MustRegisterClientGoMetrics registers a set of metrics exposed from the | ||
| // k8s.io/client-go/tools/metrics package with the prometheus registry. | ||
| func MustRegisterClientGoMetrics(registry *prometheus.Registry) { | ||
| httpMetrics := &clientGoHttpMetrics{ | ||
| latency: prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Namespace: "skupper", | ||
| Subsystem: "kubernetes_client", | ||
| Name: "http_request_duration_seconds", | ||
| Help: "Latency of kubernetes client requests in seconds by endpoint.", | ||
| }, []string{"method", "endpoint"}), | ||
| results: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Namespace: "skupper", | ||
| Subsystem: "kubernetes_client", | ||
| Name: "http_requests_total", | ||
| Help: "Total number of kubernetes client requests by status code.", | ||
| }, []string{"method", "status_code"}), | ||
| retries: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Namespace: "skupper", | ||
| Subsystem: "kubernetes_client", | ||
| Name: "http_retries_total", | ||
| Help: "Total number of kubernetes client requests retried by status code.", | ||
| }, []string{"method", "status_code"}), | ||
| } | ||
| rateLimiterMetrics := &clientGoRateLimiterMetrics{ | ||
| latency: prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Namespace: "skupper", | ||
| Subsystem: "kubernetes_client", | ||
| Name: "rate_limiter_duration_seconds", | ||
| Help: "Latency of kubernetes client side rate limiting in seconds by endpoint.", | ||
| }, []string{"method", "endpoint"}), | ||
| } | ||
|
|
||
| registry.MustRegister(httpMetrics.latency, httpMetrics.results, httpMetrics.retries, rateLimiterMetrics.latency) | ||
| metrics.Register(metrics.RegisterOpts{ | ||
| RequestLatency: httpMetrics, | ||
| RequestResult: httpMetrics, | ||
| RequestRetry: httpMetrics, | ||
|
|
||
| RateLimiterLatency: rateLimiterMetrics, | ||
| }) | ||
| } | ||
|
|
||
| type clientGoHttpMetrics struct { | ||
| latency *prometheus.HistogramVec | ||
| results *prometheus.CounterVec | ||
| retries *prometheus.CounterVec | ||
| } | ||
|
|
||
| func (m *clientGoHttpMetrics) Observe(ctx context.Context, verb string, url url.URL, latency time.Duration) { | ||
| m.latency.WithLabelValues(verb, url.EscapedPath()).Observe(latency.Seconds()) | ||
| } | ||
|
|
||
| func (m *clientGoHttpMetrics) Increment(ctx context.Context, code string, method string, host string) { | ||
| m.results.WithLabelValues(method, code).Inc() | ||
| } | ||
| func (m *clientGoHttpMetrics) IncrementRetry(ctx context.Context, code string, method string, _ string) { | ||
| m.retries.WithLabelValues(method, code).Inc() | ||
| } | ||
|
|
||
| type clientGoRateLimiterMetrics struct { | ||
| latency *prometheus.HistogramVec | ||
| } | ||
|
|
||
| func (m *clientGoRateLimiterMetrics) Observe(ctx context.Context, verb string, url url.URL, latency time.Duration) { | ||
| m.latency.WithLabelValues(verb, url.EscapedPath()).Observe(latency.Seconds()) | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "net" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| iflag "github.com/skupperproject/skupper/internal/flag" | ||
| ) | ||
|
|
||
| const metricsPath = "/metrics" | ||
|
|
||
| type Config struct { | ||
| Disabled bool | ||
| Address string | ||
| } | ||
|
|
||
| func BoundConfig(flags *flag.FlagSet) (*Config, error) { | ||
| cfg := &Config{} | ||
| err := iflag.BoolVar(flags, &cfg.Disabled, "disable-metrics", "SKUPPER_METRICS_DISABLE", false, "Set to disable metrics.") | ||
| iflag.StringVar(flags, &cfg.Address, "metrics-address", "SKUPPER_METRICS_ADDRESS", ":9000", "The address for the metrics http server to listen on.") | ||
| return cfg, err | ||
| } | ||
|
|
||
| func NewServer(cfg *Config, registry *prometheus.Registry) *Server { | ||
| mux := http.NewServeMux() | ||
| mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) | ||
| srv := &http.Server{ | ||
| ReadTimeout: 5 * time.Second, | ||
| WriteTimeout: 5 * time.Second, | ||
| IdleTimeout: 120 * time.Second, | ||
| Handler: mux, | ||
| } | ||
| return &Server{ | ||
| config: *cfg, | ||
| server: srv, | ||
| } | ||
| } | ||
|
|
||
| type Server struct { | ||
| config Config | ||
| server *http.Server | ||
| } | ||
|
|
||
| func (s *Server) Start(stopCh <-chan struct{}) error { | ||
| listenCtx, cancel := context.WithCancel(context.Background()) | ||
| go func() { | ||
| select { | ||
| case <-stopCh: | ||
| cancel() | ||
| case <-listenCtx.Done(): | ||
| } | ||
| }() | ||
| defer cancel() | ||
|
|
||
| var lc net.ListenConfig | ||
| ln, err := lc.Listen(listenCtx, "tcp", s.config.Address) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to start listener: %s", err) | ||
| } | ||
| go func() { | ||
| if err := s.server.Serve(ln); err != nil { | ||
| log.Printf("metrics server error: %s", err) | ||
| } | ||
| }() | ||
| log.Printf("Started metrics server at: %s", s.config.Address) | ||
| return nil | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a results counter for "path" and "method" as well to help identifying operations happening during the reconciliation of a given resource ytpe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense! I did consider that.
The client-go metrics API doesn't give us all of that context in the ResultMetric interface, and I think that's intentional. It gives us the LatencyMetric (http_request_duration_seconds) that does have method and URL path. I think the best we can do is something along these lines:
I suspect the intent of the ResultMetric is mostly for clients working with multiple cluster API servers as a lower cardinality indicator of api server trouble. For our single-cluster context that is less interesting, but I included it anyways since the latency metric is absent of the status code field.