2021-03-19 09:32:13 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2024-02-16 02:43:47 -06:00
|
|
|
"io"
|
2021-03-19 09:32:13 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-01-30 02:55:35 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2021-03-19 09:32:13 -05:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2024-02-16 02:43:47 -06:00
|
|
|
// The requester is primarily used for testing purposes, allowing us to inject a different implementation of withReq.
|
|
|
|
type requester interface {
|
|
|
|
withReq(ctx *contextmodel.ReqContext, method string, u *url.URL, body io.Reader, extractor func(*response.NormalResponse) (any, error), headers map[string]string) response.Response
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:32:13 -05:00
|
|
|
type LotexRuler struct {
|
2021-03-24 06:43:25 -05:00
|
|
|
log log.Logger
|
|
|
|
*AlertingProxy
|
2024-02-16 02:43:47 -06:00
|
|
|
requester requester
|
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,
|
2024-02-16 02:43:47 -06:00
|
|
|
requester: proxy,
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) RouteDeleteNamespaceRulesConfig(ctx *contextmodel.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
|
|
|
}
|
2024-02-16 02:43:47 -06:00
|
|
|
return r.requester.withReq(
|
2021-03-19 09:32:13 -05:00
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
2024-02-16 02:43:47 -06:00
|
|
|
fmt.Sprintf("%s/%s", legacyRulerPrefix, url.PathEscape(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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) RouteDeleteRuleGroupConfig(ctx *contextmodel.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
|
|
|
}
|
2024-02-16 02:43:47 -06:00
|
|
|
return r.requester.withReq(
|
2021-03-19 09:32:13 -05:00
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodDelete,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
legacyRulerPrefix,
|
2024-02-16 02:43:47 -06:00
|
|
|
url.PathEscape(namespace),
|
|
|
|
url.PathEscape(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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) RouteGetNamespaceRulesConfig(ctx *contextmodel.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
|
|
|
}
|
2024-02-16 02:43:47 -06:00
|
|
|
return r.requester.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s",
|
|
|
|
legacyRulerPrefix,
|
2024-02-16 02:43:47 -06:00
|
|
|
url.PathEscape(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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) RouteGetRulegGroupConfig(ctx *contextmodel.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
|
|
|
}
|
2024-02-16 02:43:47 -06:00
|
|
|
return r.requester.withReq(
|
2021-03-19 09:32:13 -05:00
|
|
|
ctx,
|
2021-04-07 14:36:50 -05:00
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/%s/%s",
|
|
|
|
legacyRulerPrefix,
|
2024-02-16 02:43:47 -06:00
|
|
|
url.PathEscape(namespace),
|
|
|
|
url.PathEscape(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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) RouteGetRulesConfig(ctx *contextmodel.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
|
|
|
|
2024-02-16 02:43:47 -06:00
|
|
|
return r.requester.withReq(
|
2021-03-19 09:32:13 -05:00
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) RoutePostNameRulesConfig(ctx *contextmodel.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))
|
2024-02-16 02:43:47 -06:00
|
|
|
return r.requester.withReq(ctx, http.MethodPost, u, bytes.NewBuffer(yml), jsonExtractor(nil), nil)
|
2021-03-19 09:32:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (r *LotexRuler) validateAndGetPrefix(ctx *contextmodel.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
|
|
|
}
|
|
|
|
|
2023-04-12 11:30:33 -05:00
|
|
|
ds, err := r.DataProxy.DataSourceCache.GetDatasourceByUID(ctx.Req.Context(), datasourceUID, ctx.SignedInUser, ctx.SkipDSCache)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-31 11:40:28 -05:00
|
|
|
|
2021-11-11 00:45:56 -06:00
|
|
|
// Validate URL
|
2023-02-02 10:22:43 -06:00
|
|
|
if ds.URL == "" {
|
2021-11-11 00:45:56 -06:00
|
|
|
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 {
|
2022-10-31 11:40:28 -05:00
|
|
|
r.log.Debug(
|
2022-11-02 17:36:14 -05:00
|
|
|
"Unable to determine prometheus datasource subtype, using default prefix",
|
2023-02-02 10:22:43 -06:00
|
|
|
"datasource", ds.UID, "datasourceType", ds.Type, "subtype", subtype, "prefix", prefix)
|
2022-04-04 12:30:17 -05:00
|
|
|
return prefix, nil
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:36:14 -05:00
|
|
|
r.log.Debug("Determined prometheus datasource subtype",
|
2023-02-02 10:22:43 -06:00
|
|
|
"datasource", ds.UID, "datasourceType", ds.Type, "subtype", subtype)
|
2022-04-04 12:30:17 -05:00
|
|
|
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 {
|
2024-02-16 02:43:47 -06:00
|
|
|
u.Path, _ = url.PathUnescape(newPath)
|
|
|
|
u.RawPath = newPath
|
|
|
|
|
2021-03-19 09:32:13 -05:00
|
|
|
return &u
|
|
|
|
}
|