Skip to content

Commit

Permalink
Simplify Delta ListMetrics implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Eckhart <[email protected]>
  • Loading branch information
kgeckhart committed Jan 18, 2023
1 parent fdfd20d commit 5196e13
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 66 deletions.
13 changes: 4 additions & 9 deletions collectors/delta_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DeltaCounterStore interface {
Increment(metricDescriptor *monitoring.MetricDescriptor, currentValue *ConstMetric)

// ListMetrics will return all known entries in the store for a metricDescriptorName
ListMetrics(metricDescriptorName string) map[string][]*CollectedMetric
ListMetrics(metricDescriptorName string) []*CollectedMetric
}

type metricEntry struct {
Expand Down Expand Up @@ -115,8 +115,8 @@ func toCounterKey(c *ConstMetric) uint64 {
return h
}

func (s *inMemoryDeltaCounterStore) ListMetrics(metricDescriptorName string) map[string][]*CollectedMetric {
output := map[string][]*CollectedMetric{}
func (s *inMemoryDeltaCounterStore) ListMetrics(metricDescriptorName string) []*CollectedMetric {
var output []*CollectedMetric
now := time.Now()
ttlWindowStart := now.Add(-s.ttl)

Expand All @@ -135,17 +135,12 @@ func (s *inMemoryDeltaCounterStore) ListMetrics(metricDescriptorName string) map
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)
output = append(output, &outputEntry)
}

return output
Expand Down
12 changes: 4 additions & 8 deletions collectors/delta_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DeltaDistributionStore interface {
Increment(metricDescriptor *monitoring.MetricDescriptor, currentValue *HistogramMetric)

// ListMetrics will return all known entries in the store for a metricDescriptorName
ListMetrics(metricDescriptorName string) map[string][]*CollectedHistogram
ListMetrics(metricDescriptorName string) []*CollectedHistogram
}

type histogramEntry struct {
Expand Down Expand Up @@ -135,8 +135,8 @@ func mergeHistograms(existing *HistogramMetric, current *HistogramMetric) *Histo
return current
}

func (s *inMemoryDeltaDistributionStore) ListMetrics(metricDescriptorName string) map[string][]*CollectedHistogram {
output := map[string][]*CollectedHistogram{}
func (s *inMemoryDeltaDistributionStore) ListMetrics(metricDescriptorName string) []*CollectedHistogram {
var output []*CollectedHistogram
now := time.Now()
ttlWindowStart := now.Add(-s.ttl)

Expand All @@ -156,16 +156,12 @@ func (s *inMemoryDeltaDistributionStore) ListMetrics(metricDescriptorName string
continue
}

metrics, exists := output[collected.histogram.FqName]
if !exists {
metrics = make([]*CollectedHistogram, 0)
}
histCopy := *collected.histogram
outputEntry := CollectedHistogram{
histogram: &histCopy,
lastCollectedAt: collected.lastCollectedAt,
}
output[collected.histogram.FqName] = append(metrics, &outputEntry)
output = append(output, &outputEntry)
}

return output
Expand Down
95 changes: 46 additions & 49 deletions collectors/monitoring_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,31 +253,29 @@ func (t *timeSeriesMetrics) completeDeltaConstMetrics(reportingStartTime time.Ti
now := time.Now().Truncate(time.Minute)

constMetrics := map[string][]*ConstMetric{}
for _, metrics := range descriptorMetrics {
for _, collected := range metrics {
// If the metric wasn't collected we should still export it at the next sample time to avoid staleness
if reportingStartTime.After(collected.lastCollectedAt) {
// Ideally we could use monitoring.MetricDescriptorMetadata.SamplePeriod to determine how many
// samples were missed to adjust this but monitoring.MetricDescriptorMetadata is viewed as optional
// for a monitoring.MetricDescriptor
reportingLag := collected.lastCollectedAt.Sub(collected.metric.ReportTime).Truncate(time.Minute)
collected.metric.ReportTime = now.Add(-reportingLag)
}
if t.fillMissingLabels {
if _, exists := constMetrics[collected.metric.FqName]; !exists {
constMetrics[collected.metric.FqName] = []*ConstMetric{}
}
constMetrics[collected.metric.FqName] = append(constMetrics[collected.metric.FqName], collected.metric)
} else {
t.ch <- t.newConstMetric(
collected.metric.FqName,
collected.metric.ReportTime,
collected.metric.LabelKeys,
collected.metric.ValueType,
collected.metric.Value,
collected.metric.LabelValues,
)
for _, collected := range descriptorMetrics {
// If the metric wasn't collected we should still export it at the next sample time to avoid staleness
if reportingStartTime.After(collected.lastCollectedAt) {
// Ideally we could use monitoring.MetricDescriptorMetadata.SamplePeriod to determine how many
// samples were missed to adjust this but monitoring.MetricDescriptorMetadata is viewed as optional
// for a monitoring.MetricDescriptor
reportingLag := collected.lastCollectedAt.Sub(collected.metric.ReportTime).Truncate(time.Minute)
collected.metric.ReportTime = now.Add(-reportingLag)
}
if t.fillMissingLabels {
if _, exists := constMetrics[collected.metric.FqName]; !exists {
constMetrics[collected.metric.FqName] = []*ConstMetric{}
}
constMetrics[collected.metric.FqName] = append(constMetrics[collected.metric.FqName], collected.metric)
} else {
t.ch <- t.newConstMetric(
collected.metric.FqName,
collected.metric.ReportTime,
collected.metric.LabelKeys,
collected.metric.ValueType,
collected.metric.Value,
collected.metric.LabelValues,
)
}
}

Expand All @@ -291,32 +289,31 @@ func (t *timeSeriesMetrics) completeDeltaHistogramMetrics(reportingStartTime tim
now := time.Now().Truncate(time.Minute)

histograms := map[string][]*HistogramMetric{}
for _, metrics := range descriptorMetrics {
for _, collected := range metrics {
// If the histogram wasn't collected we should still export it at the next sample time to avoid staleness
if reportingStartTime.After(collected.lastCollectedAt) {
// Ideally we could use monitoring.MetricDescriptorMetadata.SamplePeriod to determine how many
// samples were missed to adjust this but monitoring.MetricDescriptorMetadata is viewed as optional
// for a monitoring.MetricDescriptor
reportingLag := collected.lastCollectedAt.Sub(collected.histogram.ReportTime).Truncate(time.Minute)
collected.histogram.ReportTime = now.Add(-reportingLag)
}
if t.fillMissingLabels {
if _, exists := histograms[collected.histogram.FqName]; !exists {
histograms[collected.histogram.FqName] = []*HistogramMetric{}
}
histograms[collected.histogram.FqName] = append(histograms[collected.histogram.FqName], collected.histogram)
} else {
t.ch <- t.newConstHistogram(
collected.histogram.FqName,
collected.histogram.ReportTime,
collected.histogram.LabelKeys,
collected.histogram.Mean,
collected.histogram.Count,
collected.histogram.Buckets,
collected.histogram.LabelValues,
)
for _, collected := range descriptorMetrics {

// If the histogram wasn't collected we should still export it at the next sample time to avoid staleness
if reportingStartTime.After(collected.lastCollectedAt) {
// Ideally we could use monitoring.MetricDescriptorMetadata.SamplePeriod to determine how many
// samples were missed to adjust this but monitoring.MetricDescriptorMetadata is viewed as optional
// for a monitoring.MetricDescriptor
reportingLag := collected.lastCollectedAt.Sub(collected.histogram.ReportTime).Truncate(time.Minute)
collected.histogram.ReportTime = now.Add(-reportingLag)
}
if t.fillMissingLabels {
if _, exists := histograms[collected.histogram.FqName]; !exists {
histograms[collected.histogram.FqName] = []*HistogramMetric{}
}
histograms[collected.histogram.FqName] = append(histograms[collected.histogram.FqName], collected.histogram)
} else {
t.ch <- t.newConstHistogram(
collected.histogram.FqName,
collected.histogram.ReportTime,
collected.histogram.LabelKeys,
collected.histogram.Mean,
collected.histogram.Count,
collected.histogram.Buckets,
collected.histogram.LabelValues,
)
}
}

Expand Down

0 comments on commit 5196e13

Please sign in to comment.