Skip to content

Commit

Permalink
Implement warnings throughout promxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksontj committed Jun 20, 2019
1 parent 1e9b896 commit 6bf0493
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 283 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 8 additions & 38 deletions pkg/promclient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
Expand All @@ -20,51 +21,20 @@ 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) ([]string, error) {
v, _, err := p.API.LabelNames(ctx)
return v, err
}

// LabelValues performs a query for the values of the given label.
func (p *PromAPIV1) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
v, _, err := p.API.LabelValues(ctx, label)
return v, err
}

// Query performs a query for the given time.
func (p *PromAPIV1) Query(ctx context.Context, query string, ts time.Time) (model.Value, error) {
v, _, err := p.API.Query(ctx, query, ts)
return v, err
}

// QueryRange performs a query for the given range.
func (p *PromAPIV1) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, error) {
v, _, err := p.API.QueryRange(ctx, query, r)
return v, err
}

// Series finds series by label matchers.
func (p *PromAPIV1) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
v, _, err := p.API.Series(ctx, matches, startTime, endTime)
return v, err
}

// GetValue loads the raw data for a given set of matchers in the time range
func (p *PromAPIV1) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, error) {
func (p *PromAPIV1) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, api.Warnings, error) {
// http://localhost:8080/api/v1/query?query=scrape_duration_seconds%7Bjob%3D%22prometheus%22%7D&time=1507412244.663&_=1507412096887
pql, err := promhttputil.MatcherToString(matchers)
if err != nil {
return nil, err
return nil, nil, err
}

// We want to grab only the raw datapoints, so we do that through the query interface
// passing in a duration that is at least as long as ours (the added second is to deal
// with any rounding error etc since the duration is a floating point and we are casting
// to an int64
query := pql + fmt.Sprintf("[%ds]", int64(end.Sub(start).Seconds())+1)
v, _, err := p.API.Query(ctx, query, end)
return v, err
return p.API.Query(ctx, query, end)
}

// PromAPIRemoteRead implements our internal API interface using a combination of
Expand All @@ -75,14 +45,14 @@ type PromAPIRemoteRead struct {
}

// GetValue loads the raw data for a given set of matchers in the time range
func (p *PromAPIRemoteRead) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, error) {
func (p *PromAPIRemoteRead) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, api.Warnings, error) {
query, err := remote.ToQuery(int64(timestamp.FromTime(start)), int64(timestamp.FromTime(end)), matchers, nil)
if err != nil {
return nil, err
return nil, nil, err
}
result, err := p.Client.Read(ctx, query)
if err != nil {
return nil, err
return nil, nil, err
}

// convert result (timeseries) to SampleStream
Expand All @@ -107,5 +77,5 @@ func (p *PromAPIRemoteRead) GetValue(ctx context.Context, start, end time.Time,
}
}

return matrix, nil
return matrix, nil, nil
}
64 changes: 35 additions & 29 deletions pkg/promclient/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
Expand All @@ -18,109 +19,114 @@ type DebugAPI struct {
}

