-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
client.go
80 lines (66 loc) · 2.45 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
}