Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit fae675a

Browse files
committed
Refactor Register, fix log levels
1 parent e14d71e commit fae675a

File tree

6 files changed

+92
-60
lines changed

6 files changed

+92
-60
lines changed

metrics/compositereporter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ type RouteRegistryReporter interface {
4747
CaptureRegistryMessage(msg ComponentTagged, action string)
4848
CaptureRouteRegistrationLatency(t time.Duration)
4949
UnmuzzleRouteRegistrationLatency()
50-
CaptureUnregistryMessage(msg ComponentTagged, action string)
5150
}
5251

5352
type CompositeReporter struct {

metrics/metricsreporter.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,6 @@ func (m *MetricsReporter) CaptureRegistryMessage(msg ComponentTagged, action str
145145
m.Batcher.BatchIncrementCounter(componentName)
146146
}
147147

148-
func (m *MetricsReporter) CaptureUnregistryMessage(msg ComponentTagged, action string) {
149-
var componentName string
150-
if msg.Component() == "" {
151-
componentName = "unregistry_message." + action
152-
} else {
153-
componentName = "unregistry_message." + action + "." + msg.Component()
154-
}
155-
m.Batcher.BatchIncrementCounter(componentName)
156-
}
157-
158148
func (m *MetricsReporter) CaptureWebSocketUpdate() {
159149
m.Batcher.BatchIncrementCounter("websocket_upgrades")
160150
}

metrics/metricsreporter_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,33 +517,33 @@ var _ = Describe("MetricsReporter", func() {
517517
BeforeEach(func() {
518518
endpoint = new(route.Endpoint)
519519
endpoint.Tags = map[string]string{"component": "oauth-server"}
520-
metricReporter.CaptureUnregistryMessage(endpoint, "some-action")
520+
metricReporter.CaptureRegistryMessage(endpoint, "some-action")
521521
})
522522

523523
It("increments the counter metric", func() {
524524
Expect(batcher.BatchIncrementCounterCallCount()).To(Equal(1))
525-
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("unregistry_message.some-action.oauth-server"))
525+
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message.some-action.oauth-server"))
526526
})
527527

528528
It("increments the counter metric for each component unregistered", func() {
529529
endpointTwo := new(route.Endpoint)
530530
endpointTwo.Tags = map[string]string{"component": "api-server"}
531-
metricReporter.CaptureUnregistryMessage(endpointTwo, "some-action")
531+
metricReporter.CaptureRegistryMessage(endpointTwo, "some-action")
532532

533533
Expect(batcher.BatchIncrementCounterCallCount()).To(Equal(2))
534-
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("unregistry_message.some-action.oauth-server"))
535-
Expect(batcher.BatchIncrementCounterArgsForCall(1)).To(Equal("unregistry_message.some-action.api-server"))
534+
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message.some-action.oauth-server"))
535+
Expect(batcher.BatchIncrementCounterArgsForCall(1)).To(Equal("registry_message.some-action.api-server"))
536536
})
537537
})
538538
Context("when unregister msg with empty component name is incremented", func() {
539539
BeforeEach(func() {
540540
endpoint = new(route.Endpoint)
541541
endpoint.Tags = map[string]string{}
542-
metricReporter.CaptureUnregistryMessage(endpoint, "some-action")
542+
metricReporter.CaptureRegistryMessage(endpoint, "some-action")
543543
})
544544
It("increments the counter metric", func() {
545545
Expect(batcher.BatchIncrementCounterCallCount()).To(Equal(1))
546-
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("unregistry_message.some-action"))
546+
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message.some-action"))
547547
})
548548
})
549549
})

registry/registry.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,41 @@ func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) {
8686
if !r.endpointInRouterShard(endpoint) {
8787
return
8888
}
89+
t := time.Now()
90+
registerRouteResult, pool := r.registerRoute(uri)
91+
if registerRouteResult == route.RouteRegistered {
92+
r.reporter.CaptureRegistryMessage(endpoint, string(route.RouteRegistered))
93+
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
94+
r.logger.Info(string(route.RouteRegistered), buildSlogAttrs(uri, endpoint)...)
95+
}
96+
}
8997

90-
endpointPutResult := r.register(uri, endpoint)
98+
endpointPutResult := r.registerEndpoint(endpoint, pool)
9199

