Prometheus: (Chore) Switch to sdk httpclient from infra httpclient (#76955)

This commit is contained in:
Kyle Brandt 2023-10-23 10:40:43 -04:00 committed by GitHub
parent 9dc6cac1f2
commit 1b420585f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 52 deletions

View File

@ -10,8 +10,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend" "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/datasource"
sdkHttpClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -58,21 +57,27 @@ func (rt *healthCheckFailRoundTripper) RoundTrip(req *http.Request) (*http.Respo
}, nil }, nil
} }
func (provider *healthCheckProvider[T]) New(opts ...sdkHttpClient.Options) (*http.Client, error) { func (provider *healthCheckProvider[T]) New(opts ...httpclient.Options) (*http.Client, error) {
client := &http.Client{} client := &http.Client{}
provider.RoundTripper = new(T) provider.RoundTripper = new(T)
client.Transport = *provider.RoundTripper client.Transport = *provider.RoundTripper
return client, nil return client, nil
} }
func (provider *healthCheckProvider[T]) GetTransport(opts ...sdkHttpClient.Options) (http.RoundTripper, error) { func (provider *healthCheckProvider[T]) GetTransport(opts ...httpclient.Options) (http.RoundTripper, error) {
return *new(T), nil return *new(T), nil
} }
func getMockProvider[T http.RoundTripper]() *healthCheckProvider[T] { func getMockProvider[T http.RoundTripper]() *httpclient.Provider {
return &healthCheckProvider[T]{ p := &healthCheckProvider[T]{
RoundTripper: new(T), RoundTripper: new(T),
} }
anotherFN := func(o httpclient.Options, next http.RoundTripper) http.RoundTripper {
return *p.RoundTripper
}
fn := httpclient.MiddlewareFunc(anotherFN)
mid := httpclient.NamedMiddlewareFunc("mock", fn)
return httpclient.NewProvider(httpclient.ProviderOptions{Middlewares: []httpclient.Middleware{mid}})
} }
func Test_healthcheck(t *testing.T) { func Test_healthcheck(t *testing.T) {

View File

@ -13,17 +13,11 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend" "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/datasource"
sdkHttpClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
type heuristicsProvider struct {
httpclient.Provider
http.RoundTripper
}
type heuristicsSuccessRoundTripper struct { type heuristicsSuccessRoundTripper struct {
res io.ReadCloser res io.ReadCloser
status int status int
@ -40,20 +34,13 @@ func (rt *heuristicsSuccessRoundTripper) RoundTrip(req *http.Request) (*http.Res
}, nil }, nil
} }
func (provider *heuristicsProvider) New(opts ...sdkHttpClient.Options) (*http.Client, error) { func newHeuristicsSDKProvider(hrt heuristicsSuccessRoundTripper) *httpclient.Provider {
client := &http.Client{} anotherFN := func(o httpclient.Options, next http.RoundTripper) http.RoundTripper {
client.Transport = provider.RoundTripper return &hrt
return client, nil
}
func (provider *heuristicsProvider) GetTransport(opts ...sdkHttpClient.Options) (http.RoundTripper, error) {
return provider.RoundTripper, nil
}
func getHeuristicsMockProvider(rt http.RoundTripper) *heuristicsProvider {
return &heuristicsProvider{
RoundTripper: rt,
} }
fn := httpclient.MiddlewareFunc(anotherFN)
mid := httpclient.NamedMiddlewareFunc("mock", fn)
return httpclient.NewProvider(httpclient.ProviderOptions{Middlewares: []httpclient.Middleware{mid}})
} }
func Test_GetHeuristics(t *testing.T) { func Test_GetHeuristics(t *testing.T) {
@ -62,7 +49,8 @@ func Test_GetHeuristics(t *testing.T) {
res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"version\":\"1.0\"}}")), res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"version\":\"1.0\"}}")),
status: http.StatusOK, status: http.StatusOK,
} }
httpProvider := getHeuristicsMockProvider(&rt) //httpProvider := getHeuristicsMockProvider(&rt)
httpProvider := newHeuristicsSDKProvider(rt)
s := &Service{ s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, &featuremgmt.FeatureManager{}, nil, backend.NewLoggerWith("logger", "test"))), im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, &featuremgmt.FeatureManager{}, nil, backend.NewLoggerWith("logger", "test"))),
} }
@ -82,7 +70,7 @@ func Test_GetHeuristics(t *testing.T) {
res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"features\":{\"foo\":\"bar\"},\"version\":\"1.0\"}}")), res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"features\":{\"foo\":\"bar\"},\"version\":\"1.0\"}}")),
status: http.StatusOK, status: http.StatusOK,
} }
httpProvider := getHeuristicsMockProvider(&rt) httpProvider := newHeuristicsSDKProvider(rt)
s := &Service{ s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, &featuremgmt.FeatureManager{}, nil, backend.NewLoggerWith("logger", "test"))), im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, &featuremgmt.FeatureManager{}, nil, backend.NewLoggerWith("logger", "test"))),
} }

