Files
grafana/pkg/tsdb/prometheus/buffered/client.go
Andrej Ocenas 3df34fe064 Prometheus: Remove cache, pass headers in request, simplify client creation for resource calls and custom client (#51436)
* Remove cache, pass headers in request, simplify client creation

* Add test for http options creation
2022-07-04 11:18:45 +02:00

81 lines
2.5 KiB
Go

package buffered
import (
"fmt"
"net/http"
"strings"
"github.com/grafana/grafana-azure-sdk-go/azsettings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/tsdb/prometheus/buffered/azureauth"
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
"github.com/grafana/grafana/pkg/util/maputil"
"github.com/prometheus/client_golang/api"
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
)
// CreateTransportOptions creates options for the http client. Probably should be shared and should not live in the
// buffered package.
func CreateTransportOptions(settings backend.DataSourceInstanceSettings, azureSettings *azsettings.AzureSettings, features featuremgmt.FeatureToggles, logger log.Logger) (*sdkhttpclient.Options, error) {
opts, err := settings.HTTPClientOptions()
if err != nil {
return nil, err
}
jsonData, err := utils.GetJsonData(settings)
if err != nil {
return nil, fmt.Errorf("error reading settings: %w", err)
}
httpMethod, _ := maputil.GetStringOptional(jsonData, "httpMethod")
opts.Middlewares = middlewares(logger, httpMethod)
// Set SigV4 service namespace
if opts.SigV4 != nil {
opts.SigV4.Service = "aps"
}
// Azure authentication is experimental (#35857)
if features.IsEnabled(featuremgmt.FlagPrometheusAzureAuth) {
err = azureauth.ConfigureAzureAuthentication(settings, azureSettings, &opts)
if err != nil {
return nil, fmt.Errorf("error configuring Azure auth: %v", err)
}
}
return &opts, nil
}
func CreateClient(roundTripper http.RoundTripper, url string) (apiv1.API, error) {
cfg := api.Config{
Address: url,
RoundTripper: roundTripper,
}
client, err := api.NewClient(cfg)
if err != nil {
return nil, err
}
return apiv1.NewAPI(client), nil
}
func middlewares(logger log.Logger, httpMethod string) []sdkhttpclient.Middleware {
middlewares := []sdkhttpclient.Middleware{
// TODO: probably isn't needed anymore and should by done by http infra code
middleware.CustomQueryParameters(logger),
sdkhttpclient.CustomHeadersMiddleware(),
}
// Needed to control GET vs POST method of the requests
if strings.ToLower(httpMethod) == "get" {
middlewares = append(middlewares, middleware.ForceHttpGet(logger))
}
return middlewares
}