-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Share string interner between scrape cache and remote_write #10231
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // Copyright 2022 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. | ||
| // | ||
| // Inspired / copied / modified from https://gitlab.com/cznic/strutil/blob/master/strutil.go, | ||
| // which is MIT licensed, so: | ||
| // | ||
| // Copyright (c) 2014 The strutil Authors. All rights reserved. | ||
|
|
||
| package intern | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "go.uber.org/atomic" | ||
|
|
||
| "github.com/prometheus/prometheus/model/labels" | ||
| ) | ||
|
|
||
| // Shared interner | ||
| var Global = New(prometheus.DefaultRegisterer) | ||
|
|
||
| // Interner is a string interner. | ||
| type Interner interface { | ||
| // Metrics returns Metrics for the interner. | ||
| Metrics() *Metrics | ||
|
|
||
| // Intern will intern an input string, returning the interned string as | ||
| // a result. | ||
| Intern(string) string | ||
|
|
||
| // Release removes an interned string from interner. | ||
| Release(string) | ||
| } | ||
|
|
||
| func New(r prometheus.Registerer) Interner { | ||
| return &pool{ | ||
| m: NewMetrics(r), | ||
| pool: map[string]*entry{}, | ||
| } | ||
| } | ||
|
|
||
| type Metrics struct { | ||
| Strings prometheus.Gauge | ||
| NoReferenceReleases prometheus.Counter | ||
| } | ||
|
|
||
| func NewMetrics(r prometheus.Registerer) *Metrics { | ||
| var m Metrics | ||
| m.Strings = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
| Namespace: "prometheus", | ||
| Subsystem: "interner", | ||
| Name: "num_strings", | ||
| Help: "The current number of interned strings", | ||
| }) | ||
| m.NoReferenceReleases = prometheus.NewCounter(prometheus.CounterOpts{ | ||
| Namespace: "prometheus", | ||
| Subsystem: "interner", | ||
| Name: "string_interner_zero_reference_releases_total", | ||
| Help: "The number of times release has been called for strings that are not interned.", | ||
| }) | ||
|
|
||
| if r != nil { | ||
| r.MustRegister(m.Strings) | ||
| r.MustRegister(m.NoReferenceReleases) | ||
| } | ||
|
|
||
| return &m | ||
| } | ||
|
|
||
| type pool struct { | ||
| m *Metrics | ||
|
|
||
| mtx sync.RWMutex | ||
| pool map[string]*entry | ||
| } | ||
|
|
||
| type entry struct { | ||
| refs atomic.Int64 | ||
|
|
||
| s string | ||
| } | ||
|
|
||
| func newEntry(s string) *entry { | ||
| return &entry{s: s} | ||
| } | ||
|
|
||
| func (p *pool) Metrics() *Metrics { return p.m } | ||
|
|
||
| func (p *pool) Intern(s string) string { | ||
| if s == "" { | ||
| return "" | ||
| } | ||
|
|
||
| p.mtx.RLock() | ||
| interned, ok := p.pool[s] | ||
| p.mtx.RUnlock() | ||
| if ok { | ||
| interned.refs.Inc() | ||
| return interned.s | ||
| } | ||
| p.mtx.Lock() | ||
| defer p.mtx.Unlock() | ||
| if interned, ok := p.pool[s]; ok { | ||
| interned.refs.Inc() | ||
| return interned.s | ||
| } | ||
|
|
||
| p.m.Strings.Inc() | ||
| p.pool[s] = newEntry(s) | ||
| p.pool[s].refs.Store(1) | ||
| return s | ||
| } | ||
|
|
||
| func (p *pool) Release(s string) { | ||
| p.mtx.RLock() | ||
| interned, ok := p.pool[s] | ||
| p.mtx.RUnlock() | ||
|
|
||
| if !ok { | ||
| p.m.NoReferenceReleases.Inc() | ||
| return | ||
| } | ||
|
|
||
| refs := interned.refs.Dec() | ||
| if refs > 0 { | ||
| return | ||
| } | ||
|
|
||
| p.mtx.Lock() | ||
| defer p.mtx.Unlock() | ||
| if interned.refs.Load() != 0 { | ||
| return | ||
| } | ||
| p.m.Strings.Dec() | ||
| delete(p.pool, s) | ||
| } | ||
|
|
||
| // Intern is a helper function for interning all label names and values to a | ||
| // given interner. | ||
| func Intern(interner Interner, lbls labels.Labels) { | ||
| for i, l := range lbls { | ||
| lbls[i].Name = interner.Intern(l.Name) | ||
| lbls[i].Value = interner.Intern(l.Value) | ||
| } | ||
| } | ||
|
|
||
| // Release is a helper function for releasing all label names and values from a | ||
| // given interner. | ||
| func Release(interner Interner, ls labels.Labels) { | ||
| for _, l := range ls { | ||
| interner.Release(l.Name) | ||
| interner.Release(l.Value) | ||
| } | ||
| } | ||
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
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.
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.
what reason does this need to be exported? I think we should either pass in the registerer used elsewhere in the code, or if one is not passed just default to use
prometheus.DefaultRegistererUh oh!
There was an error while loading. Please reload this page.
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.
This
Globalinterner is what's being re-used between thescrapeandremotepackages.Could you explain your suggestion once more, please? Do you mean leaving it up to the New() function to decide whether we're using the Global interner (if the DefaultRegisterer is passed), or creating another interner (if a different registerer is passed)? Something else?