github.com/thanos-io/thanos@v0.32.5/pkg/api/rule/v1.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package v1
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/go-kit/log"
    10  	"github.com/opentracing/opentracing-go"
    11  	"github.com/prometheus/client_golang/prometheus"
    12  	"github.com/prometheus/common/route"
    13  
    14  	"github.com/thanos-io/thanos/pkg/api"
    15  	qapi "github.com/thanos-io/thanos/pkg/api/query"
    16  	extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
    17  	"github.com/thanos-io/thanos/pkg/logging"
    18  	"github.com/thanos-io/thanos/pkg/rules"
    19  	"github.com/thanos-io/thanos/pkg/rules/rulespb"
    20  )
    21  
    22  // RuleAPI is a very simple API used by Thanos Ruler.
    23  type RuleAPI struct {
    24  	baseAPI     *api.BaseAPI
    25  	logger      log.Logger
    26  	ruleGroups  rules.UnaryClient
    27  	alerts      alertsRetriever
    28  	reg         prometheus.Registerer
    29  	disableCORS bool
    30  }
    31  
    32  type alertsRetriever interface {
    33  	Active() []*rulespb.AlertInstance
    34  }
    35  
    36  // NewRuleAPI creates an Thanos ruler API.
    37  func NewRuleAPI(
    38  	logger log.Logger,
    39  	reg prometheus.Registerer,
    40  	ruleGroups rules.UnaryClient,
    41  	activeAlerts alertsRetriever,
    42  	disableCORS bool,
    43  	flagsMap map[string]string,
    44  ) *RuleAPI {
    45  	return &RuleAPI{
    46  		baseAPI:     api.NewBaseAPI(logger, disableCORS, flagsMap),
    47  		logger:      logger,
    48  		ruleGroups:  ruleGroups,
    49  		alerts:      activeAlerts,
    50  		reg:         reg,
    51  		disableCORS: disableCORS,
    52  	}
    53  }
    54  
    55  func (rapi *RuleAPI) Register(r *route.Router, tracer opentracing.Tracer, logger log.Logger, ins extpromhttp.InstrumentationMiddleware, logMiddleware *logging.HTTPServerMiddleware) {
    56  	rapi.baseAPI.Register(r, tracer, logger, ins, logMiddleware)
    57  
    58  	instr := api.GetInstr(tracer, logger, ins, logMiddleware, rapi.disableCORS)
    59  
    60  	r.Get("/alerts", instr("alerts", func(r *http.Request) (interface{}, []error, *api.ApiError, func()) {
    61  		return struct{ Alerts []*rulespb.AlertInstance }{Alerts: rapi.alerts.Active()}, nil, nil, func() {}
    62  	}))
    63  	r.Get("/rules", instr("rules", qapi.NewRulesHandler(rapi.ruleGroups, false)))
    64  }