Skip to content

Make the debug metrics endpoints leader aware#192

Open
jakubpliszka wants to merge 2 commits into
masterfrom
jakubpliszka/leader-aware-metrics
Open

Make the debug metrics endpoints leader aware#192
jakubpliszka wants to merge 2 commits into
masterfrom
jakubpliszka/leader-aware-metrics

Conversation

@jakubpliszka

Copy link
Copy Markdown

This pull requests omits aggregated.* and probes.* metrics on followers.

After a leader is demoted, active metric collection stops, but previously exported values remain in Go’s global expvar registry. Consequently, Datadog continues observing frozen aggregate gauges and zero-rate probe counters from the former leader.

Removing metrics from go-metrics does not remove values already published to expvar, so filtering is applied when rendering the metrics response.

Copilot AI review requested due to automatic review settings July 16, 2026 12:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the debug metrics endpoints leader-aware by filtering out aggregated.* and probes.* metrics on follower nodes, preventing Datadog from observing stale/frozen expvar values after a leader demotion.

Changes:

  • Replaces the old /debug/vars and /debug/metrics handler with an APIImpl.Metrics method that filters aggregated.* and probes.* when not leader.
  • Forces registry metrics to be (re)published into Go’s global expvar registry before rendering the filtered JSON response.
  • Adds a unit test to verify expvar values persist after unregister and are filtered on followers.
Show a summary per file
File Description
pkg/http/api.go Adds a leader-aware Metrics handler and routes /debug/vars and /debug/metrics through it with filtering.
pkg/http/api_test.go Adds coverage for leader vs follower behavior and expvar persistence after registry unregister.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 3
  • Review effort level: Low

Comment thread pkg/http/api.go
Comment thread pkg/http/api.go Outdated
Comment thread pkg/http/api.go Outdated
Comment on lines +368 to +372
func (api *APIImpl) Metrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
exp.ExpHandler(metrics.DefaultRegistry).ServeHTTP(discardWriter{Writer: io.Discard}, r)
isLeader := api.consensusService != nil && api.consensusService.IsLeader()

w.Header().Set("Content-Type", "application/json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants