Skip to content

Add optional TTL for *Vec children (MetricVec)#1986

Closed
jpinsonneau wants to merge 2 commits intoprometheus:mainfrom
jpinsonneau:ttl-support
Closed

Add optional TTL for *Vec children (MetricVec)#1986
jpinsonneau wants to merge 2 commits intoprometheus:mainfrom
jpinsonneau:ttl-support

Conversation

@jpinsonneau
Copy link
Copy Markdown

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

  • Adds an optional TTL field on Opts (used by CounterVec, GaugeVec, and HistogramVec) and threads it into MetricVec via NewMetricVecWithTTL.
  • Each child in the vector keeps an atomic lastAccessed timestamp (Unix ms). Zero TTL keeps behavior identical to NewMetricVec.
  • Collect only 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 calling Add / Set / Observe on it does not refresh TTL unless the instrumentation goes through the Vec again.

SummaryVec is unchanged in this PR (still uses NewMetricVec without TTL). Custom MetricVec types can opt in by constructing with NewMetricVecWithTTL and exposing CleanupExpired if desired.

Tests

Adds vec_test.go coverage for counter/gauge/histogram TTL, partial refresh, zero TTL, and GetMetricWith.


Assisted-by: claude-4.6-opus-high

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>
Signed-off-by: Julien Pinsonneau <jpinsonn@redhat.com>
Copy link
Copy Markdown
Member

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

Please see #1983 (comment)

@jpinsonneau
Copy link
Copy Markdown
Author

Closing in favor of #1989

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