92100
if endpointPutResult == route.EndpointAdded && !endpoint.UpdatedAt.IsZero() {
93101
r.reporter.CaptureRouteRegistrationLatency(time.Since(endpoint.UpdatedAt))
94102
}
95103

96104
r.reporter.CaptureRegistryMessage(endpoint, string(endpointPutResult))
97-
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
98-
r.logger.Info(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...)
105+
106+
switch endpointPutResult {
107+
case route.EndpointAdded:
108+
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
109+
r.logger.Info(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...)
110+
}
111+
default:
112+
if r.logger.Enabled(context.Background(), slog.LevelDebug) {
113+
r.logger.Debug(string(endpointPutResult), buildSlogAttrs(uri, endpoint)...)
114+
}
99115
}
116+
117+
r.SetTimeOfLastUpdate(t)
100118
}
101119

102-
func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.PoolPutResult {
120+
func (r *RouteRegistry) registerRoute(uri route.Uri) (route.PoolRegisterRouteResult, *route.EndpointPool) {
103121
r.RLock()
104122
defer r.RUnlock()
105-
106-
t := time.Now()
123+
poolRegisterRouteResult := route.RouteAlreadyExists
107124
routekey := uri.RouteKey()
108125
pool := r.byURI.Find(routekey)
109126

@@ -112,18 +129,24 @@ func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.
112129
r.RUnlock()
113130
pool = r.insertRouteKey(routekey, uri)
114131
r.RLock()
132+
poolRegisterRouteResult = route.RouteRegistered
115133
}
134+
return poolRegisterRouteResult, pool
135+
}
136+
137+
func (r *RouteRegistry) registerEndpoint(endpoint *route.Endpoint, pool *route.EndpointPool) route.PoolRegisterEndpointResult {
138+
r.RLock()
139+
defer r.RUnlock()
116140

117141
if endpoint.StaleThreshold > r.dropletStaleThreshold || endpoint.StaleThreshold == 0 {
118142
endpoint.StaleThreshold = r.dropletStaleThreshold
119143
}
120144

121-
endpointAdded := pool.Put(endpoint)
145+
endpointAddedResult := pool.Put(endpoint)
122146
// Overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
123147
pool.SetPoolLoadBalancingAlgorithm(endpoint)
124-
r.SetTimeOfLastUpdate(t)
125148

126-
return endpointAdded
149+
return endpointAddedResult
127150
}
128151

129152
// insertRouteKey acquires a write lock, inserts the route key into the registry and releases the write lock.
@@ -144,7 +167,7 @@ func (r *RouteRegistry) insertRouteKey(routekey route.Uri, uri route.Uri) *route
144167
LoadBalancingAlgorithm: r.DefaultLoadBalancingAlgorithm,
145168
})
146169
r.byURI.Insert(routekey, pool)
147-
r.logger.Info("route-registered", slog.Any("uri", routekey))
170+
r.logger.Info(string(route.RouteRegistered), slog.Any("uri", routekey))
148171
// for backward compatibility:
149172
r.logger.Debug("uri-added", slog.Any("uri", routekey))
150173
}
@@ -162,15 +185,15 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
162185
return
163186
}
164187

165-
r.reporter.CaptureUnregistryMessage(endpoint, string(endpointUnregisteredResult))
188+
r.reporter.CaptureRegistryMessage(endpoint, string(endpointUnregisteredResult))
166189
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
167190
r.logger.Info(string(endpointUnregisteredResult), buildSlogAttrs(routeKey, endpoint)...)
168191
}
169192

170193
routeUnregisteredResult := r.deleteRouteWithoutEndpoint(routeKey, pool)
171194
switch routeUnregisteredResult {
172195
case route.RouteUnregistered:
173-
r.reporter.CaptureUnregistryMessage(endpoint, string(routeUnregisteredResult))
196+
r.reporter.CaptureRegistryMessage(endpoint, string(routeUnregisteredResult))
174197
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
175198
r.logger.Info(string(routeUnregisteredResult), slog.Any("uri", routeKey))
176199
}

