From 386c2ffb20094f69e733dd1cf8653283bf46b6ca Mon Sep 17 00:00:00 2001 From: krishna sindhur Date: Mon, 29 Jul 2024 15:22:05 +0530 Subject: [PATCH] Use ServiceName in traefik_service_server_up metric --- pkg/healthcheck/healthcheck.go | 26 ++++++++++++++------------ pkg/healthcheck/healthcheck_test.go | 7 ++++--- pkg/server/service/service.go | 1 + 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/pkg/healthcheck/healthcheck.go b/pkg/healthcheck/healthcheck.go index b0e02e7a3..c9d3e7e36 100644 --- a/pkg/healthcheck/healthcheck.go +++ b/pkg/healthcheck/healthcheck.go @@ -50,11 +50,12 @@ type ServiceHealthChecker struct { metrics metricsHealthCheck - client *http.Client - targets map[string]*url.URL + client *http.Client + targets map[string]*url.URL + serviceName string } -func NewServiceHealthChecker(ctx context.Context, metrics metricsHealthCheck, config *dynamic.ServerHealthCheck, service StatusSetter, info *runtime.ServiceInfo, transport http.RoundTripper, targets map[string]*url.URL) *ServiceHealthChecker { +func NewServiceHealthChecker(ctx context.Context, metrics metricsHealthCheck, config *dynamic.ServerHealthCheck, service StatusSetter, info *runtime.ServiceInfo, transport http.RoundTripper, targets map[string]*url.URL, serviceName string) *ServiceHealthChecker { logger := log.Ctx(ctx) interval := time.Duration(config.Interval) @@ -80,14 +81,15 @@ func NewServiceHealthChecker(ctx context.Context, metrics metricsHealthCheck, co } return &ServiceHealthChecker{ - balancer: service, - info: info, - config: config, - interval: interval, - timeout: timeout, - targets: targets, - client: client, - metrics: metrics, + balancer: service, + info: info, + config: config, + interval: interval, + timeout: timeout, + targets: targets, + serviceName: serviceName, + client: client, + metrics: metrics, } } @@ -136,7 +138,7 @@ func (shc *ServiceHealthChecker) Launch(ctx context.Context) { shc.info.UpdateServerStatus(target.String(), statusStr) shc.metrics.ServiceServerUpGauge(). - With("service", proxyName, "url", target.String()). + With("service", shc.serviceName, "url", target.String()). Set(serverUpMetricValue) } } diff --git a/pkg/healthcheck/healthcheck_test.go b/pkg/healthcheck/healthcheck_test.go index 3eb276308..a34ac07d6 100644 --- a/pkg/healthcheck/healthcheck_test.go +++ b/pkg/healthcheck/healthcheck_test.go @@ -64,7 +64,7 @@ func TestNewServiceHealthChecker_durations(t *testing.T) { for _, test := range testCases { t.Run(test.desc, func(t *testing.T) { - healthChecker := NewServiceHealthChecker(context.Background(), nil, test.config, nil, nil, http.DefaultTransport, nil) + healthChecker := NewServiceHealthChecker(context.Background(), nil, test.config, nil, nil, http.DefaultTransport, nil, "") assert.Equal(t, test.expInterval, healthChecker.interval) assert.Equal(t, test.expTimeout, healthChecker.timeout) }) @@ -289,7 +289,7 @@ func TestServiceHealthChecker_checkHealthHTTP_NotFollowingRedirects(t *testing.T Interval: dynamic.DefaultHealthCheckInterval, Timeout: dynamic.DefaultHealthCheckTimeout, } - healthChecker := NewServiceHealthChecker(ctx, nil, config, nil, nil, http.DefaultTransport, nil) + healthChecker := NewServiceHealthChecker(ctx, nil, config, nil, nil, http.DefaultTransport, nil, "") err := healthChecker.checkHealthHTTP(ctx, testhelpers.MustParseURL(server.URL)) require.NoError(t, err) @@ -426,7 +426,7 @@ func TestServiceHealthChecker_Launch(t *testing.T) { gauge := &testhelpers.CollectingGauge{} serviceInfo := &runtime.ServiceInfo{} - hc := NewServiceHealthChecker(ctx, &MetricsMock{gauge}, config, lb, serviceInfo, http.DefaultTransport, map[string]*url.URL{"test": targetURL}) + hc := NewServiceHealthChecker(ctx, &MetricsMock{gauge}, config, lb, serviceInfo, http.DefaultTransport, map[string]*url.URL{"test": targetURL}, "foobar") wg := sync.WaitGroup{} wg.Add(1) @@ -449,6 +449,7 @@ func TestServiceHealthChecker_Launch(t *testing.T) { assert.Equal(t, test.expNumRemovedServers, lb.numRemovedServers, "removed servers") assert.Equal(t, test.expNumUpsertedServers, lb.numUpsertedServers, "upserted servers") assert.InDelta(t, test.expGaugeValue, gauge.GaugeValue, delta, "ServerUp Gauge") + assert.Equal(t, []string{"service", "foobar", "url", targetURL.String()}, gauge.LastLabelValues) assert.Equal(t, map[string]string{targetURL.String(): test.targetStatus}, serviceInfo.GetAllStatus()) }) } diff --git a/pkg/server/service/service.go b/pkg/server/service/service.go index 2a41836e3..e77fda54c 100644 --- a/pkg/server/service/service.go +++ b/pkg/server/service/service.go @@ -359,6 +359,7 @@ func (m *Manager) getLoadBalancerServiceHandler(ctx context.Context, serviceName info, roundTripper, healthCheckTargets, + serviceName, ) }