Use ServiceName in traefik_service_server_up metric

This commit is contained in:
krishna sindhur 2024-07-29 15:22:05 +05:30 committed by GitHub
parent 266a2d8b91
commit 386c2ffb20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 15 deletions

View file

@ -50,11 +50,12 @@ type ServiceHealthChecker struct {
metrics metricsHealthCheck metrics metricsHealthCheck
client *http.Client client *http.Client
targets map[string]*url.URL 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) logger := log.Ctx(ctx)
interval := time.Duration(config.Interval) interval := time.Duration(config.Interval)
@ -80,14 +81,15 @@ func NewServiceHealthChecker(ctx context.Context, metrics metricsHealthCheck, co
} }
return &ServiceHealthChecker{ return &ServiceHealthChecker{
balancer: service, balancer: service,
info: info, info: info,
config: config, config: config,
interval: interval, interval: interval,
timeout: timeout, timeout: timeout,
targets: targets, targets: targets,
client: client, serviceName: serviceName,
metrics: metrics, client: client,
metrics: metrics,
} }
} }
@ -136,7 +138,7 @@ func (shc *ServiceHealthChecker) Launch(ctx context.Context) {
shc.info.UpdateServerStatus(target.String(), statusStr) shc.info.UpdateServerStatus(target.String(), statusStr)
shc.metrics.ServiceServerUpGauge(). shc.metrics.ServiceServerUpGauge().
With("service", proxyName, "url", target.String()). With("service", shc.serviceName, "url", target.String()).
Set(serverUpMetricValue) Set(serverUpMetricValue)
} }
} }

View file

@ -64,7 +64,7 @@ func TestNewServiceHealthChecker_durations(t *testing.T) {
for _, test := range testCases { for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) { 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.expInterval, healthChecker.interval)
assert.Equal(t, test.expTimeout, healthChecker.timeout) assert.Equal(t, test.expTimeout, healthChecker.timeout)
}) })
@ -289,7 +289,7 @@ func TestServiceHealthChecker_checkHealthHTTP_NotFollowingRedirects(t *testing.T
Interval: dynamic.DefaultHealthCheckInterval, Interval: dynamic.DefaultHealthCheckInterval,
Timeout: dynamic.DefaultHealthCheckTimeout, 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)) err := healthChecker.checkHealthHTTP(ctx, testhelpers.MustParseURL(server.URL))
require.NoError(t, err) require.NoError(t, err)
@ -426,7 +426,7 @@ func TestServiceHealthChecker_Launch(t *testing.T) {
gauge := &testhelpers.CollectingGauge{} gauge := &testhelpers.CollectingGauge{}
serviceInfo := &runtime.ServiceInfo{} 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 := sync.WaitGroup{}
wg.Add(1) 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.expNumRemovedServers, lb.numRemovedServers, "removed servers")
assert.Equal(t, test.expNumUpsertedServers, lb.numUpsertedServers, "upserted servers") assert.Equal(t, test.expNumUpsertedServers, lb.numUpsertedServers, "upserted servers")
assert.InDelta(t, test.expGaugeValue, gauge.GaugeValue, delta, "ServerUp Gauge") 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()) assert.Equal(t, map[string]string{targetURL.String(): test.targetStatus}, serviceInfo.GetAllStatus())
}) })
} }

View file

@ -359,6 +359,7 @@ func (m *Manager) getLoadBalancerServiceHandler(ctx context.Context, serviceName
info, info,
roundTripper, roundTripper,
healthCheckTargets, healthCheckTargets,
serviceName,
) )
} }