registry/registry_test.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ var _ = Describe("RouteRegistry", func() {
7373
Context("when a new endpoint is registered", func() {
7474
It("emits endpoint-registered message_count metrics", func() {
7575
r.Register("foo", fooEndpoint)
76-
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(1))
76+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
7777
endpoint1, action1 := reporter.CaptureRegistryMessageArgsForCall(0)
7878
Expect(endpoint1).To(Equal(fooEndpoint))
79-
Expect(action1).To(Equal("endpoint-added"))
79+
Expect(action1).To(Equal(string(route.RouteRegistered)))
80+
endpoint2, action2 := reporter.CaptureRegistryMessageArgsForCall(1)
81+
Expect(endpoint2).To(Equal(fooEndpoint))
82+
Expect(action2).To(Equal(string(route.EndpointAdded)))
8083
})
8184
})
8285

@@ -88,13 +91,16 @@ var _ = Describe("RouteRegistry", func() {
8891
endpoint2 := route.NewEndpoint(&route.EndpointOpts{ModificationTag: modTag2})
8992
r.Register("foo", endpoint1)
9093
r.Register("foo", endpoint2)
91-
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
94+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(3))
9295
endpointR1, action1 := reporter.CaptureRegistryMessageArgsForCall(0)
9396
Expect(endpointR1).To(Equal(endpoint1))
94-
Expect(action1).To(Equal("endpoint-added"))
97+
Expect(action1).To(Equal(string(route.RouteRegistered)))
9598
endpointR2, action2 := reporter.CaptureRegistryMessageArgsForCall(1)
96-
Expect(endpointR2).To(Equal(endpoint2))
97-
Expect(action2).To(Equal("endpoint-updated"))
99+
Expect(endpointR2).To(Equal(endpoint1))
100+
Expect(action2).To(Equal(string(route.EndpointAdded)))
101+
endpointR3, action3 := reporter.CaptureRegistryMessageArgsForCall(2)
102+
Expect(endpointR3).To(Equal(endpoint2))
103+
Expect(action3).To(Equal(string(route.EndpointUpdated)))
98104
})
99105
})
100106

@@ -106,13 +112,16 @@ var _ = Describe("RouteRegistry", func() {
106112
endpoint2 := route.NewEndpoint(&route.EndpointOpts{ModificationTag: modTag2})
107113
r.Register("foo", endpoint1)
108114
r.Register("foo", endpoint2)
109-
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
115+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(3))
110116
endpointR1, action1 := reporter.CaptureRegistryMessageArgsForCall(0)
111117
Expect(endpointR1).To(Equal(endpoint1))
112-
Expect(action1).To(Equal("endpoint-added"))
118+
Expect(action1).To(Equal(string(route.RouteRegistered)))
113119
endpointR2, action2 := reporter.CaptureRegistryMessageArgsForCall(1)
114-
Expect(endpointR2).To(Equal(endpoint2))
115-
Expect(action2).To(Equal("endpoint-not-updated"))
120+
Expect(endpointR2).To(Equal(endpoint1))
121+
Expect(action2).To(Equal(string(route.EndpointAdded)))
122+
endpointR3, action3 := reporter.CaptureRegistryMessageArgsForCall(2)
123+
Expect(endpointR3).To(Equal(endpoint2))
124+
Expect(action3).To(Equal(string(route.EndpointNotUpdated)))
116125
})
117126
})
118127

@@ -562,33 +571,36 @@ var _ = Describe("RouteRegistry", func() {
562571
BeforeEach(func() {
563572
fooEndpoint.Tags = map[string]string{"component": "oauth-server"}
564573
r.Register("foo", fooEndpoint)
574+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
565575
})
576+
566577
It("emits counter metrics for unregister endpoint and route", func() {
567578
r.Unregister("foo", fooEndpoint)
568-
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(2))
569-
endpoint1, action1 := reporter.CaptureUnregistryMessageArgsForCall(0)
579+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(4))
580+
endpoint1, action1 := reporter.CaptureRegistryMessageArgsForCall(2)
570581
Expect(endpoint1).To(Equal(fooEndpoint))
571-
Expect(action1).To(Equal("endpoint-unregistered"))
572-
endpoint2, action2 := reporter.CaptureUnregistryMessageArgsForCall(1)
582+
Expect(action1).To(Equal(string(route.EndpointUnregistered)))
583+
endpoint2, action2 := reporter.CaptureRegistryMessageArgsForCall(3)
573584
Expect(endpoint2).To(Equal(fooEndpoint))
574-
Expect(action2).To(Equal("route-unregistered"))
585+
Expect(action2).To(Equal(string(route.RouteUnregistered)))
575586
})
576587
})
577588

