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-09-05 05:17:10 -07:00
|
|
|
"net"
|
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"
|
2016-08-09 03:36:23 -07:00
|
|
|
"github.com/prometheus/prometheus/relabel"
|
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.
|
2016-07-11 07:24:54 -07:00
|
|
|
func (tm *TargetManager) ApplyConfig(cfg *config.Config) error {
|
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()
|
|
|
|
}
|
2016-07-11 07:24:54 -07:00
|
|
|
return nil
|
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-08-22 10:25:33 -07:00
|
|
|
tgroups map[string][]*Target
|
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-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
|
|
|
|
2016-08-22 10:25:33 -07:00
|
|
|
// (Re-)create a fresh tgroups map to not keep stale targets around. We
|
|
|
|
// will retrieve all targets below anyway, so cleaning up everything is
|
|
|
|
// safe and doesn't inflict any additional cost.
|
|
|
|
ts.tgroups = map[string][]*Target{}
|
|
|
|
|
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-06-14 05:04:22 -07:00
|
|
|
// Just signal that there are initial sets to sync now. Actual syncing must only
|
|
|
|
// happen in the runScraping loop.
|
|
|
|
select {
|
|
|
|
case ts.syncCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2016-02-19 13:54:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-29 05:57:28 -07:00
|
|
|
m, err := discovery.NewMarathon(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot create Marathon discovery: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
app("marathon", i, m)
|
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-06-28 06:15:37 -07:00
|
|
|
for i, c := range cfg.GCESDConfigs {
|
|
|
|
gced, err := discovery.NewGCEDiscovery(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot initialize GCE discovery: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
app("gce", i, gced)
|
|
|
|
}
|
2016-04-06 20:47:02 -07:00
|
|
|
for i, c := range cfg.AzureSDConfigs {
|
|
|
|
app("azure", i, discovery.NewAzureDiscovery(c))
|
|
|
|
}
|
2016-06-08 06:54:04 -07:00
|
|
|
if len(cfg.StaticConfigs) > 0 {
|
|
|
|
app("static", 0, NewStaticProvider(cfg.StaticConfigs))
|
2015-08-07 04:18:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return providers
|
|
|
|
}
|
|
|
|
|
2016-09-05 05:17:10 -07:00
|
|
|
// populateLabels builds a label set from the given label set and scrape configuration.
|
|
|
|
// It returns a label set before relabeling was applied as the second return value.
|
|
|
|
// Returns a nil label set if the target is dropped during relabeling.
|
|
|
|
func populateLabels(lset model.LabelSet, cfg *config.ScrapeConfig) (res, orig model.LabelSet, err error) {
|
|
|
|
if _, ok := lset[model.AddressLabel]; !ok {
|
|
|
|
return nil, nil, fmt.Errorf("no address")
|
|
|
|
}
|
|
|
|
// Copy labels into the labelset for the target if they are not
|
|
|
|
// set already. Apply the labelsets in order of decreasing precedence.
|
|
|
|
scrapeLabels := model.LabelSet{
|
|
|
|
model.SchemeLabel: model.LabelValue(cfg.Scheme),
|
|
|
|
model.MetricsPathLabel: model.LabelValue(cfg.MetricsPath),
|
|
|
|
model.JobLabel: model.LabelValue(cfg.JobName),
|
|
|
|
}
|
|
|
|
for ln, lv := range scrapeLabels {
|
|
|
|
if _, ok := lset[ln]; !ok {
|
|
|
|
lset[ln] = lv
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-09-05 05:17:10 -07:00
|
|
|
}
|
|
|
|
// Encode scrape query parameters as labels.
|
|
|
|
for k, v := range cfg.Params {
|
|
|
|
if len(v) > 0 {
|
|
|
|
lset[model.LabelName(model.ParamLabelPrefix+k)] = model.LabelValue(v[0])
|
2015-04-28 15:08:58 -07:00
|
|
|
}
|
2016-09-05 05:17:10 -07:00
|
|
|
}
|
2015-04-28 15:08:58 -07:00
|
|
|
|
2016-09-05 05:17:10 -07:00
|
|
|
preRelabelLabels := lset
|
|
|
|
lset = relabel.Process(lset, cfg.RelabelConfigs...)
|
2015-06-05 13:42:39 -07:00
|
|
|
|
2016-09-05 05:17:10 -07:00
|
|
|
// Check if the target was dropped.
|
|
|
|
if lset == nil {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2016-08-09 03:36:23 -07:00
|
|
|
|
2016-09-05 05:17:10 -07:00
|
|
|
// addPort checks whether we should add a default port to the address.
|
|
|
|
// If the address is not valid, we don't append a port either.
|
|
|
|
addPort := func(s string) bool {
|
|
|
|
// If we can split, a port exists and we don't have to add one.
|
|
|
|
if _, _, err := net.SplitHostPort(s); err == nil {
|
|
|
|
return false
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-09-05 05:17:10 -07:00
|
|
|
// If adding a port makes it valid, the previous error
|
|
|
|
// was not due to an invalid address and we can append a port.
|
|
|
|
_, _, err := net.SplitHostPort(s + ":1234")
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
// If it's an address with no trailing port, infer it based on the used scheme.
|
|
|
|
if addr := string(lset[model.AddressLabel]); addPort(addr) {
|
|
|
|
// Addresses reaching this point are already wrapped in [] if necessary.
|
|
|
|
switch lset[model.SchemeLabel] {
|
|
|
|
case "http", "":
|
|
|
|
addr = addr + ":80"
|
|
|
|
case "https":
|
|
|
|
addr = addr + ":443"
|
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("invalid scheme: %q", cfg.Scheme)
|
2015-11-08 03:42:18 -08:00
|
|
|
}
|
2016-09-05 05:17:10 -07:00
|
|
|
lset[model.AddressLabel] = model.LabelValue(addr)
|
|
|
|
}
|
|
|
|
if err := config.CheckTargetAddress(lset[model.AddressLabel]); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meta labels are deleted after relabelling. Other internal labels propagate to
|
|
|
|
// the target which decides whether they will be part of their label set.
|
|
|
|
for ln := range lset {
|
|
|
|
if strings.HasPrefix(string(ln), model.MetaLabelPrefix) {
|
|
|
|
delete(lset, ln)
|
2015-11-07 06:25:51 -08:00
|
|
|
}
|
2016-09-05 05:17:10 -07:00
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2016-09-05 05:17:10 -07:00
|
|
|
// Default the instance label to the target address.
|
|
|
|
if _, ok := lset[model.InstanceLabel]; !ok {
|
|
|
|
lset[model.InstanceLabel] = lset[model.AddressLabel]
|
|
|
|
}
|
|
|
|
return lset, preRelabelLabels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// targetsFromGroup builds targets based on the given TargetGroup and config.
|
|
|
|
func targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Target, error) {
|
|
|
|
targets := make([]*Target, 0, len(tg.Targets))
|
|
|
|
|
|
|
|
for i, lset := range tg.Targets {
|
|
|
|
// Combine target labels with target group labels.
|
|
|
|
for ln, lv := range tg.Labels {
|
|
|
|
if _, ok := lset[ln]; !ok {
|
|
|
|
lset[ln] = lv
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
}
|
2016-09-05 05:17:10 -07:00
|
|
|
labels, origLabels, err := populateLabels(lset, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("instance %d in group %s: %s", i, tg, err)
|
|
|
|
}
|
|
|
|
if labels != nil {
|
|
|
|
targets = append(targets, NewTarget(labels, origLabels, cfg.Params))
|
2016-02-28 10:56:18 -08:00
|
|
|
}
|
2013-01-07 14:24:26 -08: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
|
|
|
}
|