2021-06-25 10:42:47 +02:00
|
|
|
package docker
|
2020-10-12 12:38:21 +02:00
|
|
|
|
|
|
|
import (
|
2021-06-25 11:10:20 +02:00
|
|
|
"flag"
|
2020-10-12 12:38:21 +02:00
|
|
|
"fmt"
|
2021-06-25 11:10:20 +02:00
|
|
|
"time"
|
2020-10-12 12:38:21 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 09:56:10 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-10-12 12:38:21 +02:00
|
|
|
)
|
|
|
|
|
2021-06-25 11:10:20 +02:00
|
|
|
// SDCheckInterval defines interval for docker targets refresh.
|
|
|
|
var SDCheckInterval = flag.Duration("promscrape.dockerSDCheckInterval", 30*time.Second, "Interval for checking for changes in docker. "+
|
|
|
|
"This works only if docker_sd_configs is configured in '-promscrape.config' file. "+
|
|
|
|
"See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#docker_sd_config for details")
|
|
|
|
|
|
|
|
// SwarmSDCheckInterval defines interval for dockerswarm targets refresh.
|
|
|
|
var SwarmSDCheckInterval = flag.Duration("promscrape.dockerswarmSDCheckInterval", 30*time.Second, "Interval for checking for changes in dockerswarm. "+
|
|
|
|
"This works only if dockerswarm_sd_configs is configured in '-promscrape.config' file. "+
|
|
|
|
"See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#dockerswarm_sd_config for details")
|
|
|
|
|
2021-06-25 10:42:47 +02:00
|
|
|
// DockerSDConfig defines the `docker_sd` section for Docker based discovery
|
2020-10-12 12:38:21 +02:00
|
|
|
//
|
2021-06-25 10:42:47 +02:00
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#docker_sd_config
|
|
|
|
type DockerSDConfig struct {
|
2020-11-23 15:26:52 +01:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
Port int `yaml:"port,omitempty"`
|
|
|
|
Filters []Filter `yaml:"filters,omitempty"`
|
|
|
|
|
2021-04-03 23:40:08 +02:00
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
|
|
|
ProxyURL proxy.URL `yaml:"proxy_url,omitempty"`
|
|
|
|
ProxyClientConfig promauth.ProxyClientConfig `yaml:",inline"`
|
2021-06-25 10:42:47 +02:00
|
|
|
// refresh_interval is obtained from `-promscrape.dockerSDCheckInterval` command-line option
|
2020-10-12 12:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:26:52 +01:00
|
|
|
// Filter is a filter, which can be passed to SDConfig.
|
|
|
|
type Filter struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Values []string `yaml:"values"`
|
|
|
|
}
|
|
|
|
|
2021-06-25 10:42:47 +02:00
|
|
|
// GetLabels returns docker labels according to sdc.
|
|
|
|
func (sdc *DockerSDConfig) GetLabels(baseDir string) ([]map[string]string, error) {
|
|
|
|
cfg, err := getAPIConfigFromDockerSDConfig(sdc, baseDir)
|
2020-10-12 12:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
|
|
|
}
|
2021-06-25 10:42:47 +02:00
|
|
|
return getContainersLabels(cfg)
|
2020-10-12 12:38:21 +02:00
|
|
|
}
|
2021-03-01 13:13:56 +01:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
2021-06-25 10:42:47 +02:00
|
|
|
func (sdc *DockerSDConfig) MustStop() {
|
2021-03-01 13:13:56 +01:00
|
|
|
configMap.Delete(sdc)
|
|
|
|
}
|