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
|
|
|
)
|
|
|
|
|
|
|
|
type ForkedPromSvc struct {
|
|
|
|
ProxySvc, GrafanaSvc PrometheusApiService
|
|
|
|
DatasourceCache datasources.CacheService
|
|
|
|
}
|
|
|
|
|
2021-04-09 04:55:41 -05:00
|
|
|
// NewForkedProm implements a set of routes that proxy to various Prometheus-compatible backends.
|
2021-03-24 06:43:25 -05:00
|
|
|
func NewForkedProm(datasourceCache datasources.CacheService, proxy, grafana PrometheusApiService) *ForkedPromSvc {
|
|
|
|
return &ForkedPromSvc{
|
|
|
|
ProxySvc: proxy,
|
|
|
|
GrafanaSvc: grafana,
|
|
|
|
DatasourceCache: datasourceCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ForkedPromSvc) RouteGetAlertStatuses(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, p.DatasourceCache)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case apimodels.GrafanaBackend:
|
|
|
|
return p.GrafanaSvc.RouteGetAlertStatuses(ctx)
|
|
|
|
case apimodels.LoTexRulerBackend:
|
|
|
|
return p.ProxySvc.RouteGetAlertStatuses(ctx)
|
|
|
|
default:
|
|
|
|
return response.Error(400, fmt.Sprintf("unexpected backend type (%v)", t), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ForkedPromSvc) RouteGetRuleStatuses(ctx *models.ReqContext) response.Response {
|
|
|
|
t, err := backendType(ctx, p.DatasourceCache)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(400, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case apimodels.GrafanaBackend:
|
|
|
|
return p.GrafanaSvc.RouteGetRuleStatuses(ctx)
|
|
|
|
case apimodels.LoTexRulerBackend:
|
|
|
|
return p.ProxySvc.RouteGetRuleStatuses(ctx)
|
|
|
|
default:
|
|
|
|
return response.Error(400, fmt.Sprintf("unexpected backend type (%v)", t), nil)
|
|
|
|
}
|
|
|
|
}
|