View File

@ -14,7 +14,7 @@ import (
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/grafana/grafana/pkg/infra/httpclient" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
@ -36,7 +36,7 @@ type instance struct {
versionCache *cache.Cache versionCache *cache.Cache
} }
func ProvideService(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) *Service { func ProvideService(httpClientProvider *httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer) *Service {
plog := backend.NewLoggerWith("logger", "tsdb.prometheus") plog := backend.NewLoggerWith("logger", "tsdb.prometheus")
plog.Debug("Initializing") plog.Debug("Initializing")
return &Service{ return &Service{
@ -46,7 +46,7 @@ func ProvideService(httpClientProvider httpclient.Provider, cfg *setting.Cfg, fe
} }
} }
func newInstanceSettings(httpClientProvider httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer, log log.Logger) datasource.InstanceFactoryFunc { func newInstanceSettings(httpClientProvider *httpclient.Provider, cfg *setting.Cfg, features featuremgmt.FeatureToggles, tracer tracing.Tracer, log log.Logger) datasource.InstanceFactoryFunc {
return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
// Creates a http roundTripper. // Creates a http roundTripper.
opts, err := client.CreateTransportOptions(ctx, settings, cfg, log) opts, err := client.CreateTransportOptions(ctx, settings, cfg, log)

View File

@ -9,12 +9,10 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend" "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/datasource"
sdkHttpClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/require"
) )
type fakeSender struct{} type fakeSender struct{}
@ -43,22 +41,33 @@ type fakeHTTPClientProvider struct {
Roundtripper *fakeRoundtripper Roundtripper *fakeRoundtripper
} }
func (provider *fakeHTTPClientProvider) New(opts ...sdkHttpClient.Options) (*http.Client, error) { func (provider *fakeHTTPClientProvider) New(opts ...httpclient.Options) (*http.Client, error) {
client := &http.Client{} client := &http.Client{}
provider.Roundtripper = &fakeRoundtripper{} provider.Roundtripper = &fakeRoundtripper{}
client.Transport = provider.Roundtripper client.Transport = provider.Roundtripper
return client, nil return client, nil
} }
func (provider *fakeHTTPClientProvider) GetTransport(opts ...sdkHttpClient.Options) (http.RoundTripper, error) { func (provider *fakeHTTPClientProvider) GetTransport(opts ...httpclient.Options) (http.RoundTripper, error) {
return &fakeRoundtripper{}, nil return &fakeRoundtripper{}, nil
} }
func getMockPromTestSDKProvider(f *fakeHTTPClientProvider) *httpclient.Provider {
anotherFN := func(o httpclient.Options, next http.RoundTripper) http.RoundTripper {
_, _ = f.New()
return f.Roundtripper
}
fn := httpclient.MiddlewareFunc(anotherFN)
mid := httpclient.NamedMiddlewareFunc("mock", fn)
return httpclient.NewProvider(httpclient.ProviderOptions{Middlewares: []httpclient.Middleware{mid}})
}
func TestService(t *testing.T) { func TestService(t *testing.T) {
t.Run("Service", func(t *testing.T) { t.Run("Service", func(t *testing.T) {
t.Run("CallResource", func(t *testing.T) { t.Run("CallResource", func(t *testing.T) {
t.Run("creates correct request", func(t *testing.T) { t.Run("creates correct request", func(t *testing.T) {
httpProvider := &fakeHTTPClientProvider{} f := &fakeHTTPClientProvider{}
httpProvider := getMockPromTestSDKProvider(f)
service := &Service{ service := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, &featuremgmt.FeatureManager{}, nil, backend.NewLoggerWith("logger", "test"))), im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, &setting.Cfg{}, &featuremgmt.FeatureManager{}, nil, backend.NewLoggerWith("logger", "test"))),
} }
@ -98,12 +107,12 @@ func TestService(t *testing.T) {
"Content-Type": {"application/x-www-form-urlencoded"}, "Content-Type": {"application/x-www-form-urlencoded"},
"Idempotency-Key": []string(nil), "Idempotency-Key": []string(nil),
}, },
httpProvider.Roundtripper.Req.Header) f.Roundtripper.Req.Header)
require.Equal(t, http.MethodPost, httpProvider.Roundtripper.Req.Method) require.Equal(t, http.MethodPost, f.Roundtripper.Req.Method)
body, err := io.ReadAll(httpProvider.Roundtripper.Req.Body) body, err := io.ReadAll(f.Roundtripper.Req.Body)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"), body) require.Equal(t, []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"), body)
require.Equal(t, "http://localhost:9090/api/v1/series", httpProvider.Roundtripper.Req.URL.String()) require.Equal(t, "http://localhost:9090/api/v1/series", f.Roundtripper.Req.URL.String())
}) })
}) })
}) })