578589
Context("when endpoint does not have component tag", func() {
579590
BeforeEach(func() {
580591
fooEndpoint.Tags = map[string]string{}
581592
r.Register("foo", fooEndpoint)
593+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(2))
582594
})
583595
It("emits counter metrics for unregister endpoint and route", func() {
584596
r.Unregister("foo", fooEndpoint)
585-
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(2))
586-
endpoint1, action1 := reporter.CaptureUnregistryMessageArgsForCall(0)
597+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(4))
598+
endpoint1, action1 := reporter.CaptureRegistryMessageArgsForCall(2)
587599
Expect(endpoint1).To(Equal(fooEndpoint))
588-
Expect(action1).To(Equal("endpoint-unregistered"))
589-
endpoint2, action2 := reporter.CaptureUnregistryMessageArgsForCall(1)
600+
Expect(action1).To(Equal(string(route.EndpointUnregistered)))
601+
endpoint2, action2 := reporter.CaptureRegistryMessageArgsForCall(3)
590602
Expect(endpoint2).To(Equal(fooEndpoint))
591-
Expect(action2).To(Equal("route-unregistered"))
603+
Expect(action2).To(Equal(string(route.RouteUnregistered)))
592604
})
593605
})
594606
})
@@ -604,13 +616,14 @@ var _ = Describe("RouteRegistry", func() {
604616

605617
r.Register("foo", fooEndpoint)
606618
r.Register("foo", fooEndpoint2)
619+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(3))
607620
})
608621
It("emits counter metrics for unregister endpoint only", func() {
609622
r.Unregister("foo", fooEndpoint)
610-
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(1))
611-
endpoint1, action1 := reporter.CaptureUnregistryMessageArgsForCall(0)
623+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(4))
624+
endpoint1, action1 := reporter.CaptureRegistryMessageArgsForCall(3)
612625
Expect(endpoint1).To(Equal(fooEndpoint))
613-
Expect(action1).To(Equal("endpoint-unregistered"))
626+
Expect(action1).To(Equal(string(route.EndpointUnregistered)))
614627
})
615628
})
616629
Context("when route is not registered", func() {
@@ -619,7 +632,7 @@ var _ = Describe("RouteRegistry", func() {
619632
})
620633
It("does not emit counter metrics for unregister", func() {
621634
r.Unregister("foo", fooEndpoint)
622-
Expect(reporter.CaptureUnregistryMessageCallCount()).To(Equal(0))
635+
Expect(reporter.CaptureRegistryMessageCallCount()).To(Equal(0))
623636
})
624637
})
625638

route/pool.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ type Counter struct {
2020
value int64
2121
}
2222

23-
type PoolPutResult string
23+
type PoolRegisterEndpointResult string
2424

2525
const (
26-
EndpointNotUpdated PoolPutResult = "endpoint-not-updated"
27-
EndpointUpdated PoolPutResult = "endpoint-updated"
28-
EndpointAdded PoolPutResult = "endpoint-added"
26+
EndpointNotUpdated PoolRegisterEndpointResult = "endpoint-not-updated"
27+
EndpointUpdated PoolRegisterEndpointResult = "endpoint-updated"
28+
EndpointAdded PoolRegisterEndpointResult = "endpoint-added"
29+
)
30+
31+
type PoolRegisterRouteResult string
32+
33+
const (
34+
RouteRegistered PoolRegisterRouteResult = "route-registered"
35+
RouteAlreadyExists PoolRegisterRouteResult = "route-already-exists"
2936
)
3037

3138
type PoolRemoveEndpointResult string
@@ -268,11 +275,11 @@ func (p *EndpointPool) Update() {
268275
p.updatedAt = time.Now()
269276
}
270277

271-
func (p *EndpointPool) Put(endpoint *Endpoint) PoolPutResult {
278+
func (p *EndpointPool) Put(endpoint *Endpoint) PoolRegisterEndpointResult {
272279
p.Lock()
273280
defer p.Unlock()
274281

275-
var result PoolPutResult
282+
var result PoolRegisterEndpointResult
276283
e, found := p.index[endpoint.CanonicalAddr()]
277284
if found {
278285
result = EndpointUpdated

0 commit comments

Comments
 (0)