Loki: Replace imports of infra/httpclient with SDK (#84337)

This commit is contained in:
Fabrizio 2024-03-19 10:59:53 +01:00 committed by GitHub
parent 7c17290da5
commit c1c9ccb8e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 9 deletions

View File

@ -10,8 +10,8 @@ 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/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"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"
@ -64,21 +64,30 @@ 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] { // Return a mocked HTTP client provider.
return &healthCheckProvider[T]{ //
// Example taken from `pkg/promlib/healthcheck_test.go`
func getMockProvider[T http.RoundTripper]() *httpclient.Provider {
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

@ -19,7 +19,7 @@ import (
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace" "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/log"
"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"
@ -42,7 +42,7 @@ var (
_ backend.CallResourceHandler = (*Service)(nil) _ 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{ return &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider)), im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider)),
features: features, features: features,
@ -88,7 +88,7 @@ func parseQueryModel(raw json.RawMessage) (*QueryJSONModel, error) {
return model, err 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) { return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
opts, err := settings.HTTPClientOptions(ctx) opts, err := settings.HTTPClientOptions(ctx)
if err != nil { if err != nil {