grafana/pkg/tsdb/prometheus/client/transport.go

65 lines
2.0 KiB
Go

package client
import (
"context"
"fmt"
"strings"
"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"
"github.com/grafana/grafana/pkg/util/maputil"
)
// 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) {
opts, err := settings.HTTPClientOptions(ctx)
if err != nil {
return nil, fmt.Errorf("error getting HTTP options: %w", 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"
}
// Set Azure authentication
if cfg.AzureAuthEnabled {
err = azureauth.ConfigureAzureAuthentication(settings, cfg.Azure, &opts)
if err != nil {
return nil, fmt.Errorf("error configuring Azure auth: %v", err)
}
}
return &opts, 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
}