diff --git a/model/metric.go b/model/metric.go index 9527a5c18..6289f9057 100644 --- a/model/metric.go +++ b/model/metric.go @@ -17,6 +17,7 @@ import ( "bytes" "code.google.com/p/goprotobuf/proto" "fmt" + dto "github.com/prometheus/prometheus/model/generated" "sort" "time" ) @@ -135,6 +136,17 @@ func (v Values) InsideInterval(t time.Time) (s bool) { return true } +func NewValuesFromDTO(dto *dto.SampleValueSeries) (v Values) { + for _, value := range dto.Value { + v = append(v, SamplePair{ + Timestamp: time.Unix(*value.Timestamp, 0), + Value: SampleValue(*value.Value), + }) + } + + return +} + type SampleSet struct { Metric Metric Values Values diff --git a/storage/metric/leveldb.go b/storage/metric/leveldb.go index 36022be60..653827735 100644 --- a/storage/metric/leveldb.go +++ b/storage/metric/leveldb.go @@ -656,9 +656,14 @@ func extractSampleKey(i leveldb.Iterator) (key model.SampleKey, err error) { return } -func extractSampleValues(i leveldb.Iterator) (v *dto.SampleValueSeries, err error) { - v = &dto.SampleValueSeries{} +func extractSampleValues(i leveldb.Iterator) (values model.Values, err error) { + v := &dto.SampleValueSeries{} err = proto.Unmarshal(i.Value(), v) + if err != nil { + return + } + + values = model.NewValuesFromDTO(v) return } diff --git a/storage/metric/stochastic_test.go b/storage/metric/stochastic_test.go index cdd4f43bb..9e19869a5 100644 --- a/storage/metric/stochastic_test.go +++ b/storage/metric/stochastic_test.go @@ -216,17 +216,11 @@ func levelDBGetRangeValues(l *LevelDBMetricPersistence, fp model.Fingerprint, i break } - retrievedValue, err := extractSampleValues(iterator) + retrievedValues, err := extractSampleValues(iterator) if err != nil { return nil, err } - - for _, value := range retrievedValue.Value { - samples = append(samples, model.SamplePair{ - Value: model.SampleValue(*value.Value), - Timestamp: time.Unix(*value.Timestamp, 0), - }) - } + samples = append(samples, retrievedValues...) } return @@ -463,15 +457,15 @@ func StochasticTests(persistenceMaker func() (MetricPersistence, test.Closer), t samples, err = levelDBGetRangeValues(persistence, fp, interval) if err != nil { t.Fatal(err) - return + } + if len(samples) < 2 { + t.Fatalf("expected sample count greater than %d, got %d", 2, len(samples)) } default: samples = p.GetRangeValues(fp, interval) - } - - if len(samples) < 2 { - t.Errorf("expected sample count less than %d, got %d", 2, len(samples)) - return + if len(samples) < 2 { + t.Fatalf("expected sample count greater than %d, got %d", 2, len(samples)) + } } } } diff --git a/storage/metric/tiered.go b/storage/metric/tiered.go index 8b69b8e76..5cf1b5bd0 100644 --- a/storage/metric/tiered.go +++ b/storage/metric/tiered.go @@ -472,8 +472,8 @@ func (t *tieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier targetKey = &dto.SampleKey{ Fingerprint: fingerprint.ToDTO(), } - foundKey model.SampleKey - foundValue *dto.SampleValueSeries + foundKey model.SampleKey + foundValues model.Values ) // Limit the target key to be within the series' keyspace. @@ -510,9 +510,9 @@ func (t *tieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier rewound = true } - foundValue, err = extractSampleValues(iterator) + foundValues, err = extractSampleValues(iterator) if err != nil { - panic(err) + return } // If we rewound, but the target time is still past the current block, return @@ -520,33 +520,25 @@ func (t *tieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier if rewound { foundKey, err = extractSampleKey(iterator) if err != nil { - panic(err) + return } currentChunkLastTime := foundKey.LastTimestamp if ts.After(currentChunkLastTime) { - sampleCount := len(foundValue.Value) - chunk = append(chunk, model.SamplePair{ - Timestamp: time.Unix(*foundValue.Value[sampleCount-1].Timestamp, 0), - Value: model.SampleValue(*foundValue.Value[sampleCount-1].Value), - }) + sampleCount := len(foundValues) + chunk = append(chunk, foundValues[sampleCount-1]) // We know there's a next block since we have rewound from it. iterator.Next() - foundValue, err = extractSampleValues(iterator) + foundValues, err = extractSampleValues(iterator) if err != nil { - panic(err) + return } } } // Now append all the samples of the currently seeked block to the output. - for _, sample := range foundValue.Value { - chunk = append(chunk, model.SamplePair{ - Timestamp: time.Unix(*sample.Timestamp, 0), - Value: model.SampleValue(*sample.Value), - }) - } + chunk = append(chunk, foundValues...) return }