2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-04 05:41:47 -08:00
|
|
|
// 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 retrieval
|
|
|
|
|
|
|
|
import (
|
2015-04-20 03:24:25 -07:00
|
|
|
"fmt"
|
2016-03-08 08:12:27 -08:00
|
|
|
"sort"
|
2015-04-20 03:24:25 -07:00
|
|
|
"strings"
|
2014-11-11 15:38:28 -08:00
|
|
|
"sync"
|
2016-02-19 13:54:51 -08:00
|
|
|
"time"
|
2014-12-26 04:37:30 -08:00
|
|
|
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-02-17 10:33:17 -08:00
|
|
|
"golang.org/x/net/context"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-04-20 03:24:25 -07:00
|
|
|
"github.com/prometheus/prometheus/retrieval/discovery"
|
2015-03-14 19:36:15 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2013-01-04 05:41:47 -08:00
|
|
|
)
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// A TargetProvider provides information about target groups. It maintains a set
|
|
|
|
// of sources from which TargetGroups can originate. Whenever a target provider
|
2015-04-25 03:59:05 -07:00
|
|
|
// detects a potential change, it sends the TargetGroup through its provided channel.
|
2015-04-20 03:24:25 -07:00
|
|
|
//
|
|
|
|
// The TargetProvider does not have to guarantee that an actual change happened.
|
|
|
|
// It does guarantee that it sends the new TargetGroup whenever a change happens.
|
2015-08-13 11:38:25 -07:00
|
|
|
//
|
2016-02-17 10:33:17 -08:00
|
|
|
// Providers must initially send all known target groups as soon as it can.
|
2015-04-20 03:24:25 -07:00
|
|
|
type TargetProvider interface {
|
|
|
|
// Run hands a channel to the target provider through which it can send
|
|
|
|
// updated target groups. The channel must be closed by the target provider
|
|
|
|
// if no more updates will be sent.
|
2015-08-10 07:44:32 -07:00
|
|
|
// On receiving from done Run must return.
|
2016-02-18 08:26:27 -08:00
|
|
|
Run(ctx context.Context, up chan<- []*config.TargetGroup)
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// TargetManager maintains a set of targets, starts and stops their scraping and
|
|
|
|
// creates the new targets based on the target groups it receives from various
|
|
|
|
// target providers.
|
|
|
|
type TargetManager struct {
|
2016-02-19 13:54:51 -08:00
|
|
|
appender storage.SampleAppender
|
|
|
|
scrapeConfigs []*config.ScrapeConfig
|
2016-02-17 10:33:17 -08:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
wg sync.WaitGroup
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
// Set of unqiue targets by scrape configuration.
|
|
|
|
targetSets map[string]*targetSet
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2015-05-12 07:52:56 -07:00
|
|
|
// NewTargetManager creates a new TargetManager.
|
2016-02-17 10:33:17 -08:00
|
|
|
func NewTargetManager(app storage.SampleAppender) *TargetManager {
|
|
|
|
return &TargetManager{
|
2016-02-19 13:54:51 -08:00
|
|
|
appender: app,
|
|
|
|
targetSets: map[string]*targetSet{},
|
2015-08-10 07:44:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// Run starts background processing to handle target updates.
|
|
|
|
func (tm *TargetManager) Run() {
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Info("Starting target manager...")
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-08-10 07:44:32 -07:00
|
|
|
tm.mtx.Lock()
|
2016-02-19 13:54:51 -08:00
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
tm.ctx, tm.cancel = context.WithCancel(context.Background())
|
|
|
|
tm.reload()
|
2016-02-17 10:33:17 -08:00
|
|
|
|
|
|
|
tm.mtx.Unlock()
|
2016-02-19 13:54:51 -08:00
|
|
|
|
2016-02-17 10:33:17 -08:00
|
|
|
tm.wg.Wait()
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// Stop all background processing.
|
|
|
|
func (tm *TargetManager) Stop() {
|
2016-02-17 10:33:17 -08:00
|
|
|
log.Infoln("Stopping target manager...")
|
2015-05-27 03:32:39 -07:00
|
|
|
|
2015-08-10 07:44:32 -07:00
|
|
|
tm.mtx.Lock()
|
2016-02-19 13:54:51 -08:00
|
|
|
// Cancel the base context, this will cause all target providers to shut down
|
|
|
|
// and all in-flight scrapes to abort immmediately.
|
2016-02-17 10:33:17 -08:00
|
|
|
// Started inserts will be finished before terminating.
|
|
|
|
tm.cancel()
|
|
|
|
tm.mtx.Unlock()
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
// Wait for all scrape inserts to complete.
|
2016-02-17 10:33:17 -08:00
|
|
|
tm.wg.Wait()
|
2016-02-19 13:54:51 -08:00
|
|
|
|
|
|
|
log.Debugln("Target manager stopped")
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
func (tm *TargetManager) reload() {
|
|
|
|
jobs := map[string]struct{}{}
|
|
|
|
|
|
|
|
// Start new target sets and update existing ones.
|
|
|
|
for _, scfg := range tm.scrapeConfigs {
|
|
|
|
jobs[scfg.JobName] = struct{}{}
|
|
|
|
|
|
|
|
ts, ok := tm.targetSets[scfg.JobName]
|
|
|
|
if !ok {
|
|
|
|
ts = newTargetSet(scfg, tm.appender)
|
|
|
|
tm.targetSets[scfg.JobName] = ts
|
|
|
|
|
|
|
|
tm.wg.Add(1)
|
|
|
|
|
|
|
|
go func(ts *targetSet) {
|
|
|
|
ts.runScraping(tm.ctx)
|
|
|
|
tm.wg.Done()
|
|
|
|
}(ts)
|
2016-02-23 04:34:24 -08:00
|
|
|
} else {
|
|
|
|
ts.reload(scfg)
|
2016-02-22 03:42:11 -08:00
|
|
|
}
|
|
|
|
ts.runProviders(tm.ctx, providersFromConfig(scfg))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old target sets. Waiting for stopping is already guaranteed
|
|
|
|
// by the goroutine that started the target set.
|
|
|
|
for name, ts := range tm.targetSets {
|
|
|
|
if _, ok := jobs[name]; !ok {
|
|
|
|
ts.cancel()
|
|
|
|
delete(tm.targetSets, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// Pools returns the targets currently being scraped bucketed by their job name.
|
2016-03-08 08:12:27 -08:00
|
|
|
func (tm *TargetManager) Pools() map[string]Targets {
|
2015-08-10 07:44:32 -07:00
|
|
|
tm.mtx.RLock()
|
|
|
|
defer tm.mtx.RUnlock()
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2016-03-08 08:12:27 -08:00
|
|
|
pools := map[string]Targets{}
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2016-02-17 10:33:17 -08:00
|
|
|
// TODO(fabxc): this is just a hack to maintain compatibility for now.
|
2016-02-19 13:54:51 -08:00
|
|
|
for _, ps := range tm.targetSets {
|
2016-02-23 05:37:25 -08:00
|
|
|
ps.scrapePool.mtx.RLock()
|
|
|
|
|
|
|
|
for _, t := range ps.scrapePool.targets {
|
|
|
|
job := string(t.Labels()[model.JobLabel])
|
|
|
|
pools[job] = append(pools[job], t)
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-02-23 05:37:25 -08:00
|
|
|
|
|
|
|
ps.scrapePool.mtx.RUnlock()
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-03-08 08:12:27 -08:00
|
|
|
for _, targets := range pools {
|
|
|
|
sort.Sort(targets)
|
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
return pools
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyConfig resets the manager's target providers and job configurations as defined
|
|
|
|
// by the new cfg. The state of targets that are valid in the new configuration remains unchanged.
|
2015-06-02 09:44:41 -07:00
|
|
|
// Returns true on success.
|
|
|
|
func (tm *TargetManager) ApplyConfig(cfg *config.Config) bool {
|
2016-02-17 10:33:17 -08:00
|
|
|
tm.mtx.Lock()
|
2016-02-22 03:42:11 -08:00
|
|
|
defer tm.mtx.Unlock()
|
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
tm.scrapeConfigs = cfg.ScrapeConfigs
|
2015-05-27 03:32:39 -07:00
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
if tm.ctx != nil {
|
|
|
|
tm.reload()
|
|
|
|
}
|
2015-06-02 09:44:41 -07:00
|
|
|
return true
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
// targetSet holds several TargetProviders for which the same scrape configuration
|
2016-02-23 05:37:25 -08:00
|
|
|
// is used. It maintains target groups from all given providers and sync them
|
|
|
|
// to a scrape pool.
|
2016-02-19 13:54:51 -08:00
|
|
|
type targetSet struct {
|
2016-02-23 05:37:25 -08:00
|
|
|
mtx sync.RWMutex
|
|
|
|
|
|
|
|
// Sets of targets by a source string that is unique across target providers.
|
2016-02-28 11:03:44 -08:00
|
|
|
tgroups map[string][]*Target
|
2016-02-19 13:54:51 -08:00
|
|
|
providers map[string]TargetProvider
|
2015-08-07 04:18:19 -07:00
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
scrapePool *scrapePool
|
|
|
|
config *config.ScrapeConfig
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
syncCh chan struct{}
|
|
|
|
cancelScraping func()
|
|
|
|
cancelProviders func()
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
func newTargetSet(cfg *config.ScrapeConfig, app storage.SampleAppender) *targetSet {
|
|
|
|
ts := &targetSet{
|
2016-02-28 11:03:44 -08:00
|
|
|
tgroups: map[string][]*Target{},
|
2016-02-23 02:56:09 -08:00
|
|
|
scrapePool: newScrapePool(cfg, app),
|
2016-02-19 13:54:51 -08:00
|
|
|
syncCh: make(chan struct{}, 1),
|
|
|
|
config: cfg,
|
2016-02-17 10:33:17 -08:00
|
|
|
}
|
2016-02-19 13:54:51 -08:00
|
|
|
return ts
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
func (ts *targetSet) cancel() {
|
|
|
|
ts.mtx.RLock()
|
|
|
|
defer ts.mtx.RUnlock()
|
|
|
|
|
|
|
|
if ts.cancelScraping != nil {
|
|
|
|
ts.cancelScraping()
|
|
|
|
}
|
|
|
|
if ts.cancelProviders != nil {
|
|
|
|
ts.cancelProviders()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 04:34:24 -08:00
|
|
|
func (ts *targetSet) reload(cfg *config.ScrapeConfig) {
|
|
|
|
ts.mtx.Lock()
|
|
|
|
ts.config = cfg
|
|
|
|
ts.mtx.Unlock()
|
|
|
|
|
|
|
|
ts.scrapePool.reload(cfg)
|
|
|
|
}
|
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
func (ts *targetSet) runScraping(ctx context.Context) {
|
|
|
|
ctx, ts.cancelScraping = context.WithCancel(ctx)
|
|
|
|
|
2016-04-05 11:27:31 -07:00
|
|
|
ts.scrapePool.init(ctx)
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
// Throttle syncing to once per five seconds.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break Loop
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break Loop
|
|
|
|
case <-ts.syncCh:
|
2016-02-23 05:37:25 -08:00
|
|
|
ts.mtx.RLock()
|
2016-02-19 13:54:51 -08:00
|
|
|
ts.sync()
|
2016-02-23 05:37:25 -08:00
|
|
|
ts.mtx.RUnlock()
|
2016-02-19 13:54:51 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
// We want to wait for all pending target scrapes to complete though to ensure there'll
|
|
|
|
// be no more storage writes after this point.
|
|
|
|
ts.scrapePool.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *targetSet) sync() {
|
2016-02-28 11:03:44 -08:00
|
|
|
var all []*Target
|
|
|
|
for _, targets := range ts.tgroups {
|
|
|
|
all = append(all, targets...)
|
2016-02-23 05:37:25 -08:00
|
|
|
}
|
2016-02-28 11:03:44 -08:00
|
|
|
ts.scrapePool.sync(all)
|
2016-02-19 13:54:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *targetSet) runProviders(ctx context.Context, providers map[string]TargetProvider) {
|
|
|
|
// Lock for the entire time. This may mean up to 5 seconds until the full initial set
|
|
|
|
// is retrieved and applied.
|
|
|
|
// We could release earlier with some tweaks, but this is easier to reason about.
|
|
|
|
ts.mtx.Lock()
|
|
|
|
defer ts.mtx.Unlock()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2016-02-22 03:42:11 -08:00
|
|
|
if ts.cancelProviders != nil {
|
|
|
|
ts.cancelProviders()
|
2016-02-19 13:54:51 -08:00
|
|
|
}
|
2016-02-22 03:42:11 -08:00
|
|
|
ctx, ts.cancelProviders = context.WithCancel(ctx)
|
2016-02-19 13:54:51 -08:00
|
|
|
|
|
|
|
for name, prov := range providers {
|
2016-02-17 10:33:17 -08:00
|
|
|
wg.Add(1)
|
2016-02-19 13:54:51 -08:00
|
|
|
|
|
|
|
updates := make(chan []*config.TargetGroup)
|
|
|
|
|
2016-02-17 10:33:17 -08:00
|
|
|
go func(name string, prov TargetProvider) {
|
2016-02-19 13:54:51 -08:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2016-03-08 06:49:03 -08:00
|
|
|
case initial, ok := <-updates:
|
|
|
|
// Handle the case that a target provider exits and closes the channel
|
|
|
|
// before the context is done.
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2016-02-19 13:54:51 -08:00
|
|
|
// First set of all targets the provider knows.
|
2016-03-08 06:49:03 -08:00
|
|
|
for _, tgroup := range initial {
|
2016-05-25 16:59:16 -07:00
|
|
|
if tgroup == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-08 06:49:03 -08:00
|
|
|
targets, err := targetsFromGroup(tgroup, ts.config)
|
|
|
|
if err != nil {
|
|
|
|
log.With("target_group", tgroup).Errorf("Target update failed: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ts.tgroups[name+"/"+tgroup.Source] = targets
|
|
|
|
}
|
2016-02-19 13:54:51 -08:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
// Initial set didn't arrive. Act as if it was empty
|
|
|
|
// and wait for updates later on.
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-19 13:54:51 -08:00
|
|
|
// Start listening for further updates.
|
2016-02-17 10:33:17 -08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2016-03-08 06:49:03 -08:00
|
|
|
case tgs, ok := <-updates:
|
|
|
|
// Handle the case that a target provider exits and closes the channel
|
|
|
|
// before the context is done.
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2016-02-18 08:26:27 -08:00
|
|
|
for _, tg := range tgs {
|
2016-02-19 13:54:51 -08:00
|
|
|
if err := ts.update(name, tg); err != nil {
|
2016-02-18 08:26:27 -08:00
|
|
|
log.With("target_group", tg).Errorf("Target update failed: %s", err)
|
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(name, prov)
|
|
|
|
|
2016-02-18 08:26:27 -08:00
|
|
|
go prov.Run(ctx, updates)
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
// We wait for a full initial set of target groups before releasing the mutex
|
|
|
|
// to ensure the initial sync is complete and there are no races with subsequent updates.
|
2016-02-17 10:33:17 -08:00
|
|
|
wg.Wait()
|
2016-02-19 13:54:51 -08:00
|
|
|
ts.sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
// update handles a target group update from a target provider identified by the name.
|
|
|
|
func (ts *targetSet) update(name string, tgroup *config.TargetGroup) error {
|
2016-05-25 16:59:16 -07:00
|
|
|
if tgroup == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-19 13:54:51 -08:00
|
|
|
targets, err := targetsFromGroup(tgroup, ts.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.mtx.Lock()
|
|
|
|
defer ts.mtx.Unlock()
|
|
|
|
|
|
|
|
ts.tgroups[name+"/"+tgroup.Source] = targets
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ts.syncCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-07 04:18:19 -07:00
|
|
|
// providersFromConfig returns all TargetProviders configured in cfg.
|
2016-02-17 10:33:17 -08:00
|
|
|
func providersFromConfig(cfg *config.ScrapeConfig) map[string]TargetProvider {
|
|
|
|
providers := map[string]TargetProvider{}
|
2015-08-07 04:18:19 -07:00
|
|
|
|
|
|
|
app := func(mech string, i int, tp TargetProvider) {
|
2016-02-17 10:33:17 -08:00
|
|
|
providers[fmt.Sprintf("%s/%d", mech, i)] = tp
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cfg.DNSSDConfigs {
|
2016-05-05 02:45:51 -07:00
|
|
|
app("dns", i, discovery.NewDNS(c))
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
for i, c := range cfg.FileSDConfigs {
|
|
|
|
app("file", i, discovery.NewFileDiscovery(c))
|
|
|
|
}
|
|
|
|
for i, c := range cfg.ConsulSDConfigs {
|
2016-04-25 07:56:27 -07:00
|
|
|
k, err := discovery.NewConsul(c)
|
2015-11-30 09:41:48 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot create Consul discovery: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
app("consul", i, k)
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
for i, c := range cfg.MarathonSDConfigs {
|
2016-04-25 06:30:11 -07:00
|
|
|
app("marathon", i, discovery.NewMarathon(c))
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
for i, c := range cfg.KubernetesSDConfigs {
|
|
|
|
k, err := discovery.NewKubernetesDiscovery(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot create Kubernetes discovery: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
app("kubernetes", i, k)
|
|
|
|
}
|
2015-08-07 04:18:19 -07:00
|
|
|
for i, c := range cfg.ServersetSDConfigs {
|
|
|
|
app("serverset", i, discovery.NewServersetDiscovery(c))
|
|
|
|
}
|
2016-01-09 15:34:32 -08:00
|
|
|
for i, c := range cfg.NerveSDConfigs {
|
|
|
|
app("nerve", i, discovery.NewNerveDiscovery(c))
|
|
|
|
}
|
2015-09-21 11:49:19 -07:00
|
|
|
for i, c := range cfg.EC2SDConfigs {
|
|
|
|
app("ec2", i, discovery.NewEC2Discovery(c))
|
|
|
|
}
|
2016-04-06 20:47:02 -07:00
|
|
|
for i, c := range cfg.AzureSDConfigs {
|
|
|
|
app("azure", i, discovery.NewAzureDiscovery(c))
|
|
|
|
}
|
2015-08-07 04:18:19 -07:00
|
|
|
if len(cfg.TargetGroups) > 0 {
|
|
|
|
app("static", 0, NewStaticProvider(cfg.TargetGroups))
|
|
|
|
}
|
|
|
|
|
|
|
|
return providers
|
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// targetsFromGroup builds targets based on the given TargetGroup and config.
|
2016-05-25 16:17:25 -07:00
|
|
|
// Panics if target group is nil.
|
2016-02-28 11:03:44 -08:00
|
|
|
func targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Target, error) {
|
|
|
|
targets := make([]*Target, 0, len(tg.Targets))
|
2016-02-19 13:54:51 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
for i, labels := range tg.Targets {
|
2015-07-05 12:25:45 -07:00
|
|
|
for k, v := range cfg.Params {
|
|
|
|
if len(v) > 0 {
|
2015-08-20 08:18:46 -07:00
|
|
|
labels[model.LabelName(model.ParamLabelPrefix+k)] = model.LabelValue(v[0])
|
2015-07-05 12:25:45 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 03:59:05 -07:00
|
|
|
// Copy labels into the labelset for the target if they are not
|
|
|
|
// set already. Apply the labelsets in order of decreasing precedence.
|
2015-08-20 08:18:46 -07:00
|
|
|
labelsets := []model.LabelSet{
|
2015-04-25 03:59:05 -07:00
|
|
|
tg.Labels,
|
2015-05-24 11:48:56 -07:00
|
|
|
{
|
2015-08-20 08:18:46 -07:00
|
|
|
model.SchemeLabel: model.LabelValue(cfg.Scheme),
|
|
|
|
model.MetricsPathLabel: model.LabelValue(cfg.MetricsPath),
|
|
|
|
model.JobLabel: model.LabelValue(cfg.JobName),
|
2015-05-07 01:55:03 -07:00
|
|
|
},
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2015-04-25 03:59:05 -07:00
|
|
|
for _, lset := range labelsets {
|
|
|
|
for ln, lv := range lset {
|
|
|
|
if _, ok := labels[ln]; !ok {
|
|
|
|
labels[ln] = lv
|
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2015-04-25 03:59:05 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
if _, ok := labels[model.AddressLabel]; !ok {
|
2015-04-28 15:08:58 -07:00
|
|
|
return nil, fmt.Errorf("instance %d in target group %s has no address", i, tg)
|
|
|
|
}
|
|
|
|
|
2015-06-05 13:42:39 -07:00
|
|
|
preRelabelLabels := labels
|
|
|
|
|
2015-05-07 01:55:03 -07:00
|
|
|
labels, err := Relabel(labels, cfg.RelabelConfigs...)
|
2015-04-28 15:08:58 -07:00
|
|
|
if err != nil {
|
2015-05-12 07:52:56 -07:00
|
|
|
return nil, fmt.Errorf("error while relabeling instance %d in target group %s: %s", i, tg, err)
|
2015-04-28 15:08:58 -07:00
|
|
|
}
|
|
|
|
// Check if the target was dropped.
|
|
|
|
if labels == nil {
|
|
|
|
continue
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2015-11-08 03:42:18 -08:00
|
|
|
// If no port was provided, infer it based on the used scheme.
|
|
|
|
addr := string(labels[model.AddressLabel])
|
|
|
|
if !strings.Contains(addr, ":") {
|
|
|
|
switch labels[model.SchemeLabel] {
|
|
|
|
case "http", "":
|
|
|
|
addr = fmt.Sprintf("%s:80", addr)
|
|
|
|
case "https":
|
|
|
|
addr = fmt.Sprintf("%s:443", addr)
|
|
|
|
default:
|
2016-05-25 16:05:20 -07:00
|
|
|
return nil, fmt.Errorf("invalid scheme: %q", cfg.Scheme)
|
2015-11-08 03:42:18 -08:00
|
|
|
}
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
|
|
|
}
|
2015-11-07 06:25:51 -08:00
|
|
|
if err = config.CheckTargetAddress(labels[model.AddressLabel]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
for ln := range labels {
|
2015-04-25 03:59:05 -07:00
|
|
|
// Meta labels are deleted after relabelling. Other internal labels propagate to
|
|
|
|
// the target which decides whether they will be part of their label set.
|
2015-08-20 08:18:46 -07:00
|
|
|
if strings.HasPrefix(string(ln), model.MetaLabelPrefix) {
|
2015-04-20 03:24:25 -07:00
|
|
|
delete(labels, ln)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
if _, ok := labels[model.InstanceLabel]; !ok {
|
|
|
|
labels[model.InstanceLabel] = labels[model.AddressLabel]
|
|
|
|
}
|
|
|
|
|
2016-02-28 11:03:44 -08:00
|
|
|
targets = append(targets, NewTarget(labels, preRelabelLabels, cfg.Params))
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2015-04-25 03:59:05 -07:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
return targets, nil
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-02-22 12:07:35 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// StaticProvider holds a list of target groups that never change.
|
|
|
|
type StaticProvider struct {
|
|
|
|
TargetGroups []*config.TargetGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStaticProvider returns a StaticProvider configured with the given
|
|
|
|
// target groups.
|
2015-05-07 01:55:03 -07:00
|
|
|
func NewStaticProvider(groups []*config.TargetGroup) *StaticProvider {
|
2015-04-20 03:24:25 -07:00
|
|
|
for i, tg := range groups {
|
2015-08-07 04:18:19 -07:00
|
|
|
tg.Source = fmt.Sprintf("%d", i)
|
2015-05-07 01:55:03 -07:00
|
|
|
}
|
2016-02-18 08:26:27 -08:00
|
|
|
return &StaticProvider{groups}
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2016-02-18 08:26:27 -08:00
|
|
|
func (sd *StaticProvider) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
|
|
|
// 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():
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-02-18 08:26:27 -08:00
|
|
|
close(ch)
|
2013-12-11 06:30:27 -08:00
|
|
|
}
|