2021-03-24 06:43:25 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2021-03-24 06:43:25 -05:00
|
|
|
)
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
type PrometheusApiHandler struct {
|
2022-02-04 11:42:04 -06:00
|
|
|
ProxySvc *LotexProm
|
|
|
|
GrafanaSvc *PrometheusSrv
|
|
|
|
DatasourceCache datasources.CacheService
|
2021-03-24 06:43:25 -05:00
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
// NewForkingProm implements a set of routes that proxy to various Prometheus-compatible backends.
|
|
|
|
func NewForkingProm(datasourceCache datasources.CacheService, proxy *LotexProm, grafana *PrometheusSrv) *PrometheusApiHandler {
|
|
|
|
return &PrometheusApiHandler{
|
2021-03-24 06:43:25 -05:00
|
|
|
ProxySvc: proxy,
|
|
|
|
GrafanaSvc: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *PrometheusApiHandler) handleRouteGetAlertStatuses(ctx *models.ReqContext, dsUID string) response.Response {
|
2022-05-06 14:05:02 -05:00
|
|
|
t, err := backendTypeByUID(ctx, f.DatasourceCache)
|
2021-03-24 06:43:25 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-24 06:43:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.ProxySvc.RouteGetAlertStatuses(ctx)
|
2021-03-24 06:43:25 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-24 06:43:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *PrometheusApiHandler) handleRouteGetRuleStatuses(ctx *models.ReqContext, dsUID string) response.Response {
|
2022-05-06 14:05:02 -05:00
|
|
|
t, err := backendTypeByUID(ctx, f.DatasourceCache)
|
2021-03-24 06:43:25 -05:00
|
|
|
if err != nil {
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, err, "")
|
2021-03-24 06:43:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case apimodels.LoTexRulerBackend:
|
2021-12-13 02:22:57 -06:00
|
|
|
return f.ProxySvc.RouteGetRuleStatuses(ctx)
|
2021-03-24 06:43:25 -05:00
|
|
|
default:
|
2021-05-28 10:55:03 -05:00
|
|
|
return ErrResp(400, fmt.Errorf("unexpected backend type (%v)", t), "")
|
2021-03-24 06:43:25 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-04 11:42:04 -06:00
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *PrometheusApiHandler) handleRouteGetGrafanaAlertStatuses(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetAlertStatuses(ctx)
|
|
|
|
}
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
func (f *PrometheusApiHandler) handleRouteGetGrafanaRuleStatuses(ctx *models.ReqContext) response.Response {
|
2022-02-04 11:42:04 -06:00
|
|
|
return f.GrafanaSvc.RouteGetRuleStatuses(ctx)
|
|
|
|
}
|