// LabelValues performs a query for the values of the given label.
func (d *DebugAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
func (d *DebugAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error) {
logrus.WithFields(logrus.Fields{
"api": "LabelValues",
"label": label,
}).Debug(d.PrefixMessage)

v, err := d.API.LabelValues(ctx, label)
v, w, err := d.API.LabelValues(ctx, label)

logrus.WithFields(logrus.Fields{
"api": "LabelValues",
"label": label,
"value": v,
"error": err,
"api": "LabelValues",
"label": label,
"value": v,
"warnings": w,
"error": err,
}).Trace(d.PrefixMessage)

return v, err
return v, w, err
}

// Query performs a query for the given time.
func (d *DebugAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, error) {
func (d *DebugAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error) {
logrus.WithFields(logrus.Fields{
"api": "Query",
"query": query,
"ts": ts,
}).Debug(d.PrefixMessage)

v, err := d.API.Query(ctx, query, ts)
v, w, err := d.API.Query(ctx, query, ts)

logrus.WithFields(logrus.Fields{
"api": "Query",
"query": query,
"ts": ts,
"value": v,
"error": err,
"api": "Query",
"query": query,
"ts": ts,
"value": v,
"warnings": w,
"error": err,
}).Trace(d.PrefixMessage)

return v, err
return v, w, err
}

// QueryRange performs a query for the given range.
func (d *DebugAPI) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, error) {
func (d *DebugAPI) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, api.Warnings, error) {
fmt.Println("what")
logrus.WithFields(logrus.Fields{
"api": "QueryRange",
"query": query,
"r": r,
}).Debug(d.PrefixMessage)

v, err := d.API.QueryRange(ctx, query, r)
v, w, err := d.API.QueryRange(ctx, query, r)

logrus.WithFields(logrus.Fields{
"api": "QueryRange",
"query": query,
"r": r,
"value": v,
"error": err,
"api": "QueryRange",
"query": query,
"r": r,
"value": v,
"warnings": w,
"error": err,
}).Trace(d.PrefixMessage)

return v, err
return v, w, err
}

// Series finds series by label matchers.
func (d *DebugAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
func (d *DebugAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error) {
logrus.WithFields(logrus.Fields{
"api": "Series",
"matches": matches,
"startTime": startTime,
"endTime": endTime,
}).Debug(d.PrefixMessage)

v, err := d.API.Series(ctx, matches, startTime, endTime)
v, w, err := d.API.Series(ctx, matches, startTime, endTime)

logrus.WithFields(logrus.Fields{
"api": "Series",
"matches": matches,
"startTime": startTime,
"endTime": endTime,
"value": v,
"warnings": w,
"error": err,
}).Trace(d.PrefixMessage)

return v, err
return v, w, err
}

// GetValue loads the raw data for a given set of matchers in the time range
func (d *DebugAPI) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, error) {
func (d *DebugAPI) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, api.Warnings, error) {
logrus.WithFields(logrus.Fields{
"api": "GetValue",
"start": start,
"end": end,
"matchers": matchers,
}).Debug(d.PrefixMessage)

v, err := d.API.GetValue(ctx, start, end, matchers)
v, w, err := d.API.GetValue(ctx, start, end, matchers)

logrus.WithFields(logrus.Fields{
"api": "GetValue",
"start": start,
"end": end,
"matchers": matchers,
"value": v,
"warnings": w,
"error": err,
}).Trace(d.PrefixMessage)

return v, err
return v, w, err
}
31 changes: 16 additions & 15 deletions pkg/promclient/ignore_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
Expand All @@ -17,38 +18,38 @@ type IgnoreErrorAPI struct {
}

// LabelValues performs a query for the values of the given label.
func (n *IgnoreErrorAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
v, _ := n.API.LabelValues(ctx, label)
func (n *IgnoreErrorAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error) {
v, w, _ := n.API.LabelValues(ctx, label)

return v, nil
return v, w, nil
}

// Query performs a query for the given time.
func (n *IgnoreErrorAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, error) {
v, _ := n.API.Query(ctx, query, ts)
func (n *IgnoreErrorAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error) {
v, w, _ := n.API.Query(ctx, query, ts)

return v, nil
return v, w, nil
}

// QueryRange performs a query for the given range.
func (n *IgnoreErrorAPI) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, error) {
v, _ := n.API.QueryRange(ctx, query, r)
func (n *IgnoreErrorAPI) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, api.Warnings, error) {
v, w, _ := n.API.QueryRange(ctx, query, r)

return v, nil
return v, w, nil
}

// Series finds series by label matchers.
func (n *IgnoreErrorAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
v, _ := n.API.Series(ctx, matches, startTime, endTime)
func (n *IgnoreErrorAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error) {
v, w, _ := n.API.Series(ctx, matches, startTime, endTime)

return v, nil
return v, w, nil
}

// GetValue loads the raw data for a given set of matchers in the time range
func (n *IgnoreErrorAPI) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, error) {
v, _ := n.API.GetValue(ctx, start, end, matchers)
func (n *IgnoreErrorAPI) GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, api.Warnings, error) {
v, w, _ := n.API.GetValue(ctx, start, end, matchers)

return v, nil
return v, w, nil
}

// Key returns a labelset used to determine other api clients that are the "same"
Expand Down
13 changes: 7 additions & 6 deletions pkg/promclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
Expand All @@ -12,17 +13,17 @@ 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) ([]string, error)
LabelNames(ctx context.Context) ([]string, api.Warnings, error)
// LabelValues performs a query for the values of the given label.
LabelValues(ctx context.Context, label string) (model.LabelValues, error)
LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error)
// Query performs a query for the given time.
Query(ctx context.Context, query string, ts time.Time) (model.Value, error)
Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error)
// QueryRange performs a query for the given range.
QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, error)
QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, api.Warnings, error)
// Series finds series by label matchers.
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error)
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error)
// GetValue loads the raw data for a given set of matchers in the time range
GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, error)
GetValue(ctx context.Context, start, end time.Time, matchers []*labels.Matcher) (model.Value, api.Warnings, error)
}

// APILabels includes a Key() mechanism to differentiate which APIs are "the same"
Expand Down
Loading

0 comments on commit 6bf0493

Please sign in to comment.