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
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2018-09-01 00:51:31 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-03-24 15:16:59 -07:00
|
|
|
"github.com/prometheus/common/config"
|
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
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-11-25 05:13:54 -08:00
|
|
|
)
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
var (
|
2019-02-22 01:24:19 -08:00
|
|
|
failedConfigs = prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_sd_failed_configs",
|
|
|
|
Help: "Current number of service discovery configurations that failed to load.",
|
2018-09-01 00:51:31 -07:00
|
|
|
},
|
2018-12-20 06:47:29 -08:00
|
|
|
[]string{"name"},
|
2018-09-01 00:51:31 -07:00
|
|
|
)
|
2018-10-18 08:46:59 -07:00
|
|
|
discoveredTargets = prometheus.NewGaugeVec(
|
2018-09-12 06:18:55 -07:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_sd_discovered_targets",
|
|
|
|
Help: "Current number of discovered targets.",
|
|
|
|
},
|
2018-10-18 08:46:59 -07:00
|
|
|
[]string{"name", "config"},
|
2018-09-12 06:18:55 -07:00
|
|
|
)
|
2018-12-20 06:47:29 -08:00
|
|
|
receivedUpdates = prometheus.NewCounterVec(
|
2018-09-12 06:18:55 -07:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_received_updates_total",
|
|
|
|
Help: "Total number of update events received from the SD providers.",
|
|
|
|
},
|
2018-12-20 06:47:29 -08:00
|
|
|
[]string{"name"},
|
2018-09-12 06:18:55 -07:00
|
|
|
)
|
2018-12-20 06:47:29 -08:00
|
|
|
delayedUpdates = prometheus.NewCounterVec(
|
2018-09-12 06:18:55 -07:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_updates_delayed_total",
|
|
|
|
Help: "Total number of update events that couldn't be sent immediately.",
|
|
|
|
},
|
2018-12-20 06:47:29 -08:00
|
|
|
[]string{"name"},
|
2018-09-12 06:18:55 -07:00
|
|
|
)
|
2018-12-20 06:47:29 -08:00
|
|
|
sentUpdates = prometheus.NewCounterVec(
|
2018-09-12 06:18:55 -07:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_sd_updates_total",
|
|
|
|
Help: "Total number of update events sent to the SD consumers.",
|
|
|
|
},
|
2018-12-20 06:47:29 -08:00
|
|
|
[]string{"name"},
|
2018-09-12 06:18:55 -07:00
|
|
|
)
|
2018-09-01 00:51:31 -07:00
|
|
|
)
|
|
|
|
|
2021-10-20 01:15:54 -07:00
|
|
|
func RegisterMetrics() {
|
2018-09-12 06:18:55 -07:00
|
|
|
prometheus.MustRegister(failedConfigs, discoveredTargets, receivedUpdates, delayedUpdates, sentUpdates)
|
2018-09-01 00:51:31 -07: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
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:06:15 -07:00
|
|
|
// Provider holds a Discoverer instance, its configuration, cancel func and its subscribers.
|
|
|
|
type Provider struct {
|
2018-09-01 00:51:31 -07:00
|
|
|
name string
|
|
|
|
d Discoverer
|
|
|
|
config interface{}
|
2021-10-20 01:16:20 -07:00
|
|
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
// done should be called after cleaning up resources associated with cancelled provider.
|
|
|
|
done func()
|
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
subs map[string]struct{}
|
|
|
|
|
|
|
|
// newSubs is used to temporary store subs to be used upon config reload completion.
|
|
|
|
newSubs map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:09:25 -07:00
|
|
|
// Discoverer return the Discoverer of the provider.
|
2022-06-13 12:06:15 -07:00
|
|
|
func (p *Provider) Discoverer() Discoverer {
|
|
|
|
return p.d
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
// IsStarted return true if Discoverer is started.
|
2022-06-13 12:06:15 -07:00
|
|
|
func (p *Provider) IsStarted() bool {
|
2021-10-20 01:16:20 -07:00
|
|
|
return p.cancel != nil
|
2018-09-01 00:51:31 -07:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:06:15 -07:00
|
|
|
func (p *Provider) Config() interface{} {
|
|
|
|
return p.config
|
|
|
|
}
|
|
|
|
|
2018-10-18 08:46:59 -07:00
|
|
|
// NewManager is the Discovery Manager constructor.
|
|
|
|
func NewManager(ctx context.Context, logger log.Logger, options ...func(*Manager)) *Manager {
|
2018-09-01 00:51:31 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2018-10-18 08:46:59 -07:00
|
|
|
mgr := &Manager{
|
2021-10-20 01:16:20 -07:00
|
|
|
logger: logger,
|
|
|
|
syncCh: make(chan map[string][]*targetgroup.Group),
|
|
|
|
targets: make(map[poolKey]map[string]*targetgroup.Group),
|
|
|
|
ctx: ctx,
|
|
|
|
updatert: 5 * time.Second,
|
|
|
|
triggerSend: make(chan struct{}, 1),
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-10-18 08:46:59 -07:00
|
|
|
for _, option := range options {
|
|
|
|
option(mgr)
|
|
|
|
}
|
|
|
|
return mgr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name sets the name of the manager.
|
|
|
|
func Name(n string) func(*Manager) {
|
|
|
|
return func(m *Manager) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
m.name = n
|
|
|
|
}
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2022-03-24 15:16:59 -07:00
|
|
|
// HTTPClientOptions sets the list of HTTP client options to expose to
|
|
|
|
// Discoverers. It is up to Discoverers to choose to use the options provided.
|
|
|
|
func HTTPClientOptions(opts ...config.HTTPClientOption) func(*Manager) {
|
|
|
|
return func(m *Manager) {
|
|
|
|
m.httpOpts = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-24 15:16:59 -07:00
|
|
|
logger log.Logger
|
|
|
|
name string
|
|
|
|
httpOpts []config.HTTPClientOption
|
|
|
|
mtx sync.RWMutex
|
|
|
|
ctx context.Context
|
2018-09-01 00:51:31 -07:00
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
// Some Discoverers(e.g. 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.
|
2021-10-20 01:16:20 -07:00
|
|
|
targets map[poolKey]map[string]*targetgroup.Group
|
|
|
|
targetsMtx sync.Mutex
|
|
|
|
|
2018-09-01 00:51:31 -07:00
|
|
|
// providers keeps track of SD providers.
|
2022-06-13 12:06:15 -07:00
|
|
|
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
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
// The triggerSend channel signals to the Manager that new updates have been received from providers.
|
2018-09-28 03:29:24 -07:00
|
|
|
triggerSend chan struct{}
|
2021-10-20 01:16:20 -07:00
|
|
|
|
|
|
|
// lastProvider counts providers registered during Manager's lifetime.
|
|
|
|
lastProvider uint
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:06:15 -07:00
|
|
|
// Providers returns the currently configured SD providers.
|
|
|
|
func (m *Manager) Providers() []*Provider {
|
|
|
|
return m.providers
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:16:20 -07: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()
|
2022-04-08 09:03:54 -07:00
|
|
|
<-m.ctx.Done()
|
|
|
|
m.cancelDiscoverers()
|
|
|
|
return m.ctx.Err()
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
// ApplyConfig checks if discovery provider with supplied config is already running and keeps them as is.
|
|
|
|
// Remaining providers are then stopped and new required providers are started using the provided config.
|
2020-08-20 05:48:26 -07:00
|
|
|
func (m *Manager) ApplyConfig(cfg map[string]Configs) error {
|
2018-01-17 10:12:58 -08:00
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
var failedCount int
|
2018-01-17 10:12:58 -08:00
|
|
|
for name, scfg := range cfg {
|
2019-09-20 21:01:57 -07:00
|
|
|
failedCount += m.registerProviders(scfg, name)
|
2018-09-01 00:51:31 -07:00
|
|
|
}
|
2019-09-20 21:01:57 -07:00
|
|
|
failedConfigs.WithLabelValues(m.name).Set(float64(failedCount))
|
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
// keep shows if we keep any providers after reload.
|
|
|
|
keep bool
|
2022-06-13 12:06:15 -07:00
|
|
|
newProviders []*Provider
|
2021-10-20 01:16:20 -07:00
|
|
|
)
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, prov := range m.providers {
|
2021-10-20 01:16:20 -07:00
|
|
|
// Cancel obsolete providers.
|
|
|
|
if len(prov.newSubs) == 0 {
|
|
|
|
wg.Add(1)
|
|
|
|
prov.done = func() {
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
prov.cancel()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newProviders = append(newProviders, prov)
|
|
|
|
// refTargets keeps reference targets used to populate new subs' targets
|
|
|
|
var refTargets map[string]*targetgroup.Group
|
|
|
|
prov.mu.Lock()
|
2021-11-04 17:13:04 -07:00
|
|
|
|
|
|
|
m.targetsMtx.Lock()
|
2021-10-20 01:16:20 -07:00
|
|
|
for s := range prov.subs {
|
|
|
|
keep = true
|
|
|
|
refTargets = m.targets[poolKey{s, prov.name}]
|
|
|
|
// Remove obsolete subs' targets.
|
|
|
|
if _, ok := prov.newSubs[s]; !ok {
|
|
|
|
delete(m.targets, poolKey{s, prov.name})
|
|
|
|
discoveredTargets.DeleteLabelValues(m.name, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set metrics and targets for new subs.
|
|
|
|
for s := range prov.newSubs {
|
|
|
|
if _, ok := prov.subs[s]; !ok {
|
|
|
|
discoveredTargets.WithLabelValues(m.name, s).Set(0)
|
|
|
|
}
|
|
|
|
if l := len(refTargets); l > 0 {
|
|
|
|
m.targets[poolKey{s, prov.name}] = make(map[string]*targetgroup.Group, l)
|
|
|
|
for k, v := range refTargets {
|
|
|
|
m.targets[poolKey{s, prov.name}][k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 17:13:04 -07:00
|
|
|
m.targetsMtx.Unlock()
|
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
prov.subs = prov.newSubs
|
|
|
|
prov.newSubs = map[string]struct{}{}
|
|
|
|
prov.mu.Unlock()
|
|
|
|
if !prov.IsStarted() {
|
|
|
|
m.startProvider(m.ctx, prov)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Currently downstream managers expect full target state upon config reload, so we must oblige.
|
|
|
|
// While startProvider does pull the trigger, it may take some time to do so, therefore
|
|
|
|
// we pull the trigger as soon as possible so that downstream managers can populate their state.
|
|
|
|
// See https://github.com/prometheus/prometheus/pull/8639 for details.
|
|
|
|
if keep {
|
|
|
|
select {
|
|
|
|
case m.triggerSend <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2021-10-20 01:16:20 -07:00
|
|
|
m.providers = newProviders
|
|
|
|
wg.Wait()
|
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) {
|
2022-06-13 12:06:15 -07:00
|
|
|
p := &Provider{
|
2018-09-01 00:51:31 -07:00
|
|
|
name: name,
|
|
|
|
d: worker,
|
2021-10-20 01:16:20 -07:00
|
|
|
subs: map[string]struct{}{
|
|
|
|
name: {},
|
|
|
|
},
|
2018-09-01 00:51:31 -07:00
|
|
|
}
|
|
|
|
m.providers = append(m.providers, p)
|
|
|
|
m.startProvider(ctx, p)
|
2018-05-30 05:14:34 -07:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:06:15 -07:00
|
|
|
func (m *Manager) startProvider(ctx context.Context, p *Provider) {
|
2018-09-01 00:51:31 -07:00
|
|
|
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
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
p.cancel = cancel
|
2017-11-26 17:59:34 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
// cleaner cleans resources associated with provider.
|
2022-06-13 12:06:15 -07:00
|
|
|
func (m *Manager) cleaner(p *Provider) {
|
2021-10-20 01:16:20 -07:00
|
|
|
m.targetsMtx.Lock()
|
|
|
|
p.mu.RLock()
|
|
|
|
for s := range p.subs {
|
|
|
|
delete(m.targets, poolKey{s, p.name})
|
|
|
|
}
|
|
|
|
p.mu.RUnlock()
|
|
|
|
m.targetsMtx.Unlock()
|
|
|
|
if p.done != nil {
|
|
|
|
p.done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:06:15 -07:00
|
|
|
func (m *Manager) updater(ctx context.Context, p *Provider, updates chan []*targetgroup.Group) {
|
2021-10-20 01:16:20 -07:00
|
|
|
// Ensure targets from this provider are cleaned up.
|
|
|
|
defer m.cleaner(p)
|
2017-12-01 04:59:24 -08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case tgs, ok := <-updates:
|
2018-12-20 06:47:29 -08:00
|
|
|
receivedUpdates.WithLabelValues(m.name).Inc()
|
2017-12-01 04:59:24 -08:00
|
|
|
if !ok {
|
2020-04-11 01:22:18 -07:00
|
|
|
level.Debug(m.logger).Log("msg", "Discoverer channel closed", "provider", p.name)
|
2021-10-20 01:16:20 -07:00
|
|
|
// Wait for provider cancellation to ensure targets are cleaned up when expected.
|
|
|
|
<-ctx.Done()
|
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
|
|
|
|
2021-10-20 01:16:20 -07:00
|
|
|
p.mu.RLock()
|
|
|
|
for s := range p.subs {
|
2018-09-01 00:51:31 -07:00
|
|
|
m.updateGroup(poolKey{setName: s, provider: p.name}, tgs)
|
|
|
|
}
|
2021-10-20 01:16:20 -07:00
|
|
|
p.mu.RUnlock()
|
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
|
2021-10-20 01:16:20 -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-12-20 06:47:29 -08:00
|
|
|
sentUpdates.WithLabelValues(m.name).Inc()
|
2018-08-27 08:12:11 -07:00
|
|
|
select {
|
|
|
|
case m.syncCh <- m.allGroups():
|
|
|
|
default:
|
2018-12-20 06:47:29 -08:00
|
|
|
delayedUpdates.WithLabelValues(m.name).Inc()
|
2020-04-11 01:22:18 -07:00
|
|
|
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() {
|
2021-10-20 01:16:20 -07:00
|
|
|
m.mtx.RLock()
|
|
|
|
defer m.mtx.RUnlock()
|
|
|
|
for _, p := range m.providers {
|
|
|
|
if p.cancel != nil {
|
|
|
|
p.cancel()
|
|
|
|
}
|
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) {
|
2021-10-20 01:16:20 -07:00
|
|
|
m.targetsMtx.Lock()
|
|
|
|
defer m.targetsMtx.Unlock()
|
2018-01-17 10:12:58 -08:00
|
|
|
|
2020-03-01 23:35:02 -08:00
|
|
|
if _, ok := m.targets[poolKey]; !ok {
|
|
|
|
m.targets[poolKey] = make(map[string]*targetgroup.Group)
|
|
|
|
}
|
2018-01-17 10:12:58 -08:00
|
|
|
for _, tg := range tgs {
|
|
|
|
if tg != nil { // Some Discoverers send nil target group so need to check for it to avoid panics.
|
2023-08-14 14:28:23 -07: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
|
|
|
tSets := map[string][]*targetgroup.Group{}
|
2021-05-14 13:32:15 -07:00
|
|
|
n := map[string]int{}
|
2021-10-20 01:16:20 -07:00
|
|
|
|
|
|
|
m.targetsMtx.Lock()
|
|
|
|
defer m.targetsMtx.Unlock()
|
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)
|
2021-05-14 13:32:15 -07:00
|
|
|
n[pkey.setName] += len(tg.Targets)
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2021-05-14 13:32:15 -07:00
|
|
|
}
|
|
|
|
for setName, v := range n {
|
|
|
|
discoveredTargets.WithLabelValues(m.name, setName).Set(float64(v))
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2018-01-17 10:12:58 -08:00
|
|
|
return tSets
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
2019-09-20 21:01:57 -07:00
|
|
|
// registerProviders returns a number of failed SD config.
|
2020-08-20 05:48:26 -07:00
|
|
|
func (m *Manager) registerProviders(cfgs Configs, setName string) int {
|
2019-09-20 21:01:57 -07:00
|
|
|
var (
|
2020-08-20 05:48:26 -07:00
|
|
|
failed int
|
|
|
|
added bool
|
2019-09-20 21:01:57 -07:00
|
|
|
)
|
2020-08-20 05:48:26 -07:00
|
|
|
add := func(cfg Config) {
|
2018-09-01 00:51:31 -07:00
|
|
|
for _, p := range m.providers {
|
|
|
|
if reflect.DeepEqual(cfg, p.config) {
|
2021-10-20 01:16:20 -07:00
|
|
|
p.newSubs[setName] = struct{}{}
|
2018-11-30 08:59:57 -08:00
|
|
|
added = true
|
2018-09-01 00:51:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 05:48:26 -07:00
|
|
|
typ := cfg.Name()
|
|
|
|
d, err := cfg.NewDiscoverer(DiscovererOptions{
|
2022-12-22 03:19:06 -08:00
|
|
|
Logger: log.With(m.logger, "discovery", typ, "config", setName),
|
2022-03-24 15:16:59 -07:00
|
|
|
HTTPClientOptions: m.httpOpts,
|
2020-08-20 05:48:26 -07:00
|
|
|
})
|
2018-09-01 00:51:31 -07:00
|
|
|
if err != nil {
|
2022-12-22 03:19:06 -08:00
|
|
|
level.Error(m.logger).Log("msg", "Cannot create service discovery", "err", err, "type", typ, "config", setName)
|
2020-08-20 05:48:26 -07:00
|
|
|
failed++
|
2018-09-01 00:51:31 -07:00
|
|
|
return
|
|
|
|
}
|
2022-06-13 12:06:15 -07:00
|
|
|
m.providers = append(m.providers, &Provider{
|
2021-10-20 01:16:20 -07:00
|
|
|
name: fmt.Sprintf("%s/%d", typ, m.lastProvider),
|
2018-09-01 00:51:31 -07:00
|
|
|
d: d,
|
|
|
|
config: cfg,
|
2021-10-20 01:16:20 -07:00
|
|
|
newSubs: map[string]struct{}{
|
|
|
|
setName: {},
|
|
|
|
},
|
2018-09-01 00:51:31 -07:00
|
|
|
})
|
2021-10-20 01:16:20 -07:00
|
|
|
m.lastProvider++
|
2020-08-20 05:48:26 -07:00
|
|
|
added = true
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2020-08-20 05:48:26 -07:00
|
|
|
for _, cfg := range cfgs {
|
|
|
|
add(cfg)
|
2018-11-30 08:59:57 -08:00
|
|
|
}
|
|
|
|
if !added {
|
|
|
|
// Add an empty target group to force the refresh of the corresponding
|
|
|
|
// scrape pool and to notify the receiver that this target set has no
|
|
|
|
// current targets.
|
|
|
|
// It can happen because the combined set of SD configurations is empty
|
|
|
|
// or because we fail to instantiate all the SD configurations.
|
2020-08-20 05:48:26 -07:00
|
|
|
add(StaticConfig{{}})
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2020-08-20 05:48:26 -07:00
|
|
|
return failed
|
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)
|
|
|
|
}
|