Skip to content

Commit

Permalink
Propoagate start/end times in Label queries
Browse files Browse the repository at this point in the history
Fixes #472
  • Loading branch information
jacksontj committed Nov 1, 2021
1 parent 7a0fef6 commit c33ae69
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 46 deletions.
14 changes: 4 additions & 10 deletions pkg/promclient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package promclient
import (
"context"
"fmt"
"math"
"time"

v1 "github.com/prometheus/client_golang/api/prometheus/v1"
Expand All @@ -15,25 +14,20 @@ import (
"github.com/jacksontj/promxy/pkg/promhttputil"
)

var (
minTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()
)

// PromAPIV1 implements our internal API interface using *only* the v1 HTTP API
// Simply wraps the prom API to fullfil our internal API interface
type PromAPIV1 struct {
v1.API
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (p *PromAPIV1) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
return p.API.LabelNames(ctx, matchers, minTime, maxTime)
func (p *PromAPIV1) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
return p.API.LabelNames(ctx, matchers, startTime, endTime)
}

// LabelValues performs a query for the values of the given label.
func (p *PromAPIV1) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
return p.API.LabelValues(ctx, label, matchers, minTime, maxTime)
func (p *PromAPIV1) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
return p.API.LabelValues(ctx, label, matchers, startTime, endTime)
}

