From a197508d09e092375b5106e81ef17ac75ebeb2e2 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Tue, 21 Jul 2020 23:24:43 +0200 Subject: [PATCH 1/2] Add docker swarm test Signed-off-by: Julien Pivotto --- config/config_test.go | 22 ++++++++++++++++++++++ config/testdata/conf.good.yml | 5 +++++ discovery/dockerswarm/dockerswarm.go | 19 +++++++------------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index aa3e0b6e8..eca06556c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -31,6 +31,7 @@ import ( sd_config "github.com/prometheus/prometheus/discovery/config" "github.com/prometheus/prometheus/discovery/consul" "github.com/prometheus/prometheus/discovery/dns" + "github.com/prometheus/prometheus/discovery/dockerswarm" "github.com/prometheus/prometheus/discovery/ec2" "github.com/prometheus/prometheus/discovery/file" "github.com/prometheus/prometheus/discovery/kubernetes" @@ -605,6 +606,27 @@ var expectedConf = &Config{ }, }, }, + { + JobName: "dockerswarm", + + HonorTimestamps: true, + ScrapeInterval: model.Duration(15 * time.Second), + ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout, + + MetricsPath: DefaultScrapeConfig.MetricsPath, + Scheme: DefaultScrapeConfig.Scheme, + + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{ + DockerSwarmSDConfigs: []*dockerswarm.SDConfig{ + { + Host: "http://127.0.0.1:2375", + Role: "nodes", + Port: 80, + RefreshInterval: model.Duration(60 * time.Second), + }, + }, + }, + }, { JobName: "service-openstack", diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index 40968fc74..5571666c9 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -259,6 +259,11 @@ scrape_configs: cert_file: valid_cert_file key_file: valid_key_file +- job_name: dockerswarm + dockerswarm_sd_configs: + - host: http://127.0.0.1:2375 + role: nodes + - job_name: service-openstack openstack_sd_configs: - role: instance diff --git a/discovery/dockerswarm/dockerswarm.go b/discovery/dockerswarm/dockerswarm.go index 0464f032f..ef98890ff 100644 --- a/discovery/dockerswarm/dockerswarm.go +++ b/discovery/dockerswarm/dockerswarm.go @@ -44,7 +44,6 @@ type SDConfig struct { HTTPClientConfig config_util.HTTPClientConfig `yaml:",inline"` Host string `yaml:"host"` - url *url.URL Role string `yaml:"role"` Port int `yaml:"port"` @@ -62,11 +61,9 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { if c.Host == "" { return fmt.Errorf("host missing") } - url, err := url.Parse(c.Host) - if err != nil { + if _, err = url.Parse(c.Host); err != nil { return err } - c.url = url switch c.Role { case "services", "nodes", "tasks": case "": @@ -89,17 +86,15 @@ type Discovery struct { // NewDiscovery returns a new Discovery which periodically refreshes its targets. func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { var err error + d := &Discovery{ port: conf.Port, role: conf.Role, } - // This is used in tests. In normal situations, it is set when Unmarshaling. - if conf.url == nil { - conf.url, err = url.Parse(conf.Host) - if err != nil { - return nil, err - } + hostURL, err := url.Parse(conf.Host) + if err != nil { + return nil, err } opts := []client.Opt{ @@ -110,7 +105,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { // There are other protocols than HTTP supported by the Docker daemon, like // unix, which are not supported by the HTTP client. Passing HTTP client // options to the Docker client makes those non-HTTP requests fail. - if conf.url.Scheme == "http" || conf.url.Scheme == "https" { + if hostURL.Scheme == "http" || hostURL.Scheme == "https" { rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false) if err != nil { return nil, err @@ -120,7 +115,7 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) { Transport: rt, Timeout: time.Duration(conf.RefreshInterval), }), - client.WithScheme(conf.url.Scheme), + client.WithScheme(hostURL.Scheme), ) } From f8ec72d73021bed281f1ae01f56f73a707b31b9f Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Tue, 21 Jul 2020 23:28:43 +0200 Subject: [PATCH 2/2] Add digitalocean test Signed-off-by: Julien Pivotto --- config/config_test.go | 25 ++++++++++++++++++++++++- config/testdata/conf.good.yml | 4 ++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index eca06556c..e485653b4 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -30,6 +30,7 @@ import ( "github.com/prometheus/prometheus/discovery/azure" sd_config "github.com/prometheus/prometheus/discovery/config" "github.com/prometheus/prometheus/discovery/consul" + "github.com/prometheus/prometheus/discovery/digitalocean" "github.com/prometheus/prometheus/discovery/dns" "github.com/prometheus/prometheus/discovery/dockerswarm" "github.com/prometheus/prometheus/discovery/ec2" @@ -606,6 +607,28 @@ var expectedConf = &Config{ }, }, }, + { + JobName: "digitalocean-droplets", + + HonorTimestamps: true, + ScrapeInterval: model.Duration(15 * time.Second), + ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout, + + MetricsPath: DefaultScrapeConfig.MetricsPath, + Scheme: DefaultScrapeConfig.Scheme, + + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{ + DigitalOceanSDConfigs: []*digitalocean.SDConfig{ + { + HTTPClientConfig: config_util.HTTPClientConfig{ + BearerToken: "abcdef", + }, + Port: 80, + RefreshInterval: model.Duration(60 * time.Second), + }, + }, + }, + }, { JobName: "dockerswarm", @@ -713,7 +736,7 @@ func TestElideSecrets(t *testing.T) { yamlConfig := string(config) matches := secretRe.FindAllStringIndex(yamlConfig, -1) - testutil.Assert(t, len(matches) == 7, "wrong number of secret matches found") + testutil.Assert(t, len(matches) == 8, "wrong number of secret matches found") testutil.Assert(t, !strings.Contains(yamlConfig, "mysecret"), "yaml marshal reveals authentication credentials.") } diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index 5571666c9..7d0b441ef 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -259,6 +259,10 @@ scrape_configs: cert_file: valid_cert_file key_file: valid_key_file +- job_name: digitalocean-droplets + digitalocean_sd_configs: + - bearer_token: abcdef + - job_name: dockerswarm dockerswarm_sd_configs: - host: http://127.0.0.1:2375