2021-03-19 09:32:13 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2021-03-19 09:32:13 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
2022-04-04 12:30:17 -05:00
|
|
|
const (
|
|
|
|
Prometheus = "prometheus"
|
|
|
|
Cortex = "cortex"
|
|
|
|
Mimir = "mimir"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PrometheusDatasourceType = "prometheus"
|
|
|
|
LokiDatasourceType = "loki"
|
|
|
|
|
|
|
|
mimirPrefix = "/config/v1/rules"
|
|
|
|
prometheusPrefix = "/rules"
|
|
|
|
lokiPrefix = "/api/prom/rules"
|
|
|
|
|
|
|
|
subtypeQuery = "subtype"
|
|
|
|
)
|
|
|
|
|
2021-03-29 00:55:09 -05:00
|
|
|
var dsTypeToRulerPrefix = map[string]string{
|
2022-04-04 12:30:17 -05:00
|
|
|
PrometheusDatasourceType: prometheusPrefix,
|
|
|
|
LokiDatasourceType: lokiPrefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
var subtypeToPrefix = map[string]string{
|
|
|
|
Prometheus: prometheusPrefix,
|
|
|
|
Cortex: prometheusPrefix,
|
|
|
|
Mimir: mimirPrefix,
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
|
|
|
|
type LotexRuler struct {
|
2021-03-24 06:43:25 -05:00
|
|
|
log log.Logger
|
|
|
|
*AlertingProxy
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2021-03-24 06:43:25 -05:00
|
|
|
func NewLotexRuler(proxy *AlertingProxy, log log.Logger) *LotexRuler {
|
|
|
|
return &LotexRuler{
|
|
|
|
log: log,
|
|
|
|
AlertingProxy: proxy,
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (r *LotexRuler) RouteDeleteNamespaceRulesConfig(ctx *models.ReqContext, namespace string) response.Response {
|
2021-11-11 00:45:56 -06:00
|
|
|
legacyRulerPrefix, err := r.validateAndGetPrefix(ctx)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
2022-06-23 15:13:39 -05:00
|
|
|
fmt.Sprintf("%s/%s", legacyRulerPrefix, namespace),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
messageExtractor,
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (r *LotexRuler) RouteDeleteRuleGroupConfig(ctx *models.ReqContext, namespace string, group string) response.Response {
|
2021-11-11 00:45:56 -06:00
|
|
|
legacyRulerPrefix, err := r.validateAndGetPrefix(ctx)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
legacyRulerPrefix,
|
2022-06-23 15:13:39 -05:00
|
|
|
namespace,
|
|
|
|
group,
|
2021-03-19 09:32:13 -05:00
|
|
|
),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
messageExtractor,
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (r *LotexRuler) RouteGetNamespaceRulesConfig(ctx *models.ReqContext, namespace string) response.Response {
|
2021-11-11 00:45:56 -06:00
|
|
|
legacyRulerPrefix, err := r.validateAndGetPrefix(ctx)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s",
|
|
|
|
legacyRulerPrefix,
|
2022-06-23 15:13:39 -05:00
|
|
|
namespace,
|
2021-03-19 09:32:13 -05:00
|
|
|
),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
yamlExtractor(apimodels.NamespaceConfigResponse{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (r *LotexRuler) RouteGetRulegGroupConfig(ctx *models.ReqContext, namespace string, group string) response.Response {
|
2021-11-11 00:45:56 -06:00
|
|
|
legacyRulerPrefix, err := r.validateAndGetPrefix(ctx)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
legacyRulerPrefix,
|
2022-06-23 15:13:39 -05:00
|
|
|
namespace,
|
|
|
|
group,
|
2021-03-19 09:32:13 -05:00
|
|
|
),
|
2021-04-07 14:36:50 -05:00
|
|
|
),
|
|
|
|
nil,
|
2021-04-12 04:04:37 -05:00
|
|
|
yamlExtractor(&apimodels.GettableRuleGroupConfig{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LotexRuler) RouteGetRulesConfig(ctx *models.ReqContext) response.Response {
|
2021-11-11 00:45:56 -06:00
|
|
|
legacyRulerPrefix, err := r.validateAndGetPrefix(ctx)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-11-11 00:45:56 -06:00
|
|
|
|
2021-03-19 09:32:13 -05:00
|
|
|
return r.withReq(
|
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
legacyRulerPrefix,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
yamlExtractor(apimodels.NamespaceConfigResponse{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:13:39 -05:00
|
|
|
func (r *LotexRuler) RoutePostNameRulesConfig(ctx *models.ReqContext, conf apimodels.PostableRuleGroupConfig, ns string) response.Response {
|
2021-11-11 00:45:56 -06:00
|
|
|
legacyRulerPrefix, err := r.validateAndGetPrefix(ctx)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
2021-03-19 09:32:13 -05:00
|
|
|
yml, err := yaml.Marshal(conf)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(500, err, "Failed marshal rule group")
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
u := withPath(*ctx.Req.URL, fmt.Sprintf("%s/%s", legacyRulerPrefix, ns))
|
2021-04-07 14:36:50 -05:00
|
|
|
return r.withReq(ctx, http.MethodPost, u, bytes.NewBuffer(yml), jsonExtractor(nil), nil)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2021-11-11 00:45:56 -06:00
|
|
|
func (r *LotexRuler) validateAndGetPrefix(ctx *models.ReqContext) (string, error) {
|
2022-05-05 06:58:32 -05:00
|
|
|
datasourceUID := web.Params(ctx.Req)[":DatasourceUID"]
|
|
|
|
if datasourceUID == "" {
|
|
|
|
return "", fmt.Errorf("datasource UID is invalid")
|
2022-01-14 10:55:57 -06:00
|
|
|
}
|
|
|
|
|
2022-05-05 06:58:32 -05:00
|
|
|
ds, err := r.DataProxy.DataSourceCache.GetDatasourceByUID(ctx.Req.Context(), datasourceUID, ctx.SignedInUser, ctx.SkipCache)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-11-11 00:45:56 -06:00
|
|
|
// Validate URL
|
|
|
|
if ds.Url == "" {
|
|
|
|
return "", fmt.Errorf("URL for this data source is empty")
|
|
|
|
}
|
|
|
|
|
2021-03-29 00:55:09 -05:00
|
|
|
prefix, ok := dsTypeToRulerPrefix[ds.Type]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unexpected datasource type. expecting loki or prometheus")
|
|
|
|
}
|
2022-04-04 12:30:17 -05:00
|
|
|
|
|
|
|
// If the datasource is Loki, there's nothing else for us to do - it doesn't have subtypes.
|
|
|
|
if ds.Type == LokiDatasourceType {
|
|
|
|
return prefix, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Prometheus datasource, can have many subtypes: Cortex, Mimir and vanilla Prometheus.
|
|
|
|
// Based on these subtypes, we want to use a different proxying path.
|
|
|
|
subtype := ctx.Query(subtypeQuery)
|
|
|
|
subTypePrefix, ok := subtypeToPrefix[subtype]
|
|
|
|
if !ok {
|
|
|
|
r.log.Debug("unable to determine prometheus datasource subtype, using default prefix", "subtype", subtype)
|
|
|
|
return prefix, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return subTypePrefix, nil
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 09:32:13 -05:00
|
|
|
func withPath(u url.URL, newPath string) *url.URL {
|
|
|
|
// TODO: handle path escaping
|
|
|
|
u.Path = newPath
|
|
|
|
return &u
|
|
|
|
}
|