2021-06-25 10:20:18 +00:00
|
|
|
package dockerswarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2021-06-25 10:20:18 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SDCheckInterval defines interval for dockerswarm targets refresh.
|
|
|
|
var SDCheckInterval = 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. "+
|
2024-04-18 00:27:47 +00:00
|
|
|
"See https://docs.victoriametrics.com/sd_configs/#dockerswarm_sd_configs for details")
|
2021-06-25 10:20:18 +00:00
|
|
|
|
|
|
|
// SDConfig represents docker swarm service discovery configuration
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#dockerswarm_sd_config
|
|
|
|
type SDConfig struct {
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
Role string `yaml:"role"`
|
|
|
|
Port int `yaml:"port,omitempty"`
|
|
|
|
Filters []Filter `yaml:"filters,omitempty"`
|
|
|
|
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2021-10-26 18:21:08 +00:00
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
2021-06-25 10:20:18 +00:00
|
|
|
ProxyClientConfig promauth.ProxyClientConfig `yaml:",inline"`
|
|
|
|
// refresh_interval is obtained from `-promscrape.dockerswarmSDCheckInterval` command-line option
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter is a filter, which can be passed to SDConfig.
|
|
|
|
type Filter struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Values []string `yaml:"values"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels returns dockerswarm labels according to sdc.
|
2022-11-30 05:22:12 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]*promutils.Labels, error) {
|
2021-06-25 10:20:18 +00:00
|
|
|
cfg, err := getAPIConfig(sdc, baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
|
|
|
}
|
|
|
|
switch sdc.Role {
|
2023-01-12 06:54:16 +00:00
|
|
|
case "tasks":
|
2021-06-25 10:20:18 +00:00
|
|
|
return getTasksLabels(cfg)
|
2023-01-12 06:54:16 +00:00
|
|
|
case "services":
|
2021-06-25 10:20:18 +00:00
|
|
|
return getServicesLabels(cfg)
|
2023-01-12 06:54:16 +00:00
|
|
|
case "nodes":
|
2021-06-25 10:20:18 +00:00
|
|
|
return getNodesLabels(cfg)
|
|
|
|
default:
|
2023-10-25 21:19:33 +00:00
|
|
|
return nil, fmt.Errorf("skipping unexpected role=%q; must be one of `tasks`, `services` or `nodes`", sdc.Role)
|
2021-06-25 10:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
2023-07-27 21:47:53 +00:00
|
|
|
v := configMap.Delete(sdc)
|
|
|
|
if v != nil {
|
|
|
|
cfg := v.(*apiConfig)
|
|
|
|
cfg.client.Stop()
|
|
|
|
}
|
2021-06-25 10:20:18 +00:00
|
|
|
}
|