NgAlerting: loki & cortex have different prom & ruler endpoint prefixes (#32344)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
apimodels "github.com/grafana/alerting-api/pkg/api"
|
||||
@@ -9,10 +10,20 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
const (
|
||||
promRulesPath = "/prometheus/api/v1/rules"
|
||||
promAlertsPath = "/prometheus/api/v1/alerts"
|
||||
)
|
||||
type promEndpoints struct {
|
||||
rules, alerts string
|
||||
}
|
||||
|
||||
var dsTypeToLotexRoutes = map[string]promEndpoints{
|
||||
"prometheus": {
|
||||
rules: "/api/v1/rules",
|
||||
alerts: "/api/v1/alerts",
|
||||
},
|
||||
"loki": {
|
||||
rules: "/prometheus/api/v1/rules",
|
||||
alerts: "/prometheus/api/v1/alerts",
|
||||
},
|
||||
}
|
||||
|
||||
type LotexProm struct {
|
||||
log log.Logger
|
||||
@@ -27,11 +38,16 @@ func NewLotexProm(proxy *AlertingProxy, log log.Logger) *LotexProm {
|
||||
}
|
||||
|
||||
func (p *LotexProm) RouteGetAlertStatuses(ctx *models.ReqContext) response.Response {
|
||||
endpoints, err := p.getEndpoints(ctx)
|
||||
if err != nil {
|
||||
return response.Error(500, err.Error(), nil)
|
||||
}
|
||||
|
||||
return p.withReq(
|
||||
ctx, &http.Request{
|
||||
URL: withPath(
|
||||
*ctx.Req.URL,
|
||||
promAlertsPath,
|
||||
endpoints.alerts,
|
||||
),
|
||||
},
|
||||
jsonExtractor(&apimodels.AlertResponse{}),
|
||||
@@ -39,13 +55,30 @@ func (p *LotexProm) RouteGetAlertStatuses(ctx *models.ReqContext) response.Respo
|
||||
}
|
||||
|
||||
func (p *LotexProm) RouteGetRuleStatuses(ctx *models.ReqContext) response.Response {
|
||||
endpoints, err := p.getEndpoints(ctx)
|
||||
if err != nil {
|
||||
return response.Error(500, err.Error(), nil)
|
||||
}
|
||||
|
||||
return p.withReq(
|
||||
ctx, &http.Request{
|
||||
URL: withPath(
|
||||
*ctx.Req.URL,
|
||||
promRulesPath,
|
||||
endpoints.rules,
|
||||
),
|
||||
},
|
||||
jsonExtractor(&apimodels.RuleResponse{}),
|
||||
)
|
||||
}
|
||||
|
||||
func (p *LotexProm) getEndpoints(ctx *models.ReqContext) (*promEndpoints, error) {
|
||||
ds, err := p.DataProxy.DatasourceCache.GetDatasource(ctx.ParamsInt64("Recipient"), ctx.SignedInUser, ctx.SkipCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
routes, ok := dsTypeToLotexRoutes[ds.Type]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected datasource type. expecting loki or prometheus")
|
||||
}
|
||||
return &routes, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user