mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
5.2 KiB
191 lines
5.2 KiB
4 years ago
|
// Copyright 2020 The Prometheus Authors
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
4 years ago
|
package moby
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"github.com/docker/docker/api/types/filters"
|
||
4 years ago
|
"github.com/docker/docker/client"
|
||
|
"github.com/go-kit/kit/log"
|
||
4 years ago
|
"github.com/prometheus/common/config"
|
||
4 years ago
|
"github.com/prometheus/common/model"
|
||
4 years ago
|
"github.com/prometheus/common/version"
|
||
4 years ago
|
|
||
4 years ago
|
"github.com/prometheus/prometheus/discovery"
|
||
4 years ago
|
"github.com/prometheus/prometheus/discovery/refresh"
|
||
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
swarmLabel = model.MetaLabelPrefix + "dockerswarm_"
|
||
|
)
|
||
|
|
||
4 years ago
|
var userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
|
||
|
|
||
4 years ago
|
// DefaultDockerSwarmSDConfig is the default Docker Swarm SD configuration.
|
||
|
var DefaultDockerSwarmSDConfig = DockerSwarmSDConfig{
|
||
4 years ago
|
RefreshInterval: model.Duration(60 * time.Second),
|
||
|
Port: 80,
|
||
|
Filters: []Filter{},
|
||
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func init() {
|
||
4 years ago
|
discovery.RegisterConfig(&DockerSwarmSDConfig{})
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// DockerSwarmSDConfig is the configuration for Docker Swarm based service discovery.
|
||
|
type DockerSwarmSDConfig struct {
|
||
4 years ago
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
||
4 years ago
|
|
||
4 years ago
|
Host string `yaml:"host"`
|
||
|
Role string `yaml:"role"`
|
||
|
Port int `yaml:"port"`
|
||
|
Filters []Filter `yaml:"filters"`
|
||
4 years ago
|
|
||
|
RefreshInterval model.Duration `yaml:"refresh_interval"`
|
||
|
}
|
||
|
|
||
4 years ago
|
// Filter represent a filter that can be passed to Docker Swarm to reduce the
|
||
|
// amount of data received.
|
||
|
type Filter struct {
|
||
|
Name string `yaml:"name"`
|
||
|
Values []string `yaml:"values"`
|
||
|
}
|
||
|
|
||
4 years ago
|
// Name returns the name of the Config.
|
||
4 years ago
|
func (*DockerSwarmSDConfig) Name() string { return "dockerswarm" }
|
||
4 years ago
|
|
||
|
// NewDiscoverer returns a Discoverer for the Config.
|
||
4 years ago
|
func (c *DockerSwarmSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
||
4 years ago
|
return NewDiscovery(c, opts.Logger)
|
||
|
}
|
||
|
|
||
|
// SetDirectory joins any relative file paths with dir.
|
||
4 years ago
|
func (c *DockerSwarmSDConfig) SetDirectory(dir string) {
|
||
4 years ago
|
c.HTTPClientConfig.SetDirectory(dir)
|
||
|
}
|
||
|
|
||
4 years ago
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||
4 years ago
|
func (c *DockerSwarmSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||
|
*c = DefaultDockerSwarmSDConfig
|
||
|
type plain DockerSwarmSDConfig
|
||
4 years ago
|
err := unmarshal((*plain)(c))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if c.Host == "" {
|
||
|
return fmt.Errorf("host missing")
|
||
|
}
|
||
4 years ago
|
if _, err = url.Parse(c.Host); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
switch c.Role {
|
||
|
case "services", "nodes", "tasks":
|
||
|
case "":
|
||
|
return fmt.Errorf("role missing (one of: tasks, services, nodes)")
|
||
|
default:
|
||
|
return fmt.Errorf("invalid role %s, expected tasks, services, or nodes", c.Role)
|
||
|
}
|
||
4 years ago
|
return c.HTTPClientConfig.Validate()
|
||
4 years ago
|
}
|
||
|
|
||
|
// Discovery periodically performs Docker Swarm requests. It implements
|
||
|
// the Discoverer interface.
|
||
|
type Discovery struct {
|
||
|
*refresh.Discovery
|
||
4 years ago
|
client *client.Client
|
||
|
role string
|
||
|
port int
|
||
|
filters filters.Args
|
||
4 years ago
|
}
|
||
|
|
||
|
// NewDiscovery returns a new Discovery which periodically refreshes its targets.
|
||
4 years ago
|
func NewDiscovery(conf *DockerSwarmSDConfig, logger log.Logger) (*Discovery, error) {
|
||
4 years ago
|
var err error
|
||
4 years ago
|
|
||
4 years ago
|
d := &Discovery{
|
||
|
port: conf.Port,
|
||
4 years ago
|
role: conf.Role,
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
hostURL, err := url.Parse(conf.Host)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
opts := []client.Opt{
|
||
4 years ago
|
client.WithHost(conf.Host),
|
||
|
client.WithAPIVersionNegotiation(),
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
d.filters = filters.NewArgs()
|
||
|
for _, f := range conf.Filters {
|
||
|
for _, v := range f.Values {
|
||
|
d.filters.Add(f.Name, v)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
// There are other protocols than HTTP supported by the Docker daemon, like
|
||
4 years ago
|
// unix, which are not supported by the HTTP client. Passing HTTP client
|
||
|
// options to the Docker client makes those non-HTTP requests fail.
|
||
4 years ago
|
if hostURL.Scheme == "http" || hostURL.Scheme == "https" {
|
||
4 years ago
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false, false)
|
||
4 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
opts = append(opts,
|
||
|
client.WithHTTPClient(&http.Client{
|
||
|
Transport: rt,
|
||
|
Timeout: time.Duration(conf.RefreshInterval),
|
||
|
}),
|
||
4 years ago
|
client.WithScheme(hostURL.Scheme),
|
||
4 years ago
|
client.WithHTTPHeaders(map[string]string{
|
||
|
"User-Agent": userAgent,
|
||
|
}),
|
||
4 years ago
|
)
|
||
|
}
|
||
|
|
||
|
d.client, err = client.NewClientWithOpts(opts...)
|
||
4 years ago
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error setting up docker swarm client: %w", err)
|
||
|
}
|
||
|
|
||
|
d.Discovery = refresh.NewDiscovery(
|
||
|
logger,
|
||
|
"dockerswarm",
|
||
|
time.Duration(conf.RefreshInterval),
|
||
4 years ago
|
d.refresh,
|
||
4 years ago
|
)
|
||
|
return d, nil
|
||
|
}
|
||
4 years ago
|
|
||
|
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
||
|
switch d.role {
|
||
|
case "services":
|
||
|
return d.refreshServices(ctx)
|
||
|
case "nodes":
|
||
|
return d.refreshNodes(ctx)
|
||
|
case "tasks":
|
||
|
return d.refreshTasks(ctx)
|
||
|
default:
|
||
|
panic(fmt.Errorf("unexpected role %s", d.role))
|
||
|
}
|
||
|
}
|