diff --git a/storage/metric/memory.go b/storage/metric/memory.go index 996283243..1ea929dcf 100644 --- a/storage/metric/memory.go +++ b/storage/metric/memory.go @@ -231,9 +231,14 @@ func (s *memorySeriesStorage) CreateEmptySeries(metric clientmodel.Metric) { s.Lock() defer s.Unlock() + m := clientmodel.Metric{} + for label, value := range metric { + m[label] = value + } + fingerprint := &clientmodel.Fingerprint{} - fingerprint.LoadFromMetric(metric) - s.getOrCreateSeries(metric, fingerprint) + fingerprint.LoadFromMetric(m) + s.getOrCreateSeries(m, fingerprint) } func (s *memorySeriesStorage) getOrCreateSeries(metric clientmodel.Metric, fingerprint *clientmodel.Fingerprint) stream { diff --git a/storage/metric/tiered_test.go b/storage/metric/tiered_test.go index cd989e68e..2c61dd2d2 100644 --- a/storage/metric/tiered_test.go +++ b/storage/metric/tiered_test.go @@ -729,3 +729,42 @@ func testTruncateBefore(t test.Tester) { func TestTruncateBefore(t *testing.T) { testTruncateBefore(t) } + +func TestGetMetricForFingerprintCachesCopyOfMetric(t *testing.T) { + ts, closer := NewTestTieredStorage(t) + defer closer.Close() + + m := clientmodel.Metric{ + clientmodel.MetricNameLabel: "testmetric", + } + samples := clientmodel.Samples{ + &clientmodel.Sample{ + Metric: m, + Value: 0, + Timestamp: clientmodel.Now(), + }, + } + + if err := ts.AppendSamples(samples); err != nil { + t.Fatalf(err.Error()) + } + + ts.Flush() + + fp := &clientmodel.Fingerprint{} + fp.LoadFromMetric(m) + m, err := ts.GetMetricForFingerprint(fp) + if err != nil { + t.Fatalf(err.Error()) + } + + m[clientmodel.MetricNameLabel] = "changedmetric" + + m, err = ts.GetMetricForFingerprint(fp) + if err != nil { + t.Fatalf(err.Error()) + } + if m[clientmodel.MetricNameLabel] != "testmetric" { + t.Fatal("Metric name label value has changed: ", m[clientmodel.MetricNameLabel]) + } +}