-
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
384151c
d396d42
cf4b9e9
e8d0bdf
42f3524
0ff0ef7
49d10c3
f366f3e
8d33a81
12af472
649400b
2b5c23b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2014 The Prometheus Authors | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2014?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, bad copy paste. The new ttl.go uses the proper copyright header 👍 |
||
| // 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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Registry is for registering metrics and then gathering registered ones. This API breaks this by having a registry with tons of New* constructors. Are we sure this will not confuse users? LLM review also captures this well in this point: https://gist.github.com/bwplotka/694d6c4a9be64e629d5c7b2f3541319b#a-coupling-constructors-to-registry
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's indeed also false impression that MustRegister would suddenly start TTL tracking: https://gist.github.com/bwplotka/694d6c4a9be64e629d5c7b2f3541319b#b-incomplete--error-prone-registration-surface
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Removed TTLRegistry entirely. Construction stays on V2.New*Vec(...Opts{TTL: ...}); registration/gathering stay on a normal Registry. See 49d10c3. |
||
| *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) | ||
| } | ||
|
kakkoyun marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // 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" | ||
| "testing/synctest" | ||
| "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) { | ||
| synctest.Test(t, func(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) | ||
|
|
||
| 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) | ||
| } | ||
| }) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure this is what we want? How this will scale when we will have another option,
newCounterVecWithTTLWithX?If we have opts, why not using it with TTL for this purpose? Would it make sense to add
ttlfor those cases?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Dropped the *WithTTL helpers.
TTL now lives on CounterVecOpts / GaugeVecOpts / HistogramVecOpts / SummaryVecOpts / MetricVecOpts, e.g.:
See 8d33a81 (and SummaryVec in 649400b).