View File

@ -11,9 +11,6 @@ import (
"testing" "testing"
"time" "time"
"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/data"
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
p "github.com/prometheus/common/model" p "github.com/prometheus/common/model"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -22,8 +19,10 @@ import (
"github.com/grafana/kindsys" "github.com/grafana/kindsys"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana/pkg/infra/httpclient" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb/prometheus/client" "github.com/grafana/grafana/pkg/tsdb/prometheus/client"
@ -430,8 +429,8 @@ type testContext struct {
func setup() (*testContext, error) { func setup() (*testContext, error) {
tracer := tracing.InitializeTracerForTest() tracer := tracing.InitializeTracerForTest()
httpProvider := &fakeHttpClientProvider{ httpProvider := &fakeHttpClientProvider{
opts: sdkhttpclient.Options{ opts: httpclient.Options{
Timeouts: &sdkhttpclient.DefaultTimeoutOptions, Timeouts: &httpclient.DefaultTimeoutOptions,
}, },
res: &http.Response{ res: &http.Response{
StatusCode: 200, StatusCode: 200,
@ -473,14 +472,14 @@ func (f *fakeFeatureToggles) IsEnabled(feature string) bool {
type fakeHttpClientProvider struct { type fakeHttpClientProvider struct {
httpclient.Provider httpclient.Provider
opts sdkhttpclient.Options opts httpclient.Options
req *http.Request req *http.Request
res *http.Response res *http.Response
} }
func (p *fakeHttpClientProvider) New(opts ...sdkhttpclient.Options) (*http.Client, error) { func (p *fakeHttpClientProvider) New(opts ...httpclient.Options) (*http.Client, error) {
p.opts = opts[0] p.opts = opts[0]
c, err := sdkhttpclient.New(opts[0]) c, err := httpclient.New(opts[0])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -488,7 +487,7 @@ func (p *fakeHttpClientProvider) New(opts ...sdkhttpclient.Options) (*http.Clien
return c, nil return c, nil
} }
func (p *fakeHttpClientProvider) GetTransport(opts ...sdkhttpclient.Options) (http.RoundTripper, error) { func (p *fakeHttpClientProvider) GetTransport(opts ...httpclient.Options) (http.RoundTripper, error) {
p.opts = opts[0] p.opts = opts[0]
return http.DefaultTransport, nil return http.DefaultTransport, nil
} }