Skip to content

Commit

Permalink
Moved http configurations packages around [Refactor] (#4715)
Browse files Browse the repository at this point in the history
* Moved http configurations around.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Moved HTTP configurations around.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Oct 1, 2021
1 parent 35b88ab commit 6b296fd
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 87 deletions.
33 changes: 16 additions & 17 deletions cmd/thanos/rule.go
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/prometheus/prometheus/util/strutil"
"github.com/thanos-io/thanos/pkg/errutil"
"github.com/thanos-io/thanos/pkg/extkingpin"
"github.com/thanos-io/thanos/pkg/httpconfig"

extflag "github.com/efficientgo/tools/extkingpin"
"github.com/thanos-io/thanos/pkg/alert"
Expand All @@ -43,12 +44,10 @@ import (
"github.com/thanos-io/thanos/pkg/discovery/dns"
"github.com/thanos-io/thanos/pkg/extprom"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
http_util "github.com/thanos-io/thanos/pkg/http"
"github.com/thanos-io/thanos/pkg/logging"
"github.com/thanos-io/thanos/pkg/objstore/client"
"github.com/thanos-io/thanos/pkg/prober"
"github.com/thanos-io/thanos/pkg/promclient"
"github.com/thanos-io/thanos/pkg/query"
thanosrules "github.com/thanos-io/thanos/pkg/rules"
"github.com/thanos-io/thanos/pkg/runutil"
grpcserver "github.com/thanos-io/thanos/pkg/server/grpc"
Expand Down Expand Up @@ -266,29 +265,29 @@ func runRule(
) error {
metrics := newRuleMetrics(reg)

var queryCfg []query.Config
var queryCfg []httpconfig.Config
var err error
if len(conf.queryConfigYAML) > 0 {
queryCfg, err = query.LoadConfigs(conf.queryConfigYAML)
queryCfg, err = httpconfig.LoadConfigs(conf.queryConfigYAML)
if err != nil {
return err
}
} else {
queryCfg, err = query.BuildQueryConfig(conf.query.addrs)
queryCfg, err = httpconfig.BuildConfig(conf.query.addrs)
if err != nil {
return err
return errors.Wrap(err, "query configuration")
}

// Build the query configuration from the legacy query flags.
var fileSDConfigs []http_util.FileSDConfig
var fileSDConfigs []httpconfig.FileSDConfig
if len(conf.query.sdFiles) > 0 {
fileSDConfigs = append(fileSDConfigs, http_util.FileSDConfig{
fileSDConfigs = append(fileSDConfigs, httpconfig.FileSDConfig{
Files: conf.query.sdFiles,
RefreshInterval: model.Duration(conf.query.sdInterval),
})
queryCfg = append(queryCfg,
query.Config{
EndpointsConfig: http_util.EndpointsConfig{
httpconfig.Config{
EndpointsConfig: httpconfig.EndpointsConfig{
Scheme: "http",
FileSDConfigs: fileSDConfigs,
},
Expand All @@ -302,16 +301,16 @@ func runRule(
extprom.WrapRegistererWithPrefix("thanos_rule_query_apis_", reg),
dns.ResolverType(conf.query.dnsSDResolver),
)
var queryClients []*http_util.Client
var queryClients []*httpconfig.Client
queryClientMetrics := extpromhttp.NewClientMetrics(extprom.WrapRegistererWith(prometheus.Labels{"client": "query"}, reg))
for _, cfg := range queryCfg {
cfg.HTTPClientConfig.ClientMetrics = queryClientMetrics
c, err := http_util.NewHTTPClient(cfg.HTTPClientConfig, "query")
c, err := httpconfig.NewHTTPClient(cfg.HTTPClientConfig, "query")
if err != nil {
return err
}
c.Transport = tracing.HTTPTripperware(logger, c.Transport)
queryClient, err := http_util.NewClient(logger, cfg.EndpointsConfig, c, queryProvider.Clone())
queryClient, err := httpconfig.NewClient(logger, cfg.EndpointsConfig, c, queryProvider.Clone())
if err != nil {
return err
}
Expand Down Expand Up @@ -381,13 +380,13 @@ func runRule(
)
for _, cfg := range alertingCfg.Alertmanagers {
cfg.HTTPClientConfig.ClientMetrics = amClientMetrics
c, err := http_util.NewHTTPClient(cfg.HTTPClientConfig, "alertmanager")
c, err := httpconfig.NewHTTPClient(cfg.HTTPClientConfig, "alertmanager")
if err != nil {
return err
}
c.Transport = tracing.HTTPTripperware(logger, c.Transport)
// Each Alertmanager client has a different list of targets thus each needs its own DNS provider.
amClient, err := http_util.NewClient(logger, cfg.EndpointsConfig, c, amProvider.Clone())
amClient, err := httpconfig.NewClient(logger, cfg.EndpointsConfig, c, amProvider.Clone())
if err != nil {
return err
}
Expand Down Expand Up @@ -706,7 +705,7 @@ func removeDuplicateQueryEndpoints(logger log.Logger, duplicatedQueriers prometh

func queryFuncCreator(
logger log.Logger,
queriers []*http_util.Client,
queriers []*httpconfig.Client,
duplicatedQuery prometheus.Counter,
ruleEvalWarnings *prometheus.CounterVec,
httpMethod string,
Expand Down Expand Up @@ -762,7 +761,7 @@ func queryFuncCreator(
}
}

func addDiscoveryGroups(g *run.Group, c *http_util.Client, interval time.Duration) {
func addDiscoveryGroups(g *run.Group, c *httpconfig.Client, interval time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
c.Discover(ctx)
Expand Down
4 changes: 2 additions & 2 deletions cmd/thanos/sidecar.go
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/thanos-io/thanos/pkg/exthttp"
"github.com/thanos-io/thanos/pkg/extkingpin"
"github.com/thanos-io/thanos/pkg/extprom"
thanoshttp "github.com/thanos-io/thanos/pkg/http"
"github.com/thanos-io/thanos/pkg/httpconfig"
"github.com/thanos-io/thanos/pkg/logging"
meta "github.com/thanos-io/thanos/pkg/metadata"
thanosmodel "github.com/thanos-io/thanos/pkg/model"
Expand Down Expand Up @@ -228,7 +228,7 @@ func runSidecar(
t := exthttp.NewTransport()
t.MaxIdleConnsPerHost = conf.connection.maxIdleConnsPerHost
t.MaxIdleConns = conf.connection.maxIdleConns
c := promclient.NewClient(&http.Client{Transport: tracing.HTTPTripperware(logger, t)}, logger, thanoshttp.ThanosUserAgent)
c := promclient.NewClient(&http.Client{Transport: tracing.HTTPTripperware(logger, t)}, logger, httpconfig.ThanosUserAgent)

promStore, err := store.NewPrometheusStore(logger, reg, c, conf.prometheus.url, component.Sidecar, m.Labels, m.Timestamps, m.Version)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions pkg/alert/config.go
Expand Up @@ -13,10 +13,10 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/relabel"
"github.com/thanos-io/thanos/pkg/httpconfig"
"gopkg.in/yaml.v2"

"github.com/thanos-io/thanos/pkg/discovery/dns"
http_util "github.com/thanos-io/thanos/pkg/http"
)

type AlertingConfig struct {
Expand All @@ -25,10 +25,10 @@ type AlertingConfig struct {

// AlertmanagerConfig represents a client to a cluster of Alertmanager endpoints.
type AlertmanagerConfig struct {
HTTPClientConfig http_util.ClientConfig `yaml:"http_config"`
EndpointsConfig http_util.EndpointsConfig `yaml:",inline"`
Timeout model.Duration `yaml:"timeout"`
APIVersion APIVersion `yaml:"api_version"`
HTTPClientConfig httpconfig.ClientConfig `yaml:"http_config"`
EndpointsConfig httpconfig.EndpointsConfig `yaml:",inline"`
Timeout model.Duration `yaml:"timeout"`
APIVersion APIVersion `yaml:"api_version"`
}

// APIVersion represents the API version of the Alertmanager endpoint.
Expand Down Expand Up @@ -61,10 +61,10 @@ func (v *APIVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {

func DefaultAlertmanagerConfig() AlertmanagerConfig {
return AlertmanagerConfig{
EndpointsConfig: http_util.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
Scheme: "http",
StaticAddresses: []string{},
FileSDConfigs: []http_util.FileSDConfig{},
FileSDConfigs: []httpconfig.FileSDConfig{},
},
Timeout: model.Duration(time.Second * 10),
APIVersion: APIv1,
Expand Down Expand Up @@ -111,18 +111,18 @@ func BuildAlertmanagerConfig(address string, timeout time.Duration) (Alertmanage
break
}
}
var basicAuth http_util.BasicAuth
var basicAuth httpconfig.BasicAuth
if parsed.User != nil && parsed.User.String() != "" {
basicAuth.Username = parsed.User.Username()
pw, _ := parsed.User.Password()
basicAuth.Password = pw
}

return AlertmanagerConfig{
HTTPClientConfig: http_util.ClientConfig{
HTTPClientConfig: httpconfig.ClientConfig{
BasicAuth: basicAuth,
},
EndpointsConfig: http_util.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
PathPrefix: parsed.Path,
Scheme: scheme,
StaticAddresses: []string{host},
Expand Down
20 changes: 10 additions & 10 deletions pkg/alert/config_test.go
Expand Up @@ -9,7 +9,7 @@ import (

"gopkg.in/yaml.v2"

"github.com/thanos-io/thanos/pkg/http"
"github.com/thanos-io/thanos/pkg/httpconfig"
"github.com/thanos-io/thanos/pkg/testutil"
)

Expand Down Expand Up @@ -54,7 +54,7 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "http://localhost:9093",
expected: AlertmanagerConfig{
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"localhost:9093"},
Scheme: "http",
},
Expand All @@ -64,7 +64,7 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "https://am.example.com",
expected: AlertmanagerConfig{
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"am.example.com"},
Scheme: "https",
},
Expand All @@ -74,7 +74,7 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "dns+http://localhost:9093",
expected: AlertmanagerConfig{
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"dns+localhost:9093"},
Scheme: "http",
},
Expand All @@ -84,7 +84,7 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "dnssrv+http://localhost",
expected: AlertmanagerConfig{
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"dnssrv+localhost"},
Scheme: "http",
},
Expand All @@ -94,7 +94,7 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "ssh+http://localhost",
expected: AlertmanagerConfig{
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"localhost"},
Scheme: "ssh+http",
},
Expand All @@ -104,7 +104,7 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "dns+https://localhost/path/prefix/",
expected: AlertmanagerConfig{
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"dns+localhost:9093"},
Scheme: "https",
PathPrefix: "/path/prefix/",
Expand All @@ -115,13 +115,13 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
{
address: "http://user:pass@localhost:9093",
expected: AlertmanagerConfig{
HTTPClientConfig: http.ClientConfig{
BasicAuth: http.BasicAuth{
HTTPClientConfig: httpconfig.ClientConfig{
BasicAuth: httpconfig.BasicAuth{
Username: "user",
Password: "pass",
},
},
EndpointsConfig: http.EndpointsConfig{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{"localhost:9093"},
Scheme: "http",
},
Expand Down
26 changes: 13 additions & 13 deletions pkg/query/config.go → pkg/httpconfig/config.go
@@ -1,7 +1,7 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package query
package httpconfig

import (
"fmt"
Expand All @@ -11,20 +11,20 @@ import (
"gopkg.in/yaml.v2"

"github.com/pkg/errors"
http_util "github.com/thanos-io/thanos/pkg/http"
)

// Config is a structure that allows pointing to various HTTP endpoint, e.g ruler connecting to queriers.
type Config struct {
HTTPClientConfig http_util.ClientConfig `yaml:"http_config"`
EndpointsConfig http_util.EndpointsConfig `yaml:",inline"`
HTTPClientConfig ClientConfig `yaml:"http_config"`
EndpointsConfig EndpointsConfig `yaml:",inline"`
}

func DefaultConfig() Config {
return Config{
EndpointsConfig: http_util.EndpointsConfig{
EndpointsConfig: EndpointsConfig{
Scheme: "http",
StaticAddresses: []string{},
FileSDConfigs: []http_util.FileSDConfig{},
FileSDConfigs: []FileSDConfig{},
},
}
}
Expand All @@ -45,12 +45,12 @@ func LoadConfigs(confYAML []byte) ([]Config, error) {
return queryCfg, nil
}

// BuildQueryConfig returns a query client configuration from a static address.
func BuildQueryConfig(queryAddrs []string) ([]Config, error) {
configs := make([]Config, 0, len(queryAddrs))
for i, addr := range queryAddrs {
// BuildConfig returns a configuration from a static addresses.
func BuildConfig(addrs []string) ([]Config, error) {
configs := make([]Config, 0, len(addrs))
for i, addr := range addrs {
if addr == "" {
return nil, errors.Errorf("static querier address cannot be empty at index %d", i)
return nil, errors.Errorf("static address cannot be empty at index %d", i)
}
// If addr is missing schema, add http.
if !strings.Contains(addr, "://") {
Expand All @@ -61,10 +61,10 @@ func BuildQueryConfig(queryAddrs []string) ([]Config, error) {
return nil, errors.Wrapf(err, "failed to parse addr %q", addr)
}
if u.Scheme != "http" && u.Scheme != "https" {
return nil, errors.Errorf("%q is not supported scheme for querier address", u.Scheme)
return nil, errors.Errorf("%q is not supported scheme for address", u.Scheme)
}
configs = append(configs, Config{
EndpointsConfig: http_util.EndpointsConfig{
EndpointsConfig: EndpointsConfig{
Scheme: u.Scheme,
StaticAddresses: []string{u.Host},
PathPrefix: u.Path,
Expand Down

0 comments on commit 6b296fd

Please sign in to comment.