Prometheus: Remove grafana/pkg/setting imports (#81813)

* upgrade grafana-azure-sdk-go v1.12.0

* add AzureAuthEnabled to config

* remove grafana/pkg/setting imports

* remove grafana/pkg/setting imports

* remove comment

* add better error message

* Fix the unit test

* fix lint
This commit is contained in:
ismail simsek 2024-02-07 11:01:09 +01:00 committed by GitHub
parent 9a045e24e8
commit 2679599968
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 35 additions and 38 deletions

View File

@ -78,7 +78,7 @@ func TestIntegrationPluginManager(t *testing.T) {
idb := influxdb.ProvideService(hcp, features)
lk := loki.ProvideService(hcp, features, tracer)
otsdb := opentsdb.ProvideService(hcp)
pr := prometheus.ProvideService(hcp, cfg)
pr := prometheus.ProvideService(hcp)
tmpo := tempo.ProvideService(hcp)
td := testdatasource.ProvideService()
pg := postgres.ProvideService(cfg)

View File

@ -8,14 +8,10 @@ import (
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/setting"
)
func TestConfigureAzureAuthentication(t *testing.T) {
cfg := &setting.Cfg{
Azure: &azsettings.AzureSettings{},
}
cfgAzure := &azsettings.AzureSettings{}
t.Run("should set Azure middleware when JsonData contains valid credentials", func(t *testing.T) {
settings := backend.DataSourceInstanceSettings{
@ -29,7 +25,7 @@ func TestConfigureAzureAuthentication(t *testing.T) {
var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}}
err := ConfigureAzureAuthentication(settings, cfg.Azure, opts)
err := ConfigureAzureAuthentication(settings, cfgAzure, opts)
require.NoError(t, err)
require.NotNil(t, opts.Middlewares)
@ -43,7 +39,7 @@ func TestConfigureAzureAuthentication(t *testing.T) {
var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}}
err := ConfigureAzureAuthentication(settings, cfg.Azure, opts)
err := ConfigureAzureAuthentication(settings, cfgAzure, opts)
require.NoError(t, err)
assert.NotContains(t, opts.CustomOptions, "_azureCredentials")
@ -58,7 +54,7 @@ func TestConfigureAzureAuthentication(t *testing.T) {
}
var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}}
err := ConfigureAzureAuthentication(settings, cfg.Azure, opts)
err := ConfigureAzureAuthentication(settings, cfgAzure, opts)
assert.Error(t, err)
})
@ -74,7 +70,7 @@ func TestConfigureAzureAuthentication(t *testing.T) {
}
var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}}
err := ConfigureAzureAuthentication(settings, cfg.Azure, opts)
err := ConfigureAzureAuthentication(settings, cfgAzure, opts)
require.NoError(t, err)
require.NotNil(t, opts.Middlewares)
@ -90,7 +86,7 @@ func TestConfigureAzureAuthentication(t *testing.T) {
}
var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}}
err := ConfigureAzureAuthentication(settings, cfg.Azure, opts)
err := ConfigureAzureAuthentication(settings, cfgAzure, opts)
require.NoError(t, err)
if opts.Middlewares != nil {
@ -111,7 +107,7 @@ func TestConfigureAzureAuthentication(t *testing.T) {
var opts = &sdkhttpclient.Options{CustomOptions: map[string]any{}}
err := ConfigureAzureAuthentication(settings, cfg.Azure, opts)
err := ConfigureAzureAuthentication(settings, cfgAzure, opts)
assert.Error(t, err)
})
}

View File

