mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Add support to distinguish Prometheus datasource subtypes (Mimir, Cortex and Vanilla Prometheus) (#46771)
* Add basic UI for custom ruler URL * Add build info fetching for alerting data sources * Add keeping data sources build info in the store * Use data source build info to construct data source urls * Remove unused code * Add custom ruler support in prometheus api calls * Migrate actions * Use thunk condition to prevent multiple data source buildinfo fetches * Unify prom and ruler rules loading * Upgrade RuleEditor tests * Upgrade RuleList tests * Upgrade PanelAlertTab tests * Upgrade actions tests * Build info refactoring * Get rid of lotex ruler support action * Add prom ruler availability checking when the buildinfo is not available * Add rulerUrlBuilder tests * Improve prometheus data source validation, small build info refactoring * Change prefix based on Prometheus subtype * Use the correct path * Revert config routing * Add deprecation notice for /api/prom prefix * Add tests to the datasource subtype * Remove custom ruler support * Remove deprecation notice * Prevent fetching ruler rules when ruler api is not available * Add build info tests * Unify naming of ruler methods * Fix test * Change buildinfo data source validation * Use strings for subtype params and unveil mimir * organise imports * frontend changes and wordsmithing * fix test suite * add a nicer verbose message for prometheus datasources * detect Mimir datasource * fix test * fix buildinfo test for Mimir * shrink vectors * add some code documentation * DRY prepareRulesFilterQueryParams * clarify that Prometheus does not support managing rules * Improve buildinfo error handling Co-authored-by: gotjosh <josue.abreu@gmail.com> Co-authored-by: gillesdemey <gilles.de.mey@gmail.com>
This commit is contained in:
@@ -16,9 +16,32 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
const (
|
||||
Prometheus = "prometheus"
|
||||
Cortex = "cortex"
|
||||
Mimir = "mimir"
|
||||
)
|
||||
|
||||
const (
|
||||
PrometheusDatasourceType = "prometheus"
|
||||
LokiDatasourceType = "loki"
|
||||
|
||||
mimirPrefix = "/config/v1/rules"
|
||||
prometheusPrefix = "/rules"
|
||||
lokiPrefix = "/api/prom/rules"
|
||||
|
||||
subtypeQuery = "subtype"
|
||||
)
|
||||
|
||||
var dsTypeToRulerPrefix = map[string]string{
|
||||
"prometheus": "/rules",
|
||||
"loki": "/api/prom/rules",
|
||||
PrometheusDatasourceType: prometheusPrefix,
|
||||
LokiDatasourceType: lokiPrefix,
|
||||
}
|
||||
|
||||
var subtypeToPrefix = map[string]string{
|
||||
Prometheus: prometheusPrefix,
|
||||
Cortex: prometheusPrefix,
|
||||
Mimir: mimirPrefix,
|
||||
}
|
||||
|
||||
type LotexRuler struct {
|
||||
@@ -171,7 +194,22 @@ func (r *LotexRuler) validateAndGetPrefix(ctx *models.ReqContext) (string, error
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unexpected datasource type. expecting loki or prometheus")
|
||||
}
|
||||
return prefix, nil
|
||||
|
||||
// If the datasource is Loki, there's nothing else for us to do - it doesn't have subtypes.
|
||||
if ds.Type == LokiDatasourceType {
|
||||
return prefix, nil
|
||||
}
|
||||
|
||||
// A Prometheus datasource, can have many subtypes: Cortex, Mimir and vanilla Prometheus.
|
||||
// Based on these subtypes, we want to use a different proxying path.
|
||||
subtype := ctx.Query(subtypeQuery)
|
||||
subTypePrefix, ok := subtypeToPrefix[subtype]
|
||||
if !ok {
|
||||
r.log.Debug("unable to determine prometheus datasource subtype, using default prefix", "subtype", subtype)
|
||||
return prefix, nil
|
||||
}
|
||||
|
||||
return subTypePrefix, nil
|
||||
}
|
||||
|
||||
func withPath(u url.URL, newPath string) *url.URL {
|
||||
|
||||
130
pkg/services/ngalert/api/lotex_ruler_test.go
Normal file
130
pkg/services/ngalert/api/lotex_ruler_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/datasourceproxy"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func TestLotexRuler_ValidateAndGetPrefix(t *testing.T) {
|
||||
tc := []struct {
|
||||
name string
|
||||
namedParams map[string]string
|
||||
urlParams string
|
||||
datasourceCache datasources.CacheService
|
||||
expected string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "with an invalid recipient",
|
||||
namedParams: map[string]string{":Recipient": "AAABBB"},
|
||||
err: errors.New("recipient is invalid"),
|
||||
},
|
||||
{
|
||||
name: "with an error while trying to fetch the datasource",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
datasourceCache: fakeCacheService{err: models.ErrDataSourceNotFound},
|
||||
err: errors.New("data source not found"),
|
||||
},
|
||||
{
|
||||
name: "with an empty datasource URL",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{}},
|
||||
err: errors.New("URL for this data source is empty"),
|
||||
},
|
||||
{
|
||||
name: "with an unsupported datasource type",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com"}},
|
||||
err: errors.New("unexpected datasource type. expecting loki or prometheus"),
|
||||
},
|
||||
{
|
||||
name: "with a Loki datasource",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com", Type: LokiDatasourceType}},
|
||||
expected: "/api/prom/rules",
|
||||
},
|
||||
{
|
||||
name: "with a Prometheus datasource",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com", Type: PrometheusDatasourceType}},
|
||||
expected: "/rules",
|
||||
},
|
||||
{
|
||||
name: "with a Prometheus datasource and subtype of Cortex",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
urlParams: "?subtype=cortex",
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com", Type: PrometheusDatasourceType}},
|
||||
expected: "/rules",
|
||||
},
|
||||
{
|
||||
name: "with a Prometheus datasource and subtype of Mimir",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
urlParams: "?subtype=mimir",
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com", Type: PrometheusDatasourceType}},
|
||||
expected: "/config/v1/rules",
|
||||
},
|
||||
{
|
||||
name: "with a Prometheus datasource and subtype of Prometheus",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
urlParams: "?subtype=prometheus",
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com", Type: PrometheusDatasourceType}},
|
||||
expected: "/rules",
|
||||
},
|
||||
{
|
||||
name: "with a Prometheus datasource and no subtype",
|
||||
namedParams: map[string]string{":Recipient": "164"},
|
||||
datasourceCache: fakeCacheService{datasource: &models.DataSource{Url: "http://loki.com", Type: PrometheusDatasourceType}},
|
||||
expected: "/rules",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tc {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Setup Proxy.
|
||||
proxy := &AlertingProxy{DataProxy: &datasourceproxy.DataSourceProxyService{DataSourceCache: tt.datasourceCache}}
|
||||
ruler := &LotexRuler{AlertingProxy: proxy, log: log.NewNopLogger()}
|
||||
|
||||
// Setup request context.
|
||||
httpReq, err := http.NewRequest(http.MethodGet, "http://grafanacloud.com"+tt.urlParams, nil)
|
||||
require.NoError(t, err)
|
||||
ctx := &models.ReqContext{Context: &web.Context{Req: web.SetURLParams(httpReq, tt.namedParams)}}
|
||||
|
||||
prefix, err := ruler.validateAndGetPrefix(ctx)
|
||||
require.Equal(t, tt.expected, prefix)
|
||||
if tt.err != nil {
|
||||
require.EqualError(t, err, tt.err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fakeCacheService struct {
|
||||
datasource *models.DataSource
|
||||
err error
|
||||
}
|
||||
|
||||
func (f fakeCacheService) GetDatasource(_ context.Context, datasourceID int64, _ *models.SignedInUser, _ bool) (*models.DataSource, error) {
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
}
|
||||
|
||||
return f.datasource, nil
|
||||
}
|
||||
|
||||
func (f fakeCacheService) GetDatasourceByUID(ctx context.Context, datasourceUID string, user *models.SignedInUser, skipCache bool) (*models.DataSource, error) {
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
}
|
||||
|
||||
return f.datasource, nil
|
||||
}
|
||||
Reference in New Issue
Block a user