Skip to content

Commit

Permalink
Merge pull request #361 from siddontang/siddontang/client-series
Browse files Browse the repository at this point in the history
v1: support series API
  • Loading branch information
beorn7 authored Dec 1, 2017
2 parents 1cdba8f + 3c0e2b3 commit 661e31b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type API interface {
QueryRange(ctx context.Context, query string, r Range) (model.Value, error)
// LabelValues performs a query for the values of the given label.
LabelValues(ctx context.Context, label string) (model.LabelValues, error)
// Series finds series by label matchers.
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error)
}

// queryResult contains result data for a query.
Expand Down Expand Up @@ -208,6 +210,34 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelVal
return labelValues, err
}

func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
u := h.client.URL(epSeries, nil)
q := u.Query()

for _, m := range matches {
q.Add("match[]", m)
}

q.Set("start", startTime.Format(time.RFC3339Nano))
q.Set("end", endTime.Format(time.RFC3339Nano))

u.RawQuery = q.Encode()

req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}

_, body, err := h.client.Do(ctx, req)
if err != nil {
return nil, err
}

var mset []model.LabelSet
err = json.Unmarshal(body, &mset)
return mset, err
}

// apiClient wraps a regular client and processes successful API responses.
// Successful also includes responses that errored at the API level.
type apiClient struct {
Expand Down
43 changes: 43 additions & 0 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func TestAPIs(t *testing.T) {
}
}

doSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, error) {
return func() (interface{}, error) {
return queryAPI.Series(context.Background(), []string{matcher}, startTime, endTime)
}
}

queryTests := []apiTest{
{
do: doQuery("2", testTime),
Expand Down Expand Up @@ -181,6 +187,43 @@ func TestAPIs(t *testing.T) {
reqPath: "/api/v1/label/mylabel/values",
err: fmt.Errorf("some error"),
},

{
do: doSeries("up", testTime.Add(-time.Minute), testTime),
inRes: []map[string]string{
{
"__name__": "up",
"job": "prometheus",
"instance": "localhost:9090"},
},
reqMethod: "GET",
reqPath: "/api/v1/series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
res: []model.LabelSet{
model.LabelSet{
"__name__": "up",
"job": "prometheus",
"instance": "localhost:9090",
},
},
},

{
do: doSeries("up", testTime.Add(-time.Minute), testTime),
inErr: fmt.Errorf("some error"),
reqMethod: "GET",
reqPath: "/api/v1/series",
reqParam: url.Values{
"match": []string{"up"},
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
"end": []string{testTime.Format(time.RFC3339Nano)},
},
err: fmt.Errorf("some error"),
},
}

var tests []apiTest
Expand Down

0 comments on commit 661e31b

Please sign in to comment.