@ -5,13 +5,13 @@ import (
"fmt"
"strings"
"github.com/grafana/grafana-azure-sdk-go/azsettings"
"github.com/grafana/grafana-azure-sdk-go/util/maputil"
"github.com/grafana/grafana-plugin-sdk-go/backend"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth"
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
@ -19,7 +19,7 @@ import (
// CreateTransportOptions creates options for the http client. Probably should be shared and should not live in the
// buffered package.
func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, cfg *setting.Cfg, logger log.Logger) (*sdkhttpclient.Options, error) {
func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, logger log.Logger) (*sdkhttpclient.Options, error) {
opts, err := settings.HTTPClientOptions(ctx)
if err != nil {
return nil, fmt.Errorf("error getting HTTP options: %w", err)
@ -38,9 +38,15 @@ func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInst
opts.SigV4.Service = "aps"
}
azureSettings, err := azsettings.ReadSettings(ctx)
if err != nil {
logger.Error("failed to read Azure settings from Grafana", "error", err.Error())
return nil, fmt.Errorf("failed to read Azure settings from Grafana: %v", err)
}
// Set Azure authentication
if cfg.AzureAuthEnabled {
err = azureauth.ConfigureAzureAuthentication(settings, cfg.Azure, &opts)
if azureSettings.AzureAuthEnabled {
err = azureauth.ConfigureAzureAuthentication(settings, azureSettings, &opts)
if err != nil {
return nil, fmt.Errorf("error configuring Azure auth: %v", err)
}

View File

@ -7,8 +7,6 @@ import (
"github.com/grafana/grafana-azure-sdk-go/azsettings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/setting"
)
func TestCreateTransportOptions(t *testing.T) {
@ -21,13 +19,17 @@ func TestCreateTransportOptions(t *testing.T) {
"httpHeaderValue1": "bar",
},
}
opts, err := CreateTransportOptions(context.Background(), settings, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))
opts, err := CreateTransportOptions(context.Background(), settings, backend.NewLoggerWith("logger", "test"))
require.NoError(t, err)
require.Equal(t, map[string]string{"foo": "bar"}, opts.Headers)
require.Equal(t, 2, len(opts.Middlewares))
})
t.Run("add azure credentials if configured", func(t *testing.T) {
cfg := backend.NewGrafanaCfg(map[string]string{
azsettings.AzureCloud: azsettings.AzurePublic,
azsettings.AzureAuthEnabled: "true",
})
settings := backend.DataSourceInstanceSettings{
BasicAuthEnabled: false,
BasicAuthUser: "",
@ -38,7 +40,8 @@ func TestCreateTransportOptions(t *testing.T) {
}`),
DecryptedSecureJSONData: map[string]string{},
}
opts, err := CreateTransportOptions(context.Background(), settings, &setting.Cfg{AzureAuthEnabled: true, Azure: &azsettings.AzureSettings{}}, backend.NewLoggerWith("logger", "test"))
ctx := backend.WithGrafanaConfig(context.Background(), cfg)
opts, err := CreateTransportOptions(ctx, settings, backend.NewLoggerWith("logger", "test"))
require.NoError(t, err)
require.Equal(t, 3, len(opts.Middlewares))
})

View File

@ -12,8 +12,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/setting"
)
type healthCheckProvider[T http.RoundTripper] struct {
@ -84,7 +82,7 @@ func Test_healthcheck(t *testing.T) {
t.Run("should do a successful health check", func(t *testing.T) {
httpProvider := getMockProvider[*healthCheckSuccessRoundTripper]()
s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))),
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))),
}
req := &backend.CheckHealthRequest{
@ -100,7 +98,7 @@ func Test_healthcheck(t *testing.T) {
t.Run("should return an error for an unsuccessful health check", func(t *testing.T) {
httpProvider := getMockProvider[*healthCheckFailRoundTripper]()
s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))),
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))),
}
req := &backend.CheckHealthRequest{

View File

@ -14,8 +14,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/setting"
)
type heuristicsSuccessRoundTripper struct {
@ -52,7 +50,7 @@ func Test_GetHeuristics(t *testing.T) {
//httpProvider := getHeuristicsMockProvider(&rt)
httpProvider := newHeuristicsSDKProvider(rt)
s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))),
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))),
}
req := HeuristicsRequest{
@ -72,7 +70,7 @@ func Test_GetHeuristics(t *testing.T) {
}
httpProvider := newHeuristicsSDKProvider(rt)
s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))),
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))),
}
req := HeuristicsRequest{

View File

@ -16,7 +16,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
"github.com/grafana/grafana/pkg/tsdb/prometheus/instrumentation"
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata"
@ -34,19 +33,19 @@ type instance struct {
versionCache *cache.Cache
}
func ProvideService(httpClientProvider *httpclient.Provider, cfg *setting.Cfg) *Service {
func ProvideService(httpClientProvider *httpclient.Provider) *Service {
plog := backend.NewLoggerWith("logger", "tsdb.prometheus")
plog.Debug("Initializing")
return &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, cfg, plog)),
im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, plog)),
logger: plog,
}
}
func newInstanceSettings(httpClientProvider *httpclient.Provider, cfg *setting.Cfg, log log.Logger) datasource.InstanceFactoryFunc {
func newInstanceSettings(httpClientProvider *httpclient.Provider, log log.Logger) datasource.InstanceFactoryFunc {
return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
// Creates a http roundTripper.
opts, err := client.CreateTransportOptions(ctx, settings, cfg, log)
opts, err := client.CreateTransportOptions(ctx, settings, log)
if err != nil {
return nil, fmt.Errorf("error creating transport options: %v", err)
}

View File

@ -11,8 +11,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/setting"
)
type fakeSender struct{}
@ -69,7 +67,7 @@ func TestService(t *testing.T) {
f := &fakeHTTPClientProvider{}
httpProvider := getMockPromTestSDKProvider(f)
service := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, backend.NewLoggerWith("logger", "test"))),
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))),
}
req := &backend.CallResourceRequest{

View File

@ -24,7 +24,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/prometheus/client"
"github.com/grafana/grafana/pkg/tsdb/prometheus/models"
"github.com/grafana/grafana/pkg/tsdb/prometheus/querydata"
@ -441,7 +440,7 @@ func setup() (*testContext, error) {
JSONData: json.RawMessage(`{"timeInterval": "15s"}`),
}
opts, err := client.CreateTransportOptions(context.Background(), settings, &setting.Cfg{}, log.New())
opts, err := client.CreateTransportOptions(context.Background(), settings, log.New())
if err != nil {
return nil, err
}