2021-03-24 06:43:25 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-03-29 00:55:09 -05:00
|
|
|
"fmt"
|
2021-03-24 06:43:25 -05:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2021-03-24 06:43:25 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2022-01-14 10:55:57 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2021-03-24 06:43:25 -05:00
|
|
|
)
|
|
|
|
|
2021-03-29 00:55:09 -05:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
2021-03-24 06:43:25 -05:00
|
|
|
|
|
|
|
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 {
|
2021-03-29 00:55:09 -05:00
|
|
|
endpoints, err := p.getEndpoints(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(http.StatusInternalServerError, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
|
|
|
|
2021-03-24 06:43:25 -05:00
|
|
|
return p.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
endpoints.alerts,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-24 06:43:25 -05:00
|
|
|
jsonExtractor(&apimodels.AlertResponse{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-24 06:43:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LotexProm) RouteGetRuleStatuses(ctx *models.ReqContext) response.Response {
|
2021-03-29 00:55:09 -05:00
|
|
|
endpoints, err := p.getEndpoints(ctx)
|
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(http.StatusInternalServerError, err, "")
|
2021-03-29 00:55:09 -05:00
|
|
|
}
|
|
|
|
|
2021-03-24 06:43:25 -05:00
|
|
|
return p.withReq(
|
2021-04-07 14:36:50 -05:00
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
withPath(
|
|
|
|
*ctx.Req.URL,
|
|
|
|
endpoints.rules,
|
|
|
|
),
|
|
|
|
nil,
|
2021-03-24 06:43:25 -05:00
|
|
|
jsonExtractor(&apimodels.RuleResponse{}),
|
2021-04-07 14:36:50 -05:00
|
|
|
nil,
|
2021-03-24 06:43:25 -05:00
|
|
|
)
|
|
|
|
}
|
2021-03-29 00:55:09 -05:00
|
|
|
|
|
|
|
func (p *LotexProm) getEndpoints(ctx *models.ReqContext) (*promEndpoints, error) {
|
2022-04-20 08:20:17 -05:00
|
|
|
datasourceID, err := strconv.ParseInt(web.Params(ctx.Req)[":DatasourceID"], 10, 64)
|
2022-01-14 10:55:57 -06:00
|
|
|
if err != nil {
|
2022-04-20 08:20:17 -05:00
|
|
|
return nil, fmt.Errorf("datasource ID is invalid")
|
2022-01-14 10:55:57 -06:00
|
|
|
}
|
|
|
|
|
2022-04-20 08:20:17 -05:00
|
|
|
ds, err := p.DataProxy.DataSourceCache.GetDatasource(ctx.Req.Context(), datasourceID, ctx.SignedInUser, ctx.SkipCache)
|
2021-03-29 00:55:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-11 00:45:56 -06:00
|
|
|
|
|
|
|
if ds.Url == "" {
|
|
|
|
return nil, fmt.Errorf("URL for this data source is empty")
|
|
|
|
}
|
|
|
|
|
2021-03-29 00:55:09 -05:00
|
|
|
routes, ok := dsTypeToLotexRoutes[ds.Type]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected datasource type. expecting loki or prometheus")
|
|
|
|
}
|
|
|
|
return &routes, nil
|
|
|
|
}
|