From c1c9ccb8e819eb5c8438cce7bdd5bf54d9fed30a Mon Sep 17 00:00:00 2001 From: Fabrizio <135109076+fabrizio-grafana@users.noreply.github.com> Date: Tue, 19 Mar 2024 10:59:53 +0100 Subject: [PATCH] Loki: Replace imports of `infra/httpclient` with SDK (#84337) --- pkg/tsdb/loki/healthcheck_test.go | 21 +++++++++++++++------ pkg/tsdb/loki/loki.go | 6 +++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pkg/tsdb/loki/healthcheck_test.go b/pkg/tsdb/loki/healthcheck_test.go index a889fefae08..f4e8db583eb 100644 --- a/pkg/tsdb/loki/healthcheck_test.go +++ b/pkg/tsdb/loki/healthcheck_test.go @@ -10,8 +10,8 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" - sdkHttpClient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" - "github.com/grafana/grafana/pkg/infra/httpclient" + "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -64,21 +64,30 @@ func (rt *healthCheckFailRoundTripper) RoundTrip(req *http.Request) (*http.Respo }, 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{} provider.RoundTripper = new(T) client.Transport = *provider.RoundTripper 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 } -func getMockProvider[T http.RoundTripper]() *healthCheckProvider[T] { - return &healthCheckProvider[T]{ +// Return a mocked HTTP client provider. +// +// Example taken from `pkg/promlib/healthcheck_test.go` +func getMockProvider[T http.RoundTripper]() *httpclient.Provider { + p := &healthCheckProvider[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) { diff --git a/pkg/tsdb/loki/loki.go b/pkg/tsdb/loki/loki.go index df2302d7727..f62bee56d78 100644 --- a/pkg/tsdb/loki/loki.go +++ b/pkg/tsdb/loki/loki.go @@ -19,7 +19,7 @@ import ( "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" - "github.com/grafana/grafana/pkg/infra/httpclient" + "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -42,7 +42,7 @@ var ( _ backend.CallResourceHandler = (*Service)(nil) ) -func ProvideService(httpClientProvider httpclient.Provider, features featuremgmt.FeatureToggles, tracer tracing.Tracer) *Service { +func ProvideService(httpClientProvider *httpclient.Provider, features featuremgmt.FeatureToggles, tracer tracing.Tracer) *Service { return &Service{ im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider)), features: features, @@ -88,7 +88,7 @@ func parseQueryModel(raw json.RawMessage) (*QueryJSONModel, error) { return model, err } -func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.InstanceFactoryFunc { +func newInstanceSettings(httpClientProvider *httpclient.Provider) datasource.InstanceFactoryFunc { return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { opts, err := settings.HTTPClientOptions(ctx) if err != nil {