Add optional TTL for *Vec children (MetricVec)#1986
Closed
jpinsonneau wants to merge 2 commits intoprometheus:mainfrom
Closed
Add optional TTL for *Vec children (MetricVec)#1986jpinsonneau wants to merge 2 commits intoprometheus:mainfrom
jpinsonneau wants to merge 2 commits intoprometheus:mainfrom
Conversation
When TTL is set on CounterOpts/GaugeOpts/HistogramOpts, Vec children that have not been accessed for longer than TTL are automatically excluded from Collect output. A CleanupExpired method is provided to explicitly remove expired entries from the internal map. This eliminates the need for external caches to track metric last-access times for expiration purposes. Made-with: Cursor Signed-off-by: Julien Pinsonneau <jpinsonn@redhat.com>
bd629c5 to
f1f5b91
Compare
Author
|
Closing in favor of #1989 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
High-cardinality and ephemeral label sets (for example per-pod or per-connection series) are painful to manage with today’s client: the usual approach is an external shadow cache plus periodic
Delete, which duplicates label storage and repeats the same pattern across projects. This is discussed in #1983 – Proposal: built-in TTL.What this PR does
TTLfield onOpts(used byCounterVec,GaugeVec, andHistogramVec) and threads it intoMetricVecviaNewMetricVecWithTTL.lastAccessedtimestamp (Unix ms). ZeroTTLkeeps behavior identical toNewMetricVec.Collectonly emits children whose last access is within the TTL window; older children are omitted from scrape output.(*MetricVec).CleanupExpired()walks the internal map and removes expired children, returning how many were deleted. Callers that care about memory (not only hiding series from scrapes) should invoke this on a schedule they control—similar in spirit to how statsd_exporter #164 expires series at the process boundary with a periodic pass.“Access” means resolving the child through the vector API (
GetMetricWith,GetMetricWithLabelValues,With,WithLabelValues, curried variants, etc.). Holding a child handle and only callingAdd/Set/Observeon it does not refresh TTL unless the instrumentation goes through theVecagain.SummaryVecis unchanged in this PR (still usesNewMetricVecwithout TTL). CustomMetricVectypes can opt in by constructing withNewMetricVecWithTTLand exposingCleanupExpiredif desired.Tests
Adds
vec_test.gocoverage for counter/gauge/histogram TTL, partial refresh, zero TTL, andGetMetricWith.Assisted-by:
claude-4.6-opus-high