Skip to content

Commit

Permalink
Merge pull request #861 from yeya24/add-exemplars
Browse files Browse the repository at this point in the history
Add exemplars API support
  • Loading branch information
beorn7 committed Apr 27, 2021
2 parents 0400fc4 + 7a147c1 commit 5486764
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
37 changes: 37 additions & 0 deletions api/prometheus/v1/api.go
Expand Up @@ -123,6 +123,7 @@ const (
epAlertManagers = apiPrefix + "/alertmanagers"
epQuery = apiPrefix + "/query"
epQueryRange = apiPrefix + "/query_range"
epQueryExemplars = apiPrefix + "/query_exemplars"
epLabels = apiPrefix + "/labels"
epLabelValues = apiPrefix + "/label/:name/values"
epSeries = apiPrefix + "/series"
Expand Down Expand Up @@ -239,6 +240,8 @@ type API interface {
Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error)
// QueryRange performs a query for the given range.
QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error)
// QueryExemplars performs a query for exemplars by the given query and time range.
QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error)
// Buildinfo returns various build information properties about the Prometheus server
Buildinfo(ctx context.Context) (BuildinfoResult, error)
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
Expand Down Expand Up @@ -588,6 +591,18 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error {
return err
}

// Exemplar is additional information associated with a time series.
type Exemplar struct {
Labels model.LabelSet `json:"labels"`
Value model.SampleValue `json:"value"`
Timestamp model.Time `json:"timestamp"`
}

type ExemplarQueryResult struct {
SeriesLabels model.LabelSet `json:"seriesLabels"`
Exemplars []Exemplar `json:"exemplars"`
}

// NewAPI returns a new API for the client.
//
// It is safe to use the returned API from multiple goroutines.
Expand Down Expand Up @@ -967,7 +982,29 @@ func (h *httpAPI) TSDB(ctx context.Context) (TSDBResult, error) {

var res TSDBResult
return res, json.Unmarshal(body, &res)
}

func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error) {
u := h.client.URL(epQueryExemplars, nil)
q := u.Query()

q.Set("query", query)
q.Set("start", formatTime(startTime))
q.Set("end", formatTime(endTime))
u.RawQuery = q.Encode()

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

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

var res []ExemplarQueryResult
return res, json.Unmarshal(body, &res)
}

// Warnings is an array of non critical errors
Expand Down
67 changes: 67 additions & 0 deletions api/prometheus/v1/api_test.go
Expand Up @@ -230,6 +230,13 @@ func TestAPIs(t *testing.T) {
}
}

doQueryExemplars := func(query string, startTime time.Time, endTime time.Time) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
v, err := promAPI.QueryExemplars(context.Background(), query, startTime, endTime)
return v, nil, err
}
}

queryTests := []apiTest{
{
do: doQuery("2", testTime),
Expand Down Expand Up @@ -1190,6 +1197,66 @@ func TestAPIs(t *testing.T) {
},
},
},

{
do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime),
reqMethod: "GET",
reqPath: "/api/v1/query_exemplars",
inErr: fmt.Errorf("some error"),
err: fmt.Errorf("some error"),
},

{
do: doQueryExemplars("tns_request_duration_seconds_bucket", testTime.Add(-1*time.Minute), testTime),
reqMethod: "GET",
reqPath: "/api/v1/query_exemplars",
inRes: []interface{}{
map[string]interface{}{
"seriesLabels": map[string]interface{}{
"__name__": "tns_request_duration_seconds_bucket",
"instance": "app:80",
"job": "tns/app",
},
"exemplars": []interface{}{
map[string]interface{}{
"labels": map[string]interface{}{
"traceID": "19fd8c8a33975a23",
},
"value": "0.003863295",
"timestamp": model.TimeFromUnixNano(testTime.UnixNano()),
},
map[string]interface{}{
"labels": map[string]interface{}{
"traceID": "67f743f07cc786b0",
},
"value": "0.001535405",
"timestamp": model.TimeFromUnixNano(testTime.UnixNano()),
},
},
},
},
res: []ExemplarQueryResult{
{
SeriesLabels: model.LabelSet{
"__name__": "tns_request_duration_seconds_bucket",
"instance": "app:80",
"job": "tns/app",
},
Exemplars: []Exemplar{
{
Labels: model.LabelSet{"traceID": "19fd8c8a33975a23"},
Value: 0.003863295,
Timestamp: model.TimeFromUnixNano(testTime.UnixNano()),
},
{
Labels: model.LabelSet{"traceID": "67f743f07cc786b0"},
Value: 0.001535405,
Timestamp: model.TimeFromUnixNano(testTime.UnixNano()),
},
},
},
},
},
}

var tests []apiTest
Expand Down

0 comments on commit 5486764

Please sign in to comment.