Skip to content

Commit

Permalink
Use constants for log and metrics messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmaen committed Dec 13, 2024
1 parent 8ec4a99 commit e14d71e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
39 changes: 9 additions & 30 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
}

Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions route/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e14d71e

Please sign in to comment.