Skip to content

Commit

Permalink
Add DebugAPI client
Browse files Browse the repository at this point in the history
DebugAPI implements the API interface and simply adds debug/trace logging to all calls made through it. This way we can get nice debug messages such as:

```
DEBU[2019-06-07T08:15:27-07:00] http://localhost:9090/api/v1/read             api=GetValue end="2019-06-07 08:11:33.855 -0700 PDT" matchers="[__name__=\"prometheus_api_remote_read_queries\"]" start="2019-06-07 07:06:33.855 -0700 PDT"
```

To avoid the performance overhead this layer is only added when the log level is >= debug.

Fixes #163
  • Loading branch information
jacksontj committed Jun 7, 2019
1 parent 36c1338 commit 6c1cb04
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 6 deletions.
126 changes: 126 additions & 0 deletions promclient/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package promclient

import (
"context"
"fmt"
"time"

v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/sirupsen/logrus"
)

// DebugAPI simply logs debug lines for the given API with the given prefix
type DebugAPI struct {
API
PrefixMessage string
}

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

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

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

return v, err
}

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

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

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

return v, err
}

// QueryRange performs a query for the given range.
func (d *DebugAPI) QueryRange(ctx context.Context, query string, r v1.Range) (model.Value, 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)

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

return v, 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) {
logrus.WithFields(logrus.Fields{
"api": "Series",
"matches": matches,
"startTime": startTime,
"endTime": endTime,
}).Debug(d.PrefixMessage)

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

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

return v, 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) {
logrus.WithFields(logrus.Fields{
"api": "GetValue",
"start": start,
"end": end,
"matchers": matchers,
}).Debug(d.PrefixMessage)

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

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

return v, err
}
21 changes: 15 additions & 6 deletions servergroup/servergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/relabel"
"github.com/prometheus/prometheus/storage/remote"
"github.com/sirupsen/logrus"

"github.com/jacksontj/promxy/promclient"

Expand Down Expand Up @@ -123,9 +124,9 @@ func (s *ServerGroup) Sync() {
panic(err) // TODO: shouldn't be possible? If this happens I guess we log and skip?
}

promAPIClient := &promclient.PromAPIV1{v1.NewAPI(client)}

var apiClient promclient.API
apiClient = &promclient.PromAPIV1{v1.NewAPI(client)}

if s.Cfg.RemoteRead {
u.Path = path.Join(u.Path, "api/v1/read")
cfg := &remote.ClientConfig{
Expand All @@ -138,9 +139,7 @@ func (s *ServerGroup) Sync() {
panic(err)
}

apiClient = &promclient.PromAPIRemoteRead{promAPIClient, remoteStorageClient}
} else {
apiClient = promAPIClient
apiClient = &promclient.PromAPIRemoteRead{apiClient, remoteStorageClient}
}

// Optionally add time range layers
Expand Down Expand Up @@ -169,7 +168,17 @@ func (s *ServerGroup) Sync() {
}
}

apiClients = append(apiClients, &promclient.AddLabelClient{apiClient, target.Merge(s.Cfg.Labels)})
// Add labels
apiClient = &promclient.AddLabelClient{apiClient, target.Merge(s.Cfg.Labels)}

// If debug logging is enabled, wrap the client with a debugAPI client
// Since these are called in the reverse order of what we add, we want
// to make sure that this is the last wrap of the client
if logrus.GetLevel() >= logrus.DebugLevel {
apiClient = &promclient.DebugAPI{apiClient, u.String()}
}

apiClients = append(apiClients, apiClient)
}
}
}
Expand Down

0 comments on commit 6c1cb04

Please sign in to comment.