grafana/pkg/services/ngalert/api/lotex_prom.go
Arve Knudsen 78596a6756
Migrate to Wire for dependency injection (#32289)
Fixes #30144

Co-authored-by: dsotirakis <sotirakis.dim@gmail.com>
Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Co-authored-by: Ida Furjesova <ida.furjesova@grafana.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
Co-authored-by: spinillos <selenepinillos@gmail.com>
Co-authored-by: Karl Persson <kalle.persson@grafana.com>
Co-authored-by: Leonard Gram <leo@xlson.com>
2021-08-25 15:11:22 +02:00

89 lines
1.8 KiB
Go

package api
import (
"fmt"
"net/http"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
)
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
*AlertingProxy
}
func NewLotexProm(proxy *AlertingProxy, log log.Logger) *LotexProm {
return &LotexProm{
log: log,
AlertingProxy: proxy,
}
}
func (p *LotexProm) RouteGetAlertStatuses(ctx *models.ReqContext) response.Response {
endpoints, err := p.getEndpoints(ctx)
if err != nil {
return ErrResp(http.StatusInternalServerError, err, "")
}
return p.withReq(
ctx,
http.MethodGet,
withPath(
*ctx.Req.URL,
endpoints.alerts,
),
nil,
jsonExtractor(&apimodels.AlertResponse{}),
nil,
)
}
func (p *LotexProm) RouteGetRuleStatuses(ctx *models.ReqContext) response.Response {
endpoints, err := p.getEndpoints(ctx)
if err != nil {
return ErrResp(http.StatusInternalServerError, err, "")
}
return p.withReq(
ctx,
http.MethodGet,
withPath(
*ctx.Req.URL,
endpoints.rules,
),
nil,
jsonExtractor(&apimodels.RuleResponse{}),
nil,
)
}
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
}