-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add opt-in Vec TTL via MetricVec and TTLRegistry #1989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jpinsonneau
wants to merge
4
commits into
prometheus:main
Choose a base branch
from
jpinsonneau:ttl-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
384151c
Add opt-in Vec TTL via MetricVec and TTLRegistry
jpinsonneau d396d42
Merge branch 'main' into ttl-registry
jpinsonneau cf4b9e9
Separate TTL Vec path and use synctest in TTL tests
jpinsonneau e8d0bdf
Fix TTL edge cases from review feedback
jpinsonneau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2014 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package prometheus | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
|
|
||
| dto "github.com/prometheus/client_model/go" | ||
| ) | ||
|
|
||
| // TTLRegistry is a dedicated Prometheus registry for metrics that need | ||
| // time-to-live behavior on *Vec children. It embeds a plain *Registry and adds: | ||
| // - Vec constructors that enable per-child TTL (same semantics as MetricVec | ||
| // built with NewMetricVecWithTTL). | ||
| // - Automatic CleanupExpired on all Vecs created through this registry before | ||
| // each Gather, so memory can be reclaimed even when scrapes are infrequent | ||
| // (see discussion in https://github.com/prometheus/client_golang/issues/1983). | ||
| // | ||
| // Default prometheus.NewRegistry, NewCounterVec, and Opts are unchanged; use | ||
| // TTLRegistry only when you explicitly opt in to Vec TTL. | ||
| type TTLRegistry struct { | ||
| *Registry | ||
|
|
||
| ttl time.Duration | ||
|
|
||
| mu sync.Mutex | ||
| vecs []*MetricVec | ||
| } | ||
|
|
||
| // NewTTLRegistry returns a registry backed by a new empty *Registry. ttl must | ||
| // be greater than zero; it applies to every Vec created via this registry's | ||
| // constructor methods. | ||
| func NewTTLRegistry(ttl time.Duration) *TTLRegistry { | ||
| if ttl <= 0 { | ||
| panic("NewTTLRegistry: ttl must be > 0") | ||
| } | ||
| return &TTLRegistry{ | ||
| Registry: NewRegistry(), | ||
| ttl: ttl, | ||
| } | ||
| } | ||
|
|
||
| func (r *TTLRegistry) track(mv *MetricVec) { | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
| r.vecs = append(r.vecs, mv) | ||
| } | ||
|
|
||
| func (r *TTLRegistry) runCleanup() { | ||
| r.mu.Lock() | ||
| vecs := append([]*MetricVec(nil), r.vecs...) | ||
| r.mu.Unlock() | ||
| for _, mv := range vecs { | ||
| mv.CleanupExpired() | ||
| } | ||
| } | ||
|
|
||
| // Gather implements Gatherer. It runs CleanupExpired on all Vecs created | ||
| // through this TTLRegistry, then delegates to the embedded Registry. | ||
| func (r *TTLRegistry) Gather() ([]*dto.MetricFamily, error) { | ||
| r.runCleanup() | ||
| return r.Registry.Gather() | ||
| } | ||
|
|
||
| // NewCounterVec is like prometheus.NewCounterVec but enables Vec TTL using this | ||
| // registry's ttl, registers the Vec, and tracks it for Gather-time cleanup. | ||
| func (r *TTLRegistry) NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec { | ||
| return r.NewCounterVecOpts(CounterVecOpts{ | ||
| CounterOpts: opts, | ||
| VariableLabels: UnconstrainedLabels(labelNames), | ||
| }) | ||
| } | ||
|
|
||
| // NewCounterVecOpts is like V2.NewCounterVec with TTL and automatic registration. | ||
| func (r *TTLRegistry) NewCounterVecOpts(opts CounterVecOpts) *CounterVec { | ||
| cv := newCounterVecWithTTL(opts, r.ttl) | ||
| r.MustRegister(cv) | ||
| r.track(cv.MetricVec) | ||
| return cv | ||
| } | ||
|
|
||
| // NewGaugeVec is like prometheus.NewGaugeVec with TTL, registration, and tracking. | ||
| func (r *TTLRegistry) NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec { | ||
| return r.NewGaugeVecOpts(GaugeVecOpts{ | ||
| GaugeOpts: opts, | ||
| VariableLabels: UnconstrainedLabels(labelNames), | ||
| }) | ||
| } | ||
|
|
||
| // NewGaugeVecOpts is like V2.NewGaugeVec with TTL and automatic registration. | ||
| func (r *TTLRegistry) NewGaugeVecOpts(opts GaugeVecOpts) *GaugeVec { | ||
| gv := newGaugeVecWithTTL(opts, r.ttl) | ||
| r.MustRegister(gv) | ||
| r.track(gv.MetricVec) | ||
| return gv | ||
| } | ||
|
|
||
| // NewHistogramVec is like prometheus.NewHistogramVec with TTL, registration, and tracking. | ||
| func (r *TTLRegistry) NewHistogramVec(opts HistogramOpts, labelNames []string) *HistogramVec { | ||
| return r.NewHistogramVecOpts(HistogramVecOpts{ | ||
| HistogramOpts: opts, | ||
| VariableLabels: UnconstrainedLabels(labelNames), | ||
| }) | ||
| } | ||
|
|
||
| // NewHistogramVecOpts is like V2.NewHistogramVec with TTL and automatic registration. | ||
| func (r *TTLRegistry) NewHistogramVecOpts(opts HistogramVecOpts) *HistogramVec { | ||
| hv := newHistogramVecWithTTL(opts, r.ttl) | ||
| r.MustRegister(hv) | ||
| r.track(hv.MetricVec) | ||
| return hv | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2014 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package prometheus | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestNewTTLRegistryPanicsOnNonPositiveTTL(t *testing.T) { | ||
| defer func() { | ||
| if recover() == nil { | ||
| t.Fatal("expected panic for ttl <= 0") | ||
| } | ||
| }() | ||
| NewTTLRegistry(0) | ||
| } | ||
|
|
||
| func TestTTLRegistryGatherRunsCleanup(t *testing.T) { | ||
| ttl := 80 * time.Millisecond | ||
| reg := NewTTLRegistry(ttl) | ||
| vec := reg.NewCounterVec(CounterOpts{ | ||
| Name: "ttl_reg_gather", | ||
| Help: "test", | ||
| }, []string{"code"}) | ||
|
|
||
| vec.WithLabelValues("200").Add(1) | ||
| if n := collectCount(vec); n != 1 { | ||
| t.Fatalf("expected 1 metric before sleep, got %d", n) | ||
| } | ||
|
|
||
| time.Sleep(ttl + 40*time.Millisecond) | ||
|
kakkoyun marked this conversation as resolved.
Outdated
|
||
|
|
||
| if _, err := reg.Gather(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if n := collectCount(vec); n != 0 { | ||
| t.Fatalf("expected 0 metrics after Gather cleanup, got %d", n) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.