Skip to content

Commit

Permalink
Introduce disctinct metrics for each route (un)registration case
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmaen committed Dec 12, 2024
1 parent 90b997f commit 2b89f38
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 58 deletions.
4 changes: 2 additions & 2 deletions metrics/compositereporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type RouteRegistryReporter interface {
CaptureRouteStats(totalRoutes int, msSinceLastUpdate int64)
CaptureRoutesPruned(prunedRoutes uint64)
CaptureLookupTime(t time.Duration)
CaptureRegistryMessage(msg ComponentTagged)
CaptureRegistryMessage(msg ComponentTagged, action string)
CaptureRouteRegistrationLatency(t time.Duration)
UnmuzzleRouteRegistrationLatency()
CaptureUnregistryMessage(msg ComponentTagged)
CaptureUnregistryMessage(msg ComponentTagged, action string)
}

type CompositeReporter struct {
Expand Down
36 changes: 20 additions & 16 deletions metrics/fakes/fake_registry_reporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions metrics/metricsreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,22 @@ func (m *MetricsReporter) CaptureRoutesPruned(routesPruned uint64) {
m.Batcher.BatchAddCounter("routes_pruned", routesPruned)
}

func (m *MetricsReporter) CaptureRegistryMessage(msg ComponentTagged) {
func (m *MetricsReporter) CaptureRegistryMessage(msg ComponentTagged, action string) {
var componentName string
if msg.Component() == "" {
componentName = "registry_message"
componentName = "registry_message." + action
} else {
componentName = "registry_message." + msg.Component()
componentName = "registry_message." + action + "." + msg.Component()
}
m.Batcher.BatchIncrementCounter(componentName)
}

func (m *MetricsReporter) CaptureUnregistryMessage(msg ComponentTagged) {
func (m *MetricsReporter) CaptureUnregistryMessage(msg ComponentTagged, action string) {
var componentName string
if msg.Component() == "" {
componentName = "unregistry_message"
componentName = "unregistry_message." + action
} else {
componentName = "unregistry_message." + msg.Component()
componentName = "unregistry_message." + action + "." + msg.Component()
}
err := m.Sender.IncrementCounter(componentName)
if err != nil {
Expand Down
26 changes: 13 additions & 13 deletions metrics/metricsreporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,22 @@ var _ = Describe("MetricsReporter", func() {

It("sends number of nats messages received from each component", func() {
endpoint.Tags = map[string]string{}
metricReporter.CaptureRegistryMessage(endpoint)
metricReporter.CaptureRegistryMessage(endpoint, "some-action")

Expect(batcher.BatchIncrementCounterCallCount()).To(Equal(1))
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message"))
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message.some-action"))
})

It("sends number of nats messages received from each component", func() {
endpoint.Tags = map[string]string{"component": "uaa"}
metricReporter.CaptureRegistryMessage(endpoint)
metricReporter.CaptureRegistryMessage(endpoint, "some-action")

endpoint.Tags = map[string]string{"component": "route-emitter"}
metricReporter.CaptureRegistryMessage(endpoint)
metricReporter.CaptureRegistryMessage(endpoint, "some-action")

Expect(batcher.BatchIncrementCounterCallCount()).To(Equal(2))
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message.uaa"))
Expect(batcher.BatchIncrementCounterArgsForCall(1)).To(Equal("registry_message.route-emitter"))
Expect(batcher.BatchIncrementCounterArgsForCall(0)).To(Equal("registry_message.uaa.some-action"))
Expect(batcher.BatchIncrementCounterArgsForCall(1)).To(Equal("registry_message.route-emitter.some-action"))
})

It("sends the total routes", func() {
Expand Down Expand Up @@ -517,33 +517,33 @@ var _ = Describe("MetricsReporter", func() {
BeforeEach(func() {
endpoint = new(route.Endpoint)
endpoint.Tags = map[string]string{"component": "oauth-server"}
metricReporter.CaptureUnregistryMessage(endpoint)
metricReporter.CaptureUnregistryMessage(endpoint, "some-action")
})

It("increments the counter metric", func() {
Expect(sender.IncrementCounterCallCount()).To(Equal(1))
Expect(sender.IncrementCounterArgsForCall(0)).To(Equal("unregistry_message.oauth-server"))
Expect(sender.IncrementCounterArgsForCall(0)).To(Equal("unregistry_message.oauth-server.some-action"))
})

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

Expect(sender.IncrementCounterCallCount()).To(Equal(2))
Expect(sender.IncrementCounterArgsForCall(0)).To(Equal("unregistry_message.oauth-server"))
Expect(sender.IncrementCounterArgsForCall(1)).To(Equal("unregistry_message.api-server"))
Expect(sender.IncrementCounterArgsForCall(0)).To(Equal("unregistry_message.oauth-server.some-action"))
Expect(sender.IncrementCounterArgsForCall(1)).To(Equal("unregistry_message.api-server.some-action"))
})
})
Context("when unregister msg with empty component name is incremented", func() {
BeforeEach(func() {
endpoint = new(route.Endpoint)
endpoint.Tags = map[string]string{}
metricReporter.CaptureUnregistryMessage(endpoint)
metricReporter.CaptureUnregistryMessage(endpoint, "some-action")
})
It("increments the counter metric", func() {
Expect(sender.IncrementCounterCallCount()).To(Equal(1))
Expect(sender.IncrementCounterArgsForCall(0)).To(Equal("unregistry_message"))
Expect(sender.IncrementCounterArgsForCall(0)).To(Equal("unregistry_message.some-action"))
})
})
})
Expand Down
21 changes: 12 additions & 9 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,27 @@ func (r *RouteRegistry) Register(uri route.Uri, endpoint *route.Endpoint) {
return
}

endpointAdded := r.register(uri, endpoint)
endpointPutResult := r.register(uri, endpoint)

r.reporter.CaptureRegistryMessage(endpoint)

if endpointAdded == route.Added && !endpoint.UpdatedAt.IsZero() {
if endpointPutResult == route.Added && !endpoint.UpdatedAt.IsZero() {
r.reporter.CaptureRouteRegistrationLatency(time.Since(endpoint.UpdatedAt))
}

switch endpointAdded {
switch endpointPutResult {
case route.Added:
r.reporter.CaptureRegistryMessage(endpoint, "endpoint-added")
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
r.logger.Info("endpoint-registered", buildSlogAttrs(uri, endpoint)...)
r.logger.Info("endpoint-added", buildSlogAttrs(uri, endpoint)...)
}
case route.Updated:
r.reporter.CaptureRegistryMessage(endpoint, "endpoint-updated")
if r.logger.Enabled(context.Background(), slog.LevelDebug) {
r.logger.Debug("endpoint-registered", buildSlogAttrs(uri, endpoint)...)
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-registered", buildSlogAttrs(uri, endpoint)...)
r.logger.Debug("endpoint-not-updated", buildSlogAttrs(uri, endpoint)...)
}
}
}
Expand Down Expand Up @@ -167,7 +168,6 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
if !r.endpointInRouterShard(endpoint) {
return
}
r.reporter.CaptureUnregistryMessage(endpoint)

routeKey := uri.RouteKey()
endpointUnregisteredResult, pool := r.unregisterEndpoint(routeKey, endpoint)
Expand All @@ -176,10 +176,12 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
}
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.EndpointUnmodified:
r.reporter.CaptureUnregistryMessage(endpoint, "endpoint-not-unregistered")
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
r.logger.Info("endpoint-not-unregistered", buildSlogAttrs(routeKey, endpoint)...)
}
Expand All @@ -188,6 +190,7 @@ func (r *RouteRegistry) Unregister(uri route.Uri, endpoint *route.Endpoint) {
routeUnregisteredResult := r.deleteRouteWithoutEndpoint(routeKey, pool)
switch routeUnregisteredResult {
case route.RouteUnregistered:
r.reporter.CaptureUnregistryMessage(endpoint, "route-unregistered")
if r.logger.Enabled(context.Background(), slog.LevelInfo) {
r.logger.Info("route-unregistered", slog.Any("uri", routeKey))
}
Expand Down
Loading

0 comments on commit 2b89f38

Please sign in to comment.