-
Notifications
You must be signed in to change notification settings - Fork 24
/
client.go
98 lines (84 loc) · 2.88 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package prometheus
import (
"errors"
"net/http"
"strings"
promapi "github.com/prometheus/client_golang/api"
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"go.uber.org/fx"
"golang.org/x/oauth2"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
commonhttp "github.com/fluxninja/aperture/v2/pkg/net/http"
promconfig "github.com/fluxninja/aperture/v2/pkg/prometheus/config"
)
var (
// swagger:operation POST /prometheus common-configuration Prometheus
// ---
// x-fn-config-env: true
// parameters:
// - in: body
// schema:
// $ref: "#/definitions/PrometheusConfig"
// - name: http_client
// in: body
// schema:
// "$ref": "#/definitions/HTTPClientConfig"
// PrometheusConfigKey is the key used to store the PrometheusConfig in the config.
prometheusConfigKey = "prometheus"
// HttpConfigKey is the key used to store the HTTPClientConfig in the config.
httpConfigKey = strings.Join([]string{prometheusConfigKey, "http_client"}, ".")
)
// Module provides a singleton pointer to prometheusv1.API via FX.
func Module() fx.Option {
return fx.Options(
fx.Provide(providePrometheusClient),
fx.Provide(providePrometheusEnforcer),
commonhttp.ClientConstructor{Name: "prometheus.http-client", ConfigKey: httpConfigKey}.Annotate(),
)
}
// ConfigOverride can be provided by an extension to control client creation behavior.
type ConfigOverride struct {
SkipClientCreation bool
}
// ClientIn holds fields, parameters, to provide Prometheus Client.
type ClientIn struct {
fx.In
HTTPClient *http.Client `name:"prometheus.http-client"`
TokenSource oauth2.TokenSource `optional:"true"`
Unmarshaller config.Unmarshaller
ConfigOverride *ConfigOverride `optional:"true"`
}
func providePrometheusClient(in ClientIn) (prometheusv1.API, promapi.Client, error) {
// Skipping creation of prometheus client if Aperture Cloud Controller is enabled for Aperture Agent
if in.ConfigOverride != nil && in.ConfigOverride.SkipClientCreation {
return nil, nil, nil
}
var config promconfig.PrometheusConfig
if err := in.Unmarshaller.UnmarshalKey(prometheusConfigKey, &config); err != nil {
log.Error().Err(err).Msg("unable to deserialize")
return nil, nil, err
}
if config.Address == "" {
err := errors.New("prometheus address not specified")
log.Error().Err(err).Msg("")
return nil, nil, err
}
if in.TokenSource != nil {
log.Info().Msg("Using Google TokenSource for prometheus API queries")
oauth2Transport := &oauth2.Transport{
Source: in.TokenSource,
Base: in.HTTPClient.Transport,
}
in.HTTPClient.Transport = oauth2Transport
}
client, err := promapi.NewClient(promapi.Config{
Address: config.Address,
RoundTripper: in.HTTPClient.Transport,
})
if err != nil {
log.Error().Err(err).Msg("Error creating client")
return nil, nil, err
}
return prometheusv1.NewAPI(client), client, nil
}