Skip to content

*: fix metrics high cardinality risks#4581

Open
pinebit wants to merge 2 commits into
mainfrom
pinebit/metrics-cardinality-fix
Open

*: fix metrics high cardinality risks#4581
pinebit wants to merge 2 commits into
mainfrom
pinebit/metrics-cardinality-fix

Conversation

@pinebit

@pinebit pinebit commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Fixes high-cardinality risks found while reviewing all Prometheus metrics against the code that populates them:

  • cmd/relay: delete all relay_p2p_* series of a peer when it disconnects. The relay accepts connections from arbitrary peers and clusters (open gater) and never deleted series, so a long-running relay accumulated (peer, peer_cluster) series indefinitely.
  • app/health: the high-cardinality check compared the number of label keys per series (~10) against 100 * numValidators, so it never fired. It now compares the series count per metric family.
  • core/validatorapi: truncate the untrusted User-Agent header to 128 bytes and sanitize it to valid UTF-8 before using it as a label value.
  • app/promauto: ResetGaugeVec no longer panics on label values containing | and Reset now matches exact leading label values instead of substrings anywhere, which could delete series of an unrelated peer whose labels contained the given value as a substring.

category: bug
ticket: none

🤖 Generated with Claude Code

@pinebit pinebit requested a review from KaloyanTanev July 4, 2026 05:25
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.38%. Comparing base (8e3bd59) to head (c313df1).

Files with missing lines Patch % Lines
core/validatorapi/router.go 33.33% 1 Missing and 1 partial ⚠️
cmd/relay/p2p.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4581      +/-   ##
==========================================
+ Coverage   57.34%   57.38%   +0.03%     
==========================================
  Files         245      245              
  Lines       33554    33556       +2     
==========================================
+ Hits        19243    19256      +13     
+ Misses      11875    11854      -21     
- Partials     2436     2446      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pinebit pinebit force-pushed the pinebit/metrics-cardinality-fix branch from 4f552cc to 56b0a62 Compare July 6, 2026 12:45
Comment thread cmd/relay/p2p.go
if state.ClusterHash != "" {
activeConnsCounter.WithLabelValues(state.Name, state.ClusterHash).Set(0)
}
deletePeerMetrics(state.Name)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wouldn't that delete historical metrics for a peer if it goes offline for a minute?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Historical data is safe, deleting a series from the client process only removes it from the live /metrics output. Everything Prometheus already scraped stays in its TSDB, so dashboards querying past ranges are unaffected.

Comment thread core/scheduler/scheduler.go Outdated
Delete relay_p2p_* series when peers disconnect, delete scheduler
validator series no longer reported by the beacon node, fix the
high-cardinality health check to compare series count instead of
label count, and bound the User-Agent metric label value.
ResetGaugeVec now matches exact leading label values and no longer
panics on label values containing the separator.

category: bug
ticket: none

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pinebit pinebit force-pushed the pinebit/metrics-cardinality-fix branch from 56b0a62 to d3c35ff Compare July 6, 2026 14:44
Keep the PR minimal per review feedback: validator series cardinality
is bounded by the cluster validator count, so the sweep is not needed
to prevent unbounded growth.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Jul 6, 2026

Copy link
Copy Markdown

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