From e14d71e2cfbdc9489a7ce6c97aab118fc69b33b3 Mon Sep 17 00:00:00 2001 From: Clemens Hoffmann Date: Fri, 13 Dec 2024 10:33:04 +0100 Subject: [PATCH] Use constants for log and metrics messages --- registry/registry.go | 39 +++++++++------------------------------ route/pool.go | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/registry/registry.go b/registry/registry.go index 3e5c7234..938bb4da 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -93,22 +93,9 @@ func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) { r.reporter.CaptureRouteRegistrationLatency(time.Since(endpoint.UpdatedAt)) } - switch endpointPutResult { - case route.EndpointAdded: - r.reporter.CaptureRegistryMessage(endpoint, "endpoint-added") - if r.logger.Enabled(context.Background(), slog.LevelInfo) { - r.logger.Info("endpoint-added", buildSlogAttrs(uri, endpoint)...) - } - case route.EndpointUpdated: - r.reporter.CaptureRegistryMessage(endpoint, "endpoint-updated") - if r.logger.Enabled(context.Background(), slog.LevelDebug) { - r.logger.Debug("endpoint-updated", buildSlogAttrs(uri, endpoint)...) - } - default: - r.reporter.CaptureRegistryMessage(endpoint, "endpoint-not-updated") - if r.logger.Enabled(context.Background(), slog.LevelDebug) { - r.logger.Debug("endpoint-not-updated", buildSlogAttrs(uri, endpoint)...) - } + r.reporter.CaptureRegistryMessage(endpoint, string(endpointPutResult)) + if r.logger.Enabled(context.Background(), slog.LevelInfo) { + r.logger.Info(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...) } } @@ -174,28 +161,20 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) { if pool == nil { return } - switch endpointUnregisteredResult { - case route.EndpointUnregistered: - r.reporter.CaptureUnregistryMessage(endpoint, "endpoint-unregistered") - if r.logger.Enabled(context.Background(), slog.LevelInfo) { - r.logger.Info("endpoint-unregistered", buildSlogAttrs(routeKey, endpoint)...) - } - case route.EndpointNotUnregistered: - r.reporter.CaptureUnregistryMessage(endpoint, "endpoint-not-unregistered") - if r.logger.Enabled(context.Background(), slog.LevelInfo) { - r.logger.Info("endpoint-not-unregistered", buildSlogAttrs(routeKey, endpoint)...) - } + + r.reporter.CaptureUnregistryMessage(endpoint, string(endpointUnregisteredResult)) + if r.logger.Enabled(context.Background(), slog.LevelInfo) { + r.logger.Info(string(endpointUnregisteredResult), buildSlogAttrs(routeKey, endpoint)...) } routeUnregisteredResult := r.deleteRouteWithoutEndpoint(routeKey, pool) switch routeUnregisteredResult { case route.RouteUnregistered: - r.reporter.CaptureUnregistryMessage(endpoint, "route-unregistered") + r.reporter.CaptureUnregistryMessage(endpoint, string(routeUnregisteredResult)) if r.logger.Enabled(context.Background(), slog.LevelInfo) { - r.logger.Info("route-unregistered", slog.Any("uri", routeKey)) + r.logger.Info(string(routeUnregisteredResult), slog.Any("uri", routeKey)) } } - } func (r *RouteRegistry) unregisterEndpoint(routeKey route.Uri, endpoint *route.Endpoint) (route.PoolRemoveEndpointResult, *route.EndpointPool) { diff --git a/route/pool.go b/route/pool.go index b689ed9f..b3ec7893 100644 --- a/route/pool.go +++ b/route/pool.go @@ -20,26 +20,26 @@ type Counter struct { value int64 } -type PoolPutResult int +type PoolPutResult string const ( - EndpointNotUpdated = PoolPutResult(iota) - EndpointUpdated - EndpointAdded + EndpointNotUpdated PoolPutResult = "endpoint-not-updated" + EndpointUpdated PoolPutResult = "endpoint-updated" + EndpointAdded PoolPutResult = "endpoint-added" ) -type PoolRemoveEndpointResult int +type PoolRemoveEndpointResult string const ( - EndpointUnregistered = PoolRemoveEndpointResult(iota) - EndpointNotUnregistered + EndpointUnregistered PoolRemoveEndpointResult = "endpoint-unregistered" + EndpointNotUnregistered PoolRemoveEndpointResult = "endpoint-not-unregistered" ) -type PoolRemoveRouteResult int +type PoolRemoveRouteResult string const ( - RouteUnregistered = PoolRemoveRouteResult(iota) - RouteNotUnregistered + RouteUnregistered PoolRemoveRouteResult = "route-unregistered" + RouteNotUnregistered PoolRemoveRouteResult = "route-not-unregistered" ) func NewCounter(initial int64) *Counter {