Skip to content

Commit

Permalink
pkg/query/api/v1: initial implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiusz Urbaniak <sergiusz.urbaniak@gmail.com>
  • Loading branch information
s-urbaniak committed Apr 27, 2020
1 parent 71fb105 commit 6de6e54
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
49 changes: 46 additions & 3 deletions pkg/query/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"math"
"net/http"
"strconv"
"strings"
"time"

"github.com/NYTimes/gziphandler"
Expand All @@ -38,11 +39,11 @@ import (
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/storage"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
"github.com/thanos-io/thanos/pkg/query"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/tracing"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func SetCORS(w http.ResponseWriter) {
type ApiFunc func(r *http.Request) (interface{}, []error, *ApiError)

type rulesRetriever interface {
RuleGroups() ([]*rules.Group, error)
RuleGroups() []*storepb.RuleGroup
}

// API can register a set of endpoints in a router and handle
Expand Down Expand Up @@ -638,5 +639,47 @@ func (api *API) labelNames(r *http.Request) (interface{}, []error, *ApiError) {
}

func (api *API) rules(r *http.Request) (interface{}, []error, *ApiError) {
panic("implement me")
var (
res = &storepb.RuleGroups{}
typeParam = strings.ToLower(r.URL.Query().Get("type"))
)

if typeParam != "" && typeParam != "alert" && typeParam != "record" {
return nil, nil, &ApiError{errorBadData, errors.Errorf("invalid query parameter type='%v'", typeParam)}
}

returnAlerts := typeParam == "" || typeParam == "alert"
returnRecording := typeParam == "" || typeParam == "record"

for _, grp := range api.rulesRetriever.RuleGroups() {
apiRuleGroup := &storepb.RuleGroup{
Name: grp.Name,
File: grp.File,
Interval: grp.Interval,
EvaluationDurationSeconds: grp.EvaluationDurationSeconds,
LastEvaluation: grp.LastEvaluation,
DeprecatedPartialResponseStrategy: grp.DeprecatedPartialResponseStrategy,
PartialResponseStrategy: grp.PartialResponseStrategy,
}

for _, r := range grp.Rules {
switch {
case r.GetAlert() != nil:
if !returnAlerts {
break
}
apiRuleGroup.Rules = append(apiRuleGroup.Rules, r)
case r.GetRecording() != nil:
if !returnRecording {
break
}
apiRuleGroup.Rules = append(apiRuleGroup.Rules, r)
default:
return nil, nil, &ApiError{ErrorInternal, fmt.Errorf("rule %v: unsupported", r)}
}
}
res.Groups = append(res.Groups, apiRuleGroup)
}

return res, nil, nil
}
5 changes: 2 additions & 3 deletions pkg/query/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package query

import (
"github.com/prometheus/prometheus/rules"
"github.com/thanos-io/thanos/pkg/store/storepb"
)

Expand All @@ -18,6 +17,6 @@ type rulesRetriever struct {
rulesServer storepb.RulesServer
}

func (rr *rulesRetriever) RuleGroups() ([]*rules.Group, error) {
return nil, nil
func (rr *rulesRetriever) RuleGroups() []*storepb.RuleGroup {
return nil
}

0 comments on commit 6de6e54

Please sign in to comment.