diff --git a/storage/local/chunk.go b/storage/local/chunk.go index 52e13eecef..112198c0de 100644 --- a/storage/local/chunk.go +++ b/storage/local/chunk.go @@ -142,6 +142,20 @@ func (cd *chunkDesc) lastTime() clientmodel.Timestamp { return cd.c.newIterator().lastTimestamp() } +func (cd *chunkDesc) lastSamplePair() *metric.SamplePair { + cd.Lock() + defer cd.Unlock() + + if cd.c == nil { + return nil + } + it := cd.c.newIterator() + return &metric.SamplePair{ + Timestamp: it.lastTimestamp(), + Value: it.lastSampleValue(), + } +} + func (cd *chunkDesc) isEvicted() bool { cd.Lock() defer cd.Unlock() diff --git a/storage/local/interface.go b/storage/local/interface.go index 71fa5ad838..245fe33cc5 100644 --- a/storage/local/interface.go +++ b/storage/local/interface.go @@ -40,6 +40,9 @@ type Storage interface { // label matchers. At least one label matcher must be specified that does not // match the empty string. MetricsForLabelMatchers(matchers ...*metric.LabelMatcher) map[clientmodel.Fingerprint]clientmodel.COWMetric + // LastSamplePairForFingerprint returns the last sample pair for the provided fingerprint. + // If the respective time series is evicted, nil is returned. + LastSamplePairForFingerprint(clientmodel.Fingerprint) *metric.SamplePair // Get all of the label values that are associated with a given label name. LabelValuesForLabelName(clientmodel.LabelName) clientmodel.LabelValues // Get the metric associated with the provided fingerprint. diff --git a/storage/local/storage.go b/storage/local/storage.go index 76f961bf98..66b7aa961b 100644 --- a/storage/local/storage.go +++ b/storage/local/storage.go @@ -301,7 +301,7 @@ func (s *memorySeriesStorage) WaitForIndexing() { s.persistence.waitForIndexing() } -// NewIterator implements storage. +// NewIterator implements Storage. func (s *memorySeriesStorage) NewIterator(fp clientmodel.Fingerprint) SeriesIterator { s.fpLocker.Lock(fp) defer s.fpLocker.Unlock(fp) @@ -321,6 +321,18 @@ func (s *memorySeriesStorage) NewIterator(fp clientmodel.Fingerprint) SeriesIter } } +// LastSampleForFingerprint implements Storage. +func (s *memorySeriesStorage) LastSamplePairForFingerprint(fp clientmodel.Fingerprint) *metric.SamplePair { + s.fpLocker.Lock(fp) + defer s.fpLocker.Unlock(fp) + + series, ok := s.fpToSeries.get(fp) + if !ok { + return nil + } + return series.head().lastSamplePair() +} + // boundedIterator wraps a SeriesIterator and does not allow fetching // data from earlier than the configured start time. type boundedIterator struct {