2017-11-25 05:13:54 -08:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-09-01 00:51:31 -07:00
|
|
|
"reflect"
|
2018-01-17 10:12:58 -08:00
|
|
|
"sync"
|
2018-01-26 05:57:33 -08:00
|
|
|
"time"
|
2017-11-25 05:13:54 -08:00
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2018-09-01 00:51:31 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-11-25 05:13:54 -08:00
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
sd_config "github.com/prometheus/prometheus/discovery/config"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-11-25 05:13:54 -08:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery/azure"
|
|
|
|
"github.com/prometheus/prometheus/discovery/consul"
|
|
|
|
"github.com/prometheus/prometheus/discovery/dns"
|
|
|
|
"github.com/prometheus/prometheus/discovery/ec2"
|
|
|
|
"github.com/prometheus/prometheus/discovery/file"
|
|
|
|
"github.com/prometheus/prometheus/discovery/gce"
|
|
|
|
"github.com/prometheus/prometheus/discovery/kubernetes"
|
|
|
|
"github.com/prometheus/prometheus/discovery/marathon"
|
|
|
|
"github.com/prometheus/prometheus/discovery/openstack"
|
|
|
|
"github.com/prometheus/prometheus/discovery/triton"
|
|
|
|
"github.com/prometheus/prometheus/discovery/zookeeper"
|
|
|
|
)
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
var (
|
|
|
|
failedConfigs = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_configs_failed_total",
|
|
|
|
Help: "Total number of service discovery configurations that failed to load.",
|
|
|
|
},
|
|
|
|
)
|
2018-09-12 06:18:55 -07:00
|
|
|
discoveredTargets = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_sd_discovered_targets",
|
|
|
|
Help: "Current number of discovered targets.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
receivedUpdates = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_received_updates_total",
|
|
|
|
Help: "Total number of update events received from the SD providers.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
delayedUpdates = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_updates_delayed_total",
|
|
|
|
Help: "Total number of update events that couldn't be sent immediately.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
sentUpdates = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_updates_total",
|
|
|
|
Help: "Total number of update events sent to the SD consumers.",
|
|
|
|
},
|
|
|
|
)
|
2018-09-01 00:51:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-09-12 06:18:55 -07:00
|
|
|
prometheus.MustRegister(failedConfigs, discoveredTargets, receivedUpdates, delayedUpdates, sentUpdates)
|
2018-09-01 00:51:31 -07:00
|
|
|
}
|
|
|
|
|
2017-11-26 14:18:05 -08:00
|
|
|
// Discoverer provides information about target groups. It maintains a set
|
2017-11-25 05:13:54 -08:00
|
|
|
// of sources from which TargetGroups can originate. Whenever a discovery provider
|
2017-11-26 14:18:05 -08:00
|
|
|
// detects a potential change, it sends the TargetGroup through its channel.
|
2017-11-25 05:13:54 -08:00
|
|
|
//
|
2017-11-26 14:18:05 -08:00
|
|
|
// Discoverer does not know if an actual change happened.
|
2017-11-25 05:13:54 -08:00
|
|
|
// It does guarantee that it sends the new TargetGroup whenever a change happens.
|
|
|
|
//
|
2017-11-26 14:18:05 -08:00
|
|
|
// Discoverers should initially send a full set of all discoverable TargetGroups.
|
|
|
|
type Discoverer interface {
|
2018-09-12 06:18:55 -07:00
|
|
|
// Run hands a channel to the discovery provider (Consul, DNS etc) through which it can send
|
2017-11-25 05:13:54 -08:00
|
|
|
// updated target groups.
|
|
|
|
// Must returns if the context gets canceled. It should not close the update
|
|
|
|
// channel on returning.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
Run(ctx context.Context, up chan<- []*targetgroup.Group)
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2017-12-03 05:56:28 -08:00
|
|
|
type poolKey struct {
|
2017-12-18 11:41:31 -08:00
|
|
|
setName string
|
2017-12-03 05:56:28 -08:00
|
|
|
provider string
|
|
|
|
}
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
// provider holds a Discoverer instance, its configuration and its subscribers.
|
|
|
|
type provider struct {
|
|
|
|
name string
|
|
|
|
d Discoverer
|
|
|
|
subs []string
|
|
|
|
config interface{}
|
|
|
|
}
|
|
|
|
|
2017-11-26 14:18:05 -08:00
|
|
|
// NewManager is the Discovery Manager constructor
|
2018-01-25 15:32:36 -08:00
|
|
|
func NewManager(ctx context.Context, logger log.Logger) *Manager {
|
2018-09-01 00:51:31 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2017-11-26 14:18:05 -08:00
|
|
|
return &Manager{
|
|
|
|
logger: logger,
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
syncCh: make(chan map[string][]*targetgroup.Group),
|
2018-01-04 05:14:22 -08:00
|
|
|
targets: make(map[poolKey]map[string]*targetgroup.Group),
|
2017-11-26 14:18:05 -08:00
|
|
|
discoverCancel: []context.CancelFunc{},
|
2018-01-25 15:32:36 -08:00
|
|
|
ctx: ctx,
|
2018-09-05 06:44:52 -07:00
|
|
|
updatert: 5 * time.Second,
|
2018-09-28 03:29:24 -07:00
|
|
|
triggerSend: make(chan struct{}, 1),
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 03:42:09 -08:00
|
|
|
// Manager maintains a set of discovery providers and sends each update to a map channel.
|
|
|
|
// Targets are grouped by the target set name.
|
2017-11-26 14:18:05 -08:00
|
|
|
type Manager struct {
|
|
|
|
logger log.Logger
|
2018-01-17 10:12:58 -08:00
|
|
|
mtx sync.RWMutex
|
|
|
|
ctx context.Context
|
2017-11-26 14:18:05 -08:00
|
|
|
discoverCancel []context.CancelFunc
|
2018-09-01 00:51:31 -07:00
|
|
|
|
2018-01-04 13:57:28 -08:00
|
|
|
// Some Discoverers(eg. k8s) send only the updates for a given target group
|
2018-01-12 04:19:52 -08:00
|
|
|
// so we use map[tg.Source]*targetgroup.Group to know which group to update.
|
2018-01-04 05:14:22 -08:00
|
|
|
targets map[poolKey]map[string]*targetgroup.Group
|
2018-09-01 00:51:31 -07:00
|
|
|
// providers keeps track of SD providers.
|
|
|
|
providers []*provider
|
2018-09-12 06:18:55 -07:00
|
|
|
// The sync channel sends the updates as a map where the key is the job value from the scrape config.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
syncCh chan map[string][]*targetgroup.Group
|
2018-09-05 06:44:52 -07:00
|
|
|
|
|
|
|
// How long to wait before sending updates to the channel. The variable
|
|
|
|
// should only be modified in unit tests.
|
|
|
|
updatert time.Duration
|
2018-09-12 06:18:55 -07:00
|
|
|
|
2018-09-28 03:29:24 -07:00
|
|
|
// The triggerSend channel signals to the manager that new updates have been received from providers.
|
|
|
|
triggerSend chan struct{}
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the background processing
|
2018-01-25 15:32:36 -08:00
|
|
|
func (m *Manager) Run() error {
|
2018-09-28 03:29:24 -07:00
|
|
|
go m.sender()
|
2018-02-25 23:58:10 -08:00
|
|
|
for range m.ctx.Done() {
|
|
|
|
m.cancelDiscoverers()
|
|
|
|
return m.ctx.Err()
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-02-25 23:58:10 -08:00
|
|
|
return nil
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2018-09-12 06:18:55 -07:00
|
|
|
// SyncCh returns a read only channel used by all the clients to receive target updates.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func (m *Manager) SyncCh() <-chan map[string][]*targetgroup.Group {
|
2017-11-25 05:13:54 -08:00
|
|
|
return m.syncCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyConfig removes all running discovery providers and starts new ones using the provided config.
|
2017-12-30 09:27:50 -08:00
|
|
|
func (m *Manager) ApplyConfig(cfg map[string]sd_config.ServiceDiscoveryConfig) error {
|
2018-01-17 10:12:58 -08:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
m.cancelDiscoverers()
|
|
|
|
for name, scfg := range cfg {
|
2018-09-01 00:51:31 -07:00
|
|
|
m.registerProviders(scfg, name)
|
|
|
|
}
|
|
|
|
for _, prov := range m.providers {
|
|
|
|
m.startProvider(m.ctx, prov)
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:58 -08:00
|
|
|
return nil
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2018-05-30 05:14:34 -07:00
|
|
|
// StartCustomProvider is used for sdtool. Only use this if you know what you're doing.
|
|
|
|
func (m *Manager) StartCustomProvider(ctx context.Context, name string, worker Discoverer) {
|
2018-09-01 00:51:31 -07:00
|
|
|
p := &provider{
|
|
|
|
name: name,
|
|
|
|
d: worker,
|
|
|
|
subs: []string{name},
|
|
|
|
}
|
|
|
|
m.providers = append(m.providers, p)
|
|
|
|
m.startProvider(ctx, p)
|
2018-05-30 05:14:34 -07:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
func (m *Manager) startProvider(ctx context.Context, p *provider) {
|
|
|
|
level.Debug(m.logger).Log("msg", "Starting provider", "provider", p.name, "subs", fmt.Sprintf("%v", p.subs))
|
2017-12-01 04:59:24 -08:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
updates := make(chan []*targetgroup.Group)
|
2017-11-26 17:59:34 -08:00
|
|
|
|
|
|
|
m.discoverCancel = append(m.discoverCancel, cancel)
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
go p.d.Run(ctx, updates)
|
2018-09-05 04:32:47 -07:00
|
|
|
go m.updater(ctx, p, updates)
|
2017-12-01 04:59:24 -08:00
|
|
|
}
|
|
|
|
|
2018-09-05 04:32:47 -07:00
|
|
|
func (m *Manager) updater(ctx context.Context, p *provider, updates chan []*targetgroup.Group) {
|
2017-12-01 04:59:24 -08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case tgs, ok := <-updates:
|
2018-09-12 06:18:55 -07:00
|
|
|
receivedUpdates.Inc()
|
2017-12-01 04:59:24 -08:00
|
|
|
if !ok {
|
2018-09-12 06:18:55 -07:00
|
|
|
level.Debug(m.logger).Log("msg", "discoverer channel closed", "provider", p.name)
|
2018-09-05 06:44:52 -07:00
|
|
|
return
|
2017-11-26 17:59:34 -08:00
|
|
|
}
|
2018-09-12 06:18:55 -07:00
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, s := range p.subs {
|
|
|
|
m.updateGroup(poolKey{setName: s, provider: p.name}, tgs)
|
|
|
|
}
|
2018-01-26 05:57:33 -08:00
|
|
|
|
2018-08-27 08:12:11 -07:00
|
|
|
select {
|
2018-09-28 03:29:24 -07:00
|
|
|
case m.triggerSend <- struct{}{}:
|
2018-08-27 08:12:11 -07:00
|
|
|
default:
|
|
|
|
}
|
2018-09-12 06:18:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 03:29:24 -07:00
|
|
|
func (m *Manager) sender() {
|
2018-09-12 06:18:55 -07:00
|
|
|
ticker := time.NewTicker(m.updatert)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return
|
2018-09-05 04:32:47 -07:00
|
|
|
case <-ticker.C: // Some discoverers send updates too often so we throttle these with the ticker.
|
2018-08-27 08:12:11 -07:00
|
|
|
select {
|
2018-09-28 03:29:24 -07:00
|
|
|
case <-m.triggerSend:
|
2018-09-12 06:18:55 -07:00
|
|
|
sentUpdates.Inc()
|
2018-08-27 08:12:11 -07:00
|
|
|
select {
|
|
|
|
case m.syncCh <- m.allGroups():
|
|
|
|
default:
|
2018-09-12 06:18:55 -07:00
|
|
|
delayedUpdates.Inc()
|
|
|
|
level.Debug(m.logger).Log("msg", "discovery receiver's channel was full so will retry the next cycle")
|
2018-09-05 04:32:47 -07:00
|
|
|
select {
|
2018-09-28 03:29:24 -07:00
|
|
|
case m.triggerSend <- struct{}{}:
|
2018-09-05 04:32:47 -07:00
|
|
|
default:
|
|
|
|
}
|
2018-08-27 08:12:11 -07:00
|
|
|
}
|
|
|
|
default:
|
2018-01-26 05:57:33 -08:00
|
|
|
}
|
2017-11-26 17:59:34 -08:00
|
|
|
}
|
2017-12-01 04:59:24 -08:00
|
|
|
}
|
2017-11-26 17:59:34 -08:00
|
|
|
}
|
|
|
|
|
2017-11-26 14:18:05 -08:00
|
|
|
func (m *Manager) cancelDiscoverers() {
|
|
|
|
for _, c := range m.discoverCancel {
|
|
|
|
c()
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-01-04 05:14:22 -08:00
|
|
|
m.targets = make(map[poolKey]map[string]*targetgroup.Group)
|
2018-09-01 00:51:31 -07:00
|
|
|
m.providers = nil
|
2017-12-01 04:59:24 -08:00
|
|
|
m.discoverCancel = nil
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2018-01-04 05:57:34 -08:00
|
|
|
func (m *Manager) updateGroup(poolKey poolKey, tgs []*targetgroup.Group) {
|
2018-01-17 10:12:58 -08:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
for _, tg := range tgs {
|
|
|
|
if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics.
|
|
|
|
if _, ok := m.targets[poolKey]; !ok {
|
|
|
|
m.targets[poolKey] = make(map[string]*targetgroup.Group)
|
2018-01-04 05:14:22 -08:00
|
|
|
}
|
2018-01-17 10:12:58 -08:00
|
|
|
m.targets[poolKey][tg.Source] = tg
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2017-12-01 04:59:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:14:22 -08:00
|
|
|
func (m *Manager) allGroups() map[string][]*targetgroup.Group {
|
2018-01-17 10:12:58 -08:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
tSets := map[string][]*targetgroup.Group{}
|
2018-09-12 06:18:55 -07:00
|
|
|
var n int
|
2018-01-17 10:12:58 -08:00
|
|
|
for pkey, tsets := range m.targets {
|
|
|
|
for _, tg := range tsets {
|
|
|
|
// Even if the target group 'tg' is empty we still need to send it to the 'Scrape manager'
|
|
|
|
// to signal that it needs to stop all scrape loops for this target set.
|
|
|
|
tSets[pkey.setName] = append(tSets[pkey.setName], tg)
|
2018-09-12 06:18:55 -07:00
|
|
|
n += len(tg.Targets)
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 06:18:55 -07:00
|
|
|
discoveredTargets.Set(float64(n))
|
2018-01-17 10:12:58 -08:00
|
|
|
return tSets
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
func (m *Manager) registerProviders(cfg sd_config.ServiceDiscoveryConfig, setName string) {
|
|
|
|
add := func(cfg interface{}, newDiscoverer func() (Discoverer, error)) {
|
|
|
|
t := reflect.TypeOf(cfg).String()
|
|
|
|
for _, p := range m.providers {
|
|
|
|
if reflect.DeepEqual(cfg, p.config) {
|
|
|
|
p.subs = append(p.subs, setName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := newDiscoverer()
|
|
|
|
if err != nil {
|
|
|
|
level.Error(m.logger).Log("msg", "Cannot create service discovery", "err", err, "type", t)
|
|
|
|
failedConfigs.Inc()
|
|
|
|
return
|
|
|
|
}
|
2017-11-25 05:13:54 -08:00
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
provider := provider{
|
|
|
|
name: fmt.Sprintf("%s/%d", t, len(m.providers)),
|
|
|
|
d: d,
|
|
|
|
config: cfg,
|
|
|
|
subs: []string{setName},
|
|
|
|
}
|
|
|
|
m.providers = append(m.providers, &provider)
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.DNSSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return dns.NewDiscovery(*c, log.With(m.logger, "discovery", "dns")), nil
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.FileSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return file.NewDiscovery(c, log.With(m.logger, "discovery", "file")), nil
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.ConsulSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return consul.NewDiscovery(c, log.With(m.logger, "discovery", "consul"))
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.MarathonSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return marathon.NewDiscovery(*c, log.With(m.logger, "discovery", "marathon"))
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.KubernetesSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return kubernetes.New(log.With(m.logger, "discovery", "k8s"), c)
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.ServersetSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
2018-09-28 02:36:43 -07:00
|
|
|
return zookeeper.NewServersetDiscovery(c, log.With(m.logger, "discovery", "zookeeper"))
|
2018-09-01 00:51:31 -07:00
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.NerveSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
2018-09-28 02:36:43 -07:00
|
|
|
return zookeeper.NewNerveDiscovery(c, log.With(m.logger, "discovery", "nerve"))
|
2018-09-01 00:51:31 -07:00
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.EC2SDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return ec2.NewDiscovery(c, log.With(m.logger, "discovery", "ec2")), nil
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.OpenstackSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return openstack.NewDiscovery(c, log.With(m.logger, "discovery", "openstack"))
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.GCESDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return gce.NewDiscovery(*c, log.With(m.logger, "discovery", "gce"))
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.AzureSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return azure.NewDiscovery(c, log.With(m.logger, "discovery", "azure")), nil
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, c := range cfg.TritonSDConfigs {
|
|
|
|
add(c, func() (Discoverer, error) {
|
|
|
|
return triton.New(log.With(m.logger, "discovery", "triton"), c)
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
if len(cfg.StaticConfigs) > 0 {
|
2018-09-01 00:51:31 -07:00
|
|
|
add(setName, func() (Discoverer, error) {
|
|
|
|
return &StaticProvider{cfg.StaticConfigs}, nil
|
|
|
|
})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticProvider holds a list of target groups that never change.
|
|
|
|
type StaticProvider struct {
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
TargetGroups []*targetgroup.Group
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2017-11-26 14:18:05 -08:00
|
|
|
// Run implements the Worker interface.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func (sd *StaticProvider) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
2017-11-25 05:13:54 -08:00
|
|
|
// We still have to consider that the consumer exits right away in which case
|
|
|
|
// the context will be canceled.
|
|
|
|
select {
|
|
|
|
case ch <- sd.TargetGroups:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}
|