-
Notifications
You must be signed in to change notification settings - Fork 111
Track delta metrics between scrapes #168
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
Merged
SuperQ
merged 8 commits into
prometheus-community:master
from
kgeckhart:keckhart/track-delta-metrics-between-scrapes
Nov 22, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21512c5
Support delta metrics which are counters with an in-memory store
kgeckhart 9c541a8
Introduce a delta distribution store and refactor existing store to c…
kgeckhart 61209c4
Only consider descriptors which were collected and add config flags
kgeckhart d26d63e
Add go docs, a section in the readme, and make sure the aggregateDelt…
kgeckhart a28d78e
Minor clarification on where the initial 0 value comes from
kgeckhart ffb190b
Locking at the entry level just in case external implementation changes
kgeckhart aefd076
Use sync.Map to fix concurrent read/write issue
kgeckhart d070558
Apply suggestions from code review
kgeckhart 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,152 @@ | ||
| // 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. | ||
|
|
||
| package collectors | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/go-kit/log/level" | ||
| "google.golang.org/api/monitoring/v3" | ||
| ) | ||
|
|
||
| type CollectedMetric struct { | ||
| metric *ConstMetric | ||
| lastCollectedAt time.Time | ||
| } | ||
|
|
||
| // DeltaCounterStore defines a set of functions which must be implemented in order to be used as a DeltaCounterStore | ||
| // which accumulates DELTA Counter metrics over time | ||
| type DeltaCounterStore interface { | ||
|
|
||
| // Increment will use the incoming metricDescriptor and currentValue to either create a new entry or add the incoming | ||
| // value to an existing entry in the underlying store | ||
| Increment(metricDescriptor *monitoring.MetricDescriptor, currentValue *ConstMetric) | ||
|
|
||
| // ListMetrics will return all known entries in the store for a metricDescriptorName | ||
| ListMetrics(metricDescriptorName string) map[string][]*CollectedMetric | ||
| } | ||
|
|
||
| type metricEntry struct { | ||
| collected map[uint64]*CollectedMetric | ||
| mutex *sync.RWMutex | ||
| } | ||
|
|
||
| type inMemoryDeltaCounterStore struct { | ||
| store *sync.Map | ||
| ttl time.Duration | ||
| logger log.Logger | ||
| } | ||
|
|
||
| // NewInMemoryDeltaCounterStore returns an implementation of DeltaCounterStore which is persisted in-memory | ||
| func NewInMemoryDeltaCounterStore(logger log.Logger, ttl time.Duration) DeltaCounterStore { | ||
| return &inMemoryDeltaCounterStore{ | ||
| store: &sync.Map{}, | ||
| logger: logger, | ||
| ttl: ttl, | ||
| } | ||
| } | ||
|
|
||
| func (s *inMemoryDeltaCounterStore) Increment(metricDescriptor *monitoring.MetricDescriptor, currentValue *ConstMetric) { | ||
| if currentValue == nil { | ||
| return | ||
| } | ||
|
|
||
| tmp, _ := s.store.LoadOrStore(metricDescriptor.Name, &metricEntry{ | ||
| collected: map[uint64]*CollectedMetric{}, | ||
| mutex: &sync.RWMutex{}, | ||
| }) | ||
| entry := tmp.(*metricEntry) | ||
|
|
||
| key := toCounterKey(currentValue) | ||
|
|
||
| entry.mutex.Lock() | ||
| defer entry.mutex.Unlock() | ||
| existing := entry.collected[key] | ||
|
|
||
| if existing == nil { | ||
| level.Debug(s.logger).Log("msg", "Tracking new counter", "fqName", currentValue.fqName, "key", key, "current_value", currentValue.value, "incoming_time", currentValue.reportTime) | ||
| entry.collected[key] = &CollectedMetric{currentValue, time.Now()} | ||
| return | ||
| } | ||
|
|
||
| if existing.metric.reportTime.Before(currentValue.reportTime) { | ||
| level.Debug(s.logger).Log("msg", "Incrementing existing counter", "fqName", currentValue.fqName, "key", key, "current_value", existing.metric.value, "adding", currentValue.value, "last_reported_time", entry.collected[key].metric.reportTime, "incoming_time", currentValue.reportTime) | ||
| currentValue.value = currentValue.value + existing.metric.value | ||
| existing.metric = currentValue | ||
| existing.lastCollectedAt = time.Now() | ||
| return | ||
| } | ||
|
|
||
| level.Debug(s.logger).Log("msg", "Ignoring old sample for counter", "fqName", currentValue.fqName, "key", key, "last_reported_time", existing.metric.reportTime, "incoming_time", currentValue.reportTime) | ||
| } | ||
|
|
||
| func toCounterKey(c *ConstMetric) uint64 { | ||
| labels := make(map[string]string) | ||
| keysCopy := append([]string{}, c.labelKeys...) | ||
| for i := range c.labelKeys { | ||
| labels[c.labelKeys[i]] = c.labelValues[i] | ||
| } | ||
| sort.Strings(keysCopy) | ||
|
|
||
| var keyParts []string | ||
| for _, k := range keysCopy { | ||
| keyParts = append(keyParts, fmt.Sprintf("%s:%s", k, labels[k])) | ||
| } | ||
| hashText := fmt.Sprintf("%s|%s", c.fqName, strings.Join(keyParts, "|")) | ||
| h := hashNew() | ||
| h = hashAdd(h, hashText) | ||
|
|
||
| return h | ||
| } | ||
|
|
||
| func (s *inMemoryDeltaCounterStore) ListMetrics(metricDescriptorName string) map[string][]*CollectedMetric { | ||
| output := map[string][]*CollectedMetric{} | ||
| now := time.Now() | ||
| ttlWindowStart := now.Add(-s.ttl) | ||
|
|
||
| tmp, exists := s.store.Load(metricDescriptorName) | ||
| if !exists { | ||
| return output | ||
| } | ||
| entry := tmp.(*metricEntry) | ||
|
|
||
| entry.mutex.Lock() | ||
| defer entry.mutex.Unlock() | ||
| for key, collected := range entry.collected { | ||
| //Scan and remove metrics which are outside the TTL | ||
| if ttlWindowStart.After(collected.lastCollectedAt) { | ||
| level.Debug(s.logger).Log("msg", "Deleting counter entry outside of TTL", "key", key, "fqName", collected.metric.fqName) | ||
| delete(entry.collected, key) | ||
| continue | ||
| } | ||
|
|
||
| metrics, exists := output[collected.metric.fqName] | ||
| if !exists { | ||
| metrics = make([]*CollectedMetric, 0) | ||
| } | ||
| metricCopy := *collected.metric | ||
| outputEntry := CollectedMetric{ | ||
| metric: &metricCopy, | ||
| lastCollectedAt: collected.lastCollectedAt, | ||
| } | ||
| output[collected.metric.fqName] = append(metrics, &outputEntry) | ||
| } | ||
|
|
||
| return output | ||
| } |
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.