Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timefilter for LabelName and LabelValues #581

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/promclient/timefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@ type AbsoluteTimeFilter struct {
Truncate bool
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (tf *AbsoluteTimeFilter) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
if (!tf.Start.IsZero() && endTime.Before(tf.Start)) || (!tf.End.IsZero() && startTime.After(tf.End)) {
return nil, nil, nil
}

if tf.Truncate {
if startTime.Before(tf.Start) {
startTime = tf.Start
}
if endTime.After(tf.End) {
endTime = tf.End
}
}

return tf.API.LabelNames(ctx, matchers, startTime, endTime)
}

// LabelValues performs a query for the values of the given label.
func (tf *AbsoluteTimeFilter) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
if (!tf.Start.IsZero() && endTime.Before(tf.Start)) || (!tf.End.IsZero() && startTime.After(tf.End)) {
return nil, nil, nil
}

if tf.Truncate {
if startTime.Before(tf.Start) {
startTime = tf.Start
}
if endTime.After(tf.End) {
endTime = tf.End
}
}

return tf.API.LabelValues(ctx, label, matchers, startTime, endTime)
}

// Query performs a query for the given time.
func (tf *AbsoluteTimeFilter) Query(ctx context.Context, query string, ts time.Time) (model.Value, v1.Warnings, error) {
if (!tf.Start.IsZero() && ts.Before(tf.Start)) || (!tf.End.IsZero() && ts.After(tf.End)) {
Expand Down Expand Up @@ -100,6 +136,44 @@ func (tf *RelativeTimeFilter) window() (time.Time, time.Time) {
return start, end
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (tf *RelativeTimeFilter) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
tfStart, tfEnd := tf.window()
if (!tfStart.IsZero() && endTime.Before(tfStart)) || (!tfEnd.IsZero() && startTime.After(tfEnd)) {
return nil, nil, nil
}

if tf.Truncate {
if startTime.Before(tfStart) {
startTime = tfStart
}
if endTime.After(tfEnd) {
endTime = tfEnd
}
}

return tf.API.LabelNames(ctx, matchers, startTime, endTime)
}

// LabelValues performs a query for the values of the given label.
func (tf *RelativeTimeFilter) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
tfStart, tfEnd := tf.window()
if (!tfStart.IsZero() && endTime.Before(tfStart)) || (!tfEnd.IsZero() && startTime.After(tfEnd)) {
return nil, nil, nil
}

if tf.Truncate {
if startTime.Before(tfStart) {
startTime = tfStart
}
if endTime.After(tfEnd) {
endTime = tfEnd
}
}

return tf.API.LabelValues(ctx, label, matchers, startTime, endTime)
}

// Query performs a query for the given time.
func (tf *RelativeTimeFilter) Query(ctx context.Context, query string, ts time.Time) (model.Value, v1.Warnings, error) {
tfStart, tfEnd := tf.window()
Expand Down
24 changes: 24 additions & 0 deletions pkg/promclient/timefilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func timefilterTest(t *testing.T, api API, testCase timeFilterTestCase) {

for i, r := range testCase.validRanges {
t.Run(fmt.Sprintf("validRange_%d", i), func(t *testing.T) {
t.Run("label_names", func(t *testing.T) {
if _, _, err := api.LabelNames(context.TODO(), []string{"a"}, r.Start, r.End); err == nil {
t.Fatalf("Missing call to API")
}
})

t.Run("label_values", func(t *testing.T) {
if _, _, err := api.LabelValues(context.TODO(), "__name__", []string{"a"}, r.Start, r.End); err == nil {
t.Fatalf("Missing call to API")
}
})

t.Run("query_range", func(t *testing.T) {
if _, _, err := api.QueryRange(context.TODO(), "", v1.Range{Start: r.Start, End: r.End}); err == nil {
t.Fatalf("Missing call to API")
Expand All @@ -58,6 +70,18 @@ func timefilterTest(t *testing.T, api API, testCase timeFilterTestCase) {
}
for i, r := range testCase.invalidRanges {
t.Run(fmt.Sprintf("invalidRange_%d", i), func(t *testing.T) {
t.Run("label_names", func(t *testing.T) {
if _, _, err := api.LabelNames(context.TODO(), []string{"a"}, r.Start, r.End); err != nil {
t.Fatalf("Unexpected call to API")
}
})

t.Run("label_values", func(t *testing.T) {
if _, _, err := api.LabelValues(context.TODO(), "__name__", []string{"a"}, r.Start, r.End); err != nil {
t.Fatalf("Unexpected call to API")
}
})

t.Run("query_range", func(t *testing.T) {
if _, _, err := api.QueryRange(context.TODO(), "", v1.Range{Start: r.Start, End: r.End}); err != nil {
t.Fatalf("Unexpected call to API")
Expand Down