fix: copyMetrics to avoid map references elsewhere (#14113)

map labels might have been referenced else, this
can lead to concurrent access at lower layers.

avoid this by copying the information while
concurrently serving the metrics.
This commit is contained in:
Harshavardhana 2022-01-14 16:48:19 -08:00 committed by GitHub
parent b106b1c131
commit ba708f51f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -226,10 +226,31 @@ func (g *MetricsGroup) RegisterRead(read func(ctx context.Context) []Metric) {
})
}
func (m *Metric) copyMetric() Metric {
metric := Metric{
Description: m.Description,
Value: m.Value,
HistogramBucketLabel: m.HistogramBucketLabel,
StaticLabels: make(map[string]string),
VariableLabels: make(map[string]string),
Histogram: make(map[string]uint64),
}
for k, v := range m.StaticLabels {
metric.StaticLabels[k] = v
}
for k, v := range m.VariableLabels {
metric.VariableLabels[k] = v
}
for k, v := range m.Histogram {
metric.Histogram[k] = v
}
return metric
}
// Get - returns cached value always upton the configured TTL,
// once the TTL expires "read()" registered function is called
// to return the new values and updated.
func (g *MetricsGroup) Get() []Metric {
func (g *MetricsGroup) Get() (metrics []Metric) {
var c interface{}
var err error
if g.cacheInterval <= 0 {
@ -247,7 +268,11 @@ func (g *MetricsGroup) Get() []Metric {
return []Metric{}
}
return m
metrics = make([]Metric, 0, len(m))
for i := range m {
metrics = append(metrics, m[i].copyMetric())
}
return metrics
}
func getClusterCapacityTotalBytesMD() MetricDescription {