// GetValue loads the raw data for a given set of matchers in the time range
Expand Down
8 changes: 4 additions & 4 deletions pkg/promclient/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ type DebugAPI struct {
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (d *DebugAPI) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
func (d *DebugAPI) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
fields := logrus.Fields{
"api": "LabelNames",
}
logrus.WithFields(fields).Debug(d.PrefixMessage)

s := time.Now()
v, w, err := d.A.LabelNames(ctx, matchers)
v, w, err := d.A.LabelNames(ctx, matchers, startTime, endTime)
fields["took"] = time.Since(s)

if logrus.GetLevel() > logrus.DebugLevel {
Expand All @@ -40,15 +40,15 @@ func (d *DebugAPI) LabelNames(ctx context.Context, matchers []string) ([]string,
}

// LabelValues performs a query for the values of the given label.
func (d *DebugAPI) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
func (d *DebugAPI) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
fields := logrus.Fields{
"api": "LabelValues",
"label": label,
}
logrus.WithFields(fields).Debug(d.PrefixMessage)

s := time.Now()
v, w, err := d.A.LabelValues(ctx, label, matchers)
v, w, err := d.A.LabelValues(ctx, label, matchers, startTime, endTime)
fields["took"] = time.Since(s)

if logrus.GetLevel() > logrus.DebugLevel {
Expand Down
8 changes: 4 additions & 4 deletions pkg/promclient/ignore_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ type IgnoreErrorAPI struct {
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (n *IgnoreErrorAPI) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
v, w, _ := n.A.LabelNames(ctx, matchers)
func (n *IgnoreErrorAPI) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
v, w, _ := n.A.LabelNames(ctx, matchers, startTime, endTime)
return v, w, nil
}

// LabelValues performs a query for the values of the given label.
func (n *IgnoreErrorAPI) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
v, w, _ := n.A.LabelValues(ctx, label, matchers)
func (n *IgnoreErrorAPI) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
v, w, _ := n.A.LabelValues(ctx, label, matchers, startTime, endTime)

return v, w, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/promclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
// API Subset of the interface defined in the prometheus client
type API interface {
// LabelNames returns all the unique label names present in the block in sorted order.
LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error)
LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error)
// LabelValues performs a query for the values of the given label.
LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error)
LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error)
// Query performs a query for the given time.
Query(ctx context.Context, query string, ts time.Time) (model.Value, v1.Warnings, error)
// QueryRange performs a query for the given range.
Expand Down
8 changes: 4 additions & 4 deletions pkg/promclient/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (c *AddLabelClient) Key() model.LabelSet {
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (c *AddLabelClient) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
l, w, err := c.API.LabelNames(ctx, matchers)
func (c *AddLabelClient) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
l, w, err := c.API.LabelNames(ctx, matchers, startTime, endTime)
if err != nil {
return nil, nil, err
}
Expand All @@ -80,8 +80,8 @@ func (c *AddLabelClient) LabelNames(ctx context.Context, matchers []string) ([]s
}

// LabelValues performs a query for the values of the given label.
func (c *AddLabelClient) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
val, w, err := c.API.LabelValues(ctx, label, matchers)
func (c *AddLabelClient) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
val, w, err := c.API.LabelValues(ctx, label, matchers, startTime, endTime)
if err != nil {
return nil, w, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/promclient/multi_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (m *MultiAPI) recordMetric(i int, api, status string, took float64) {
}

// LabelValues performs a query for the values of the given label.
func (m *MultiAPI) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
func (m *MultiAPI) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
childContext, childContextCancel := context.WithCancel(ctx)
defer childContextCancel()

Expand All @@ -126,7 +126,7 @@ func (m *MultiAPI) LabelValues(ctx context.Context, label string, matchers []str
outstandingRequests[m.apiFingerprints[i]]++
go func(i int, retChan chan chanResult, api API, label string) {
start := time.Now()
result, w, err := api.LabelValues(childContext, label, matchers)
result, w, err := api.LabelValues(childContext, label, matchers, startTime, endTime)
took := time.Since(start)
if err != nil {
m.recordMetric(i, "label_values", "error", took.Seconds())
Expand Down Expand Up @@ -185,7 +185,7 @@ func (m *MultiAPI) LabelValues(ctx context.Context, label string, matchers []str
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (m *MultiAPI) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
func (m *MultiAPI) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
childContext, childContextCancel := context.WithCancel(ctx)
defer childContextCancel()

Expand All @@ -204,7 +204,7 @@ func (m *MultiAPI) LabelNames(ctx context.Context, matchers []string) ([]string,
outstandingRequests[m.apiFingerprints[i]]++
go func(i int, retChan chan chanResult, api API) {
start := time.Now()
result, w, err := api.LabelNames(childContext, matchers)
result, w, err := api.LabelNames(childContext, matchers, startTime, endTime)
took := time.Since(start)
if err != nil {
m.recordMetric(i, "label_names", "error", took.Seconds())
Expand Down
16 changes: 8 additions & 8 deletions pkg/promclient/multi_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type stubAPI struct {
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (s *stubAPI) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
func (s *stubAPI) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
return s.labelNames(), nil, nil
}

// LabelValues performs a query for the values of the given label.
func (s *stubAPI) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
func (s *stubAPI) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
return s.labelValues(), nil, nil
}

Expand Down Expand Up @@ -63,19 +63,19 @@ func (s *errorAPI) Key() model.LabelSet {
return nil
}

func (s *errorAPI) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
func (s *errorAPI) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
if s.err != nil {
return nil, nil, s.err
}
return s.LabelNames(ctx, matchers)
return s.LabelNames(ctx, matchers, startTime, endTime)
}

// LabelValues performs a query for the values of the given label.
func (s *errorAPI) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
func (s *errorAPI) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
if s.err != nil {
return nil, nil, s.err
}
return s.LabelValues(ctx, label, matchers)
return s.LabelValues(ctx, label, matchers, startTime, endTime)
}

// Query performs a query for the given time.
Expand Down Expand Up @@ -350,7 +350,7 @@ func TestMultiAPIMerging(t *testing.T) {
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Run("LabelNames", func(t *testing.T) {
v, _, err := test.a.LabelNames(context.TODO(), nil)
v, _, err := test.a.LabelNames(context.TODO(), nil, time.Time{}, time.Time{})
if err != nil != test.err {
if test.err {
t.Fatalf("missing expected err")
Expand All @@ -376,7 +376,7 @@ func TestMultiAPIMerging(t *testing.T) {
})

t.Run("LabelValues", func(t *testing.T) {
v, _, err := test.a.LabelValues(context.TODO(), "a", nil)
v, _, err := test.a.LabelValues(context.TODO(), "a", nil, time.Time{}, time.Time{})
if err != nil != test.err {
if test.err {
t.Fatalf("missing expected err")
Expand Down
8 changes: 4 additions & 4 deletions pkg/promclient/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ import (
type recoverAPI struct{ A API }

// LabelNames returns all the unique label names present in the block in sorted order.
func (api *recoverAPI) LabelNames(ctx context.Context, matchers []string) (v []string, w v1.Warnings, err error) {
func (api *recoverAPI) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) (v []string, w v1.Warnings, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
return api.A.LabelNames(ctx, matchers)
return api.A.LabelNames(ctx, matchers, startTime, endTime)
}

// LabelValues performs a query for the values of the given label.
func (api *recoverAPI) LabelValues(ctx context.Context, label string, matchers []string) (v model.LabelValues, w v1.Warnings, err error) {
func (api *recoverAPI) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (v model.LabelValues, w v1.Warnings, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
return api.A.LabelValues(ctx, label, matchers)
return api.A.LabelValues(ctx, label, matchers, startTime, endTime)
}

// Query performs a query for the given time.
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxyquerier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (h *ProxyQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]
matchersStrings = []string{s}
}

result, w, err := h.Client.LabelValues(h.Ctx, name, matchersStrings)
result, w, err := h.Client.LabelValues(h.Ctx, name, matchersStrings, h.Start, h.End)
warnings := promhttputil.WarningsConvert(w)
if err != nil {
return nil, warnings, errors.Cause(err)
Expand Down Expand Up @@ -137,7 +137,7 @@ func (h *ProxyQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storag
matchersStrings = []string{s}
}

v, w, err := h.Client.LabelNames(h.Ctx, matchersStrings)
v, w, err := h.Client.LabelNames(h.Ctx, matchersStrings, h.Start, h.End)
return v, promhttputil.WarningsConvert(w), err
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/servergroup/servergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ func (s *ServerGroup) QueryRange(ctx context.Context, query string, r v1.Range)
}

// LabelValues performs a query for the values of the given label.
func (s *ServerGroup) LabelValues(ctx context.Context, label string, matchers []string) (model.LabelValues, v1.Warnings, error) {
return s.State().apiClient.LabelValues(ctx, label, matchers)
func (s *ServerGroup) LabelValues(ctx context.Context, label string, matchers []string, startTime time.Time, endTime time.Time) (model.LabelValues, v1.Warnings, error) {
return s.State().apiClient.LabelValues(ctx, label, matchers, startTime, endTime)
}

// LabelNames returns all the unique label names present in the block in sorted order.
func (s *ServerGroup) LabelNames(ctx context.Context, matchers []string) ([]string, v1.Warnings, error) {
return s.State().apiClient.LabelNames(ctx, matchers)
func (s *ServerGroup) LabelNames(ctx context.Context, matchers []string, startTime time.Time, endTime time.Time) ([]string, v1.Warnings, error) {
return s.State().apiClient.LabelNames(ctx, matchers, startTime, endTime)
}

// Series finds series by label matchers.
Expand Down

0 comments on commit c33ae69

Please sign in to comment.