From d9df8efbd4e3ad4282b5371550f40d452f5d1bfc Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Fri, 21 Jul 2023 18:56:54 +0200 Subject: [PATCH] Add LabelQuerier.LabelValuesSet method Signed-off-by: Arve Knudsen --- storage/fanout_test.go | 4 ++++ storage/interface.go | 23 +++++++++++++++++++++++ storage/merge_test.go | 10 ++++++++++ storage/noop.go | 11 +++++++++++ storage/remote/read.go | 6 ++++++ storage/secondary.go | 5 +++++ 6 files changed, 59 insertions(+) diff --git a/storage/fanout_test.go b/storage/fanout_test.go index b4490636df..7d66efb7a8 100644 --- a/storage/fanout_test.go +++ b/storage/fanout_test.go @@ -237,6 +237,10 @@ func (errQuerier) LabelValues(string, ...*labels.Matcher) ([]string, storage.War return nil, nil, errors.New("label values error") } +func (errQuerier) LabelValuesStream(string, ...*labels.Matcher) (LabelValuesSet, error) { + return nil, errors.New("label values set error") +} + func (errQuerier) LabelNames(...*labels.Matcher) ([]string, storage.Warnings, error) { return nil, nil, errors.New("label names error") } diff --git a/storage/interface.go b/storage/interface.go index 74ddc5acad..8acdf90c8e 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -122,6 +122,10 @@ func (q *MockQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warning return nil, nil, nil } +func (q *MockQuerier) LabelValuesStream(string, ...*labels.Matcher) (LabelValuesSet, error) { + return nil, nil +} + func (q *MockQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { return nil, nil, nil } @@ -159,6 +163,12 @@ type LabelQuerier interface { // to label values of metrics matching the matchers. LabelValues(name string, matchers ...*labels.Matcher) ([]string, Warnings, error) + // LabelValuesSet returns an iterator over all potential values for a label name. + // It is not safe to use the strings beyond the lifetime of the querier. + // If matchers are specified the returned result set is reduced + // to label values of metrics matching the matchers. + LabelValuesSet(name string, matchers ...*labels.Matcher) (LabelValuesSet, error) + // LabelNames returns all the unique label names present in the block in sorted order. // If matchers are specified the returned result set is reduced // to label names of metrics matching the matchers. @@ -444,3 +454,16 @@ type ChunkIterable interface { } type Warnings []error + +// LabelValuesSet contains a set of label values. +type LabelValuesSet interface { + Next() bool + // At returns a label value. + At() string + // The error that iteration as failed with. + // When an error occurs, set cannot continue to iterate. + Err() error + // A collection of warnings for the whole set. + // Warnings could be return even iteration has not failed with error. + Warnings() Warnings +} diff --git a/storage/merge_test.go b/storage/merge_test.go index 82627d9871..fbf38744e1 100644 --- a/storage/merge_test.go +++ b/storage/merge_test.go @@ -1014,6 +1014,16 @@ func (m *mockGenericQuerier) LabelValues(name string, matchers ...*labels.Matche return m.resp, m.warnings, m.err } +func (m *mockGenericQuerier) LabelValuesSet(name string, matchers ...*labels.Matcher) (LabelValuesSet, error) { + m.mtx.Lock() + m.labelNamesRequested = append(m.labelNamesRequested, labelNameRequest{ + name: name, + matchers: matchers, + }) + m.mtx.Unlock() + return m.resp, m.warnings, m.err +} + func (m *mockGenericQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { m.mtx.Lock() m.labelNamesCalls++ diff --git a/storage/noop.go b/storage/noop.go index 83953ca43f..de13b14f7b 100644 --- a/storage/noop.go +++ b/storage/noop.go @@ -32,6 +32,10 @@ func (noopQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warnings, return nil, nil, nil } +func (noopQuerier) LabelValuesSet(string, ...*labels.Matcher) (LabelValuesSet, error) { + return nil, nil +} + func (noopQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { return nil, nil, nil } @@ -55,6 +59,13 @@ func (noopChunkQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warni return nil, nil, nil } +func (noopChunkQuerier) LabelValuesSet(string, ...*labels.Matcher) (LabelValuesSet, Warnings, error) { + return nil, nil +} +func (noopChunkQuerier) LabelValues(string, ...*labels.Matcher) ([]string, Warnings, error) { + return nil, nil, nil +} + func (noopChunkQuerier) LabelNames(...*labels.Matcher) ([]string, Warnings, error) { return nil, nil, nil } diff --git a/storage/remote/read.go b/storage/remote/read.go index af61334f48..b9cdb43473 100644 --- a/storage/remote/read.go +++ b/storage/remote/read.go @@ -217,6 +217,12 @@ func (q *querier) LabelValues(string, ...*labels.Matcher) ([]string, storage.War return nil, nil, errors.New("not implemented") } +// LabelValuesStream implements storage.Querier and is a noop. +func (q *querier) LabelValuesStream(string, ...*labels.Matcher) ([]string, error) { + // TODO: Implement: https://github.com/prometheus/prometheus/issues/3351 + return nil, errors.New("not implemented") +} + // LabelNames implements storage.Querier and is a noop. func (q *querier) LabelNames(...*labels.Matcher) ([]string, storage.Warnings, error) { // TODO: Implement: https://github.com/prometheus/prometheus/issues/3351 diff --git a/storage/secondary.go b/storage/secondary.go index d66a286172..af37018719 100644 --- a/storage/secondary.go +++ b/storage/secondary.go @@ -55,6 +55,11 @@ func (s *secondaryQuerier) LabelValues(name string, matchers ...*labels.Matcher) return vals, w, nil } +func (s *secondaryQuerier) LabelValuesSet(name string, matchers ...*labels.Matcher) (LabelValuesSet, error) { + vals, err := s.genericQuerier.LabelValuesStream(name, matchers...) + return vals, err +} + func (s *secondaryQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, Warnings, error) { names, w, err := s.genericQuerier.LabelNames(matchers...) if err != nil {