2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-04 13:41:47 +00: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 10:24:25 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-11-11 23:38:28 +00:00
|
|
|
"sync"
|
2014-12-26 12:37:30 +00:00
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-08-22 07:42:45 +00:00
|
|
|
"github.com/prometheus/log"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-04-20 10:24:25 +00:00
|
|
|
"github.com/prometheus/prometheus/retrieval/discovery"
|
2015-03-15 02:36:15 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2013-01-04 13:41:47 +00:00
|
|
|
)
|
|
|
|
|
2015-04-20 10:24:25 +00: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 10:59:05 +00:00
|
|
|
// detects a potential change, it sends the TargetGroup through its provided channel.
|
2015-04-20 10:24:25 +00: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 18:38:25 +00:00
|
|
|
//
|
|
|
|
// Sources() is guaranteed to be called exactly once before each call to Run().
|
|
|
|
// On a call to Run() implementing types must send a valid target group for each of
|
|
|
|
// the sources they declared in the last call to Sources().
|
2015-04-20 10:24:25 +00:00
|
|
|
type TargetProvider interface {
|
|
|
|
// Sources returns the source identifiers the provider is currently aware of.
|
|
|
|
Sources() []string
|
|
|
|
// 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 14:44:32 +00:00
|
|
|
// On receiving from done Run must return.
|
|
|
|
Run(up chan<- *config.TargetGroup, done <-chan struct{})
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 10:24:25 +00: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 {
|
2015-08-10 14:44:32 +00:00
|
|
|
mtx sync.RWMutex
|
2015-03-15 02:36:15 +00:00
|
|
|
sampleAppender storage.SampleAppender
|
2015-04-20 10:24:25 +00:00
|
|
|
running bool
|
2015-08-10 14:44:32 +00:00
|
|
|
done chan struct{}
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
// Targets by their source ID.
|
2015-05-18 11:14:41 +00:00
|
|
|
targets map[string][]*Target
|
2015-04-25 10:59:05 +00:00
|
|
|
// Providers by the scrape configs they are derived from.
|
|
|
|
providers map[*config.ScrapeConfig][]TargetProvider
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
// NewTargetManager creates a new TargetManager.
|
|
|
|
func NewTargetManager(sampleAppender storage.SampleAppender) *TargetManager {
|
2015-04-20 10:24:25 +00:00
|
|
|
tm := &TargetManager{
|
2015-03-15 02:36:15 +00:00
|
|
|
sampleAppender: sampleAppender,
|
2015-09-01 16:06:22 +00:00
|
|
|
targets: map[string][]*Target{},
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-05-12 14:52:56 +00:00
|
|
|
return tm
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
// merge multiple target group channels into a single output channel.
|
|
|
|
func merge(done <-chan struct{}, cs ...<-chan targetGroupUpdate) <-chan targetGroupUpdate {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
out := make(chan targetGroupUpdate)
|
|
|
|
|
|
|
|
// Start an output goroutine for each input channel in cs. output
|
|
|
|
// copies values from c to out until c or done is closed, then calls
|
|
|
|
// wg.Done.
|
|
|
|
redir := func(c <-chan targetGroupUpdate) {
|
|
|
|
defer wg.Done()
|
|
|
|
for n := range c {
|
|
|
|
select {
|
|
|
|
case out <- n:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(len(cs))
|
|
|
|
for _, c := range cs {
|
|
|
|
go redir(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the out channel if all inbound channels are closed.
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// targetGroupUpdate is a potentially changed/new target group
|
|
|
|
// for the given scrape configuration.
|
|
|
|
type targetGroupUpdate struct {
|
|
|
|
tg *config.TargetGroup
|
|
|
|
scfg *config.ScrapeConfig
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
// Run starts background processing to handle target updates.
|
|
|
|
func (tm *TargetManager) Run() {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Info("Starting target manager...")
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.done = make(chan struct{})
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
sources := map[string]struct{}{}
|
2015-08-10 14:44:32 +00:00
|
|
|
updates := []<-chan targetGroupUpdate{}
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-04-25 10:59:05 +00:00
|
|
|
for scfg, provs := range tm.providers {
|
2015-04-28 22:08:58 +00:00
|
|
|
for _, prov := range provs {
|
2015-08-10 14:44:32 +00:00
|
|
|
// Get an initial set of available sources so we don't remove
|
|
|
|
// target groups from the last run that are still available.
|
2015-04-28 22:08:58 +00:00
|
|
|
for _, src := range prov.Sources() {
|
2015-04-20 10:24:25 +00:00
|
|
|
sources[src] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tgc := make(chan *config.TargetGroup)
|
2015-04-20 10:24:25 +00:00
|
|
|
// Run the target provider after cleanup of the stale targets is done.
|
2015-09-25 16:41:37 +00:00
|
|
|
defer func(prov TargetProvider, tgc chan<- *config.TargetGroup, done <-chan struct{}) {
|
|
|
|
go prov.Run(tgc, done)
|
|
|
|
}(prov, tgc, tm.done)
|
2015-08-10 14:44:32 +00:00
|
|
|
|
|
|
|
tgupc := make(chan targetGroupUpdate)
|
|
|
|
updates = append(updates, tgupc)
|
|
|
|
|
2015-09-25 16:41:37 +00:00
|
|
|
go func(scfg *config.ScrapeConfig, done <-chan struct{}) {
|
2015-08-10 14:44:32 +00:00
|
|
|
defer close(tgupc)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case tg := <-tgc:
|
|
|
|
if tg == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tgupc <- targetGroupUpdate{tg: tg, scfg: scfg}
|
2015-09-25 16:41:37 +00:00
|
|
|
case <-done:
|
2015-08-10 14:44:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 16:41:37 +00:00
|
|
|
}(scfg, tm.done)
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2013-05-16 05:38:31 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
// Merge all channels of incoming target group updates into a single
|
|
|
|
// one and keep applying the updates.
|
|
|
|
go tm.handleUpdates(merge(tm.done, updates...), tm.done)
|
2015-05-27 10:32:39 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.Lock()
|
|
|
|
defer tm.mtx.Unlock()
|
|
|
|
|
|
|
|
// Remove old target groups that are no longer in the set of sources.
|
2015-04-20 10:24:25 +00:00
|
|
|
tm.removeTargets(func(src string) bool {
|
|
|
|
if _, ok := sources[src]; ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
tm.running = true
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 14:42:28 +00:00
|
|
|
// handleUpdates receives target group updates and handles them in the
|
2015-04-20 10:24:25 +00:00
|
|
|
// context of the given job config.
|
2015-08-10 14:44:32 +00:00
|
|
|
func (tm *TargetManager) handleUpdates(ch <-chan targetGroupUpdate, done <-chan struct{}) {
|
|
|
|
for {
|
|
|
|
select {
|
2015-08-24 14:42:28 +00:00
|
|
|
case update, ok := <-ch:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2015-08-10 14:44:32 +00:00
|
|
|
if update.tg == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Debugf("Received potential update for target group %q", update.tg.Source)
|
2014-12-03 17:07:23 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
if err := tm.updateTargetGroup(update.tg, update.scfg); err != nil {
|
|
|
|
log.Errorf("Error updating targets: %s", err)
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
return
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
// Stop all background processing.
|
|
|
|
func (tm *TargetManager) Stop() {
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.RLock()
|
2015-05-12 14:52:56 +00:00
|
|
|
if tm.running {
|
2015-05-27 10:32:39 +00:00
|
|
|
defer tm.stop(true)
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-05-27 10:32:39 +00:00
|
|
|
// Return the lock before calling tm.stop().
|
2015-08-10 14:44:32 +00:00
|
|
|
defer tm.mtx.RUnlock()
|
2015-05-12 14:52:56 +00:00
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
// stop background processing of the target manager. If removeTargets is true,
|
|
|
|
// existing targets will be stopped and removed.
|
|
|
|
func (tm *TargetManager) stop(removeTargets bool) {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Info("Stopping target manager...")
|
|
|
|
defer log.Info("Target manager stopped.")
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
close(tm.done)
|
2015-05-27 10:32:39 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.Lock()
|
|
|
|
defer tm.mtx.Unlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
if removeTargets {
|
|
|
|
tm.removeTargets(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.running = false
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
// removeTargets stops and removes targets for sources where f(source) is true
|
|
|
|
// or if f is nil. This method is not thread-safe.
|
|
|
|
func (tm *TargetManager) removeTargets(f func(string) bool) {
|
|
|
|
if f == nil {
|
|
|
|
f = func(string) bool { return true }
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for src, targets := range tm.targets {
|
|
|
|
if !f(src) {
|
2013-06-11 20:59:27 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
wg.Add(len(targets))
|
|
|
|
for _, target := range targets {
|
2015-05-18 11:14:41 +00:00
|
|
|
go func(t *Target) {
|
2015-04-20 10:24:25 +00:00
|
|
|
t.StopScraper()
|
|
|
|
wg.Done()
|
|
|
|
}(target)
|
|
|
|
}
|
|
|
|
delete(tm.targets, src)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateTargetGroup creates new targets for the group and replaces the old targets
|
|
|
|
// for the source ID.
|
2015-04-25 10:59:05 +00:00
|
|
|
func (tm *TargetManager) updateTargetGroup(tgroup *config.TargetGroup, cfg *config.ScrapeConfig) error {
|
2015-04-20 10:24:25 +00:00
|
|
|
newTargets, err := tm.targetsFromGroup(tgroup, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-11 20:59:27 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.Lock()
|
|
|
|
defer tm.mtx.Unlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-04-25 10:59:05 +00:00
|
|
|
if !tm.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-07 11:18:19 +00:00
|
|
|
oldTargets, ok := tm.targets[tgroup.Source]
|
2015-04-20 10:24:25 +00:00
|
|
|
if ok {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
// Replace the old targets with the new ones while keeping the state
|
|
|
|
// of intersecting targets.
|
|
|
|
for i, tnew := range newTargets {
|
2015-05-18 11:14:41 +00:00
|
|
|
var match *Target
|
2015-04-20 10:24:25 +00:00
|
|
|
for j, told := range oldTargets {
|
|
|
|
if told == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tnew.InstanceIdentifier() == told.InstanceIdentifier() {
|
|
|
|
match = told
|
|
|
|
oldTargets[j] = nil
|
|
|
|
break
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2015-06-05 20:42:39 +00:00
|
|
|
// Update the existing target and discard the new equivalent.
|
2015-04-20 10:24:25 +00:00
|
|
|
// Otherwise start scraping the new target.
|
|
|
|
if match != nil {
|
|
|
|
// Updating is blocked during a scrape. We don't want those wait times
|
|
|
|
// to build up.
|
|
|
|
wg.Add(1)
|
2015-05-18 11:14:41 +00:00
|
|
|
go func(t *Target) {
|
2015-06-05 20:42:39 +00:00
|
|
|
match.Update(cfg, t.fullLabels(), t.metaLabels)
|
2015-04-20 10:24:25 +00:00
|
|
|
wg.Done()
|
|
|
|
}(tnew)
|
|
|
|
newTargets[i] = match
|
|
|
|
} else {
|
|
|
|
go tnew.RunScraper(tm.sampleAppender)
|
2015-03-15 02:36:15 +00:00
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
// Remove all old targets that disappeared.
|
|
|
|
for _, told := range oldTargets {
|
|
|
|
if told != nil {
|
|
|
|
wg.Add(1)
|
2015-05-18 11:14:41 +00:00
|
|
|
go func(t *Target) {
|
2015-04-20 10:24:25 +00:00
|
|
|
t.StopScraper()
|
|
|
|
wg.Done()
|
|
|
|
}(told)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
} else {
|
|
|
|
// The source ID is new, start all target scrapers.
|
|
|
|
for _, tnew := range newTargets {
|
|
|
|
go tnew.RunScraper(tm.sampleAppender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newTargets) > 0 {
|
2015-08-07 11:18:19 +00:00
|
|
|
tm.targets[tgroup.Source] = newTargets
|
2015-04-20 10:24:25 +00:00
|
|
|
} else {
|
2015-08-07 11:18:19 +00:00
|
|
|
delete(tm.targets, tgroup.Source)
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pools returns the targets currently being scraped bucketed by their job name.
|
2015-05-18 11:14:41 +00:00
|
|
|
func (tm *TargetManager) Pools() map[string][]*Target {
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.RLock()
|
|
|
|
defer tm.mtx.RUnlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
pools := map[string][]*Target{}
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
for _, ts := range tm.targets {
|
|
|
|
for _, t := range ts {
|
2015-08-20 15:18:46 +00:00
|
|
|
job := string(t.BaseLabels()[model.JobLabel])
|
2015-04-20 10:24:25 +00:00
|
|
|
pools[job] = append(pools[job], t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 16:44:41 +00:00
|
|
|
// Returns true on success.
|
|
|
|
func (tm *TargetManager) ApplyConfig(cfg *config.Config) bool {
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.RLock()
|
2015-05-27 10:32:39 +00:00
|
|
|
running := tm.running
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.RUnlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-27 10:32:39 +00:00
|
|
|
if running {
|
2015-05-12 14:52:56 +00:00
|
|
|
tm.stop(false)
|
|
|
|
// Even if updating the config failed, we want to continue rather than stop scraping anything.
|
|
|
|
defer tm.Run()
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-04-25 10:59:05 +00:00
|
|
|
providers := map[*config.ScrapeConfig][]TargetProvider{}
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
for _, scfg := range cfg.ScrapeConfigs {
|
2015-08-07 11:18:19 +00:00
|
|
|
providers[scfg] = providersFromConfig(scfg)
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.Lock()
|
|
|
|
defer tm.mtx.Unlock()
|
2015-05-27 10:32:39 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
tm.providers = providers
|
2015-06-02 16:44:41 +00:00
|
|
|
return true
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 11:18:19 +00:00
|
|
|
// prefixedTargetProvider wraps TargetProvider and prefixes source strings
|
|
|
|
// to make the sources unique across a configuration.
|
|
|
|
type prefixedTargetProvider struct {
|
|
|
|
TargetProvider
|
|
|
|
|
|
|
|
job string
|
|
|
|
mechanism string
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *prefixedTargetProvider) prefix(src string) string {
|
|
|
|
return fmt.Sprintf("%s:%s:%d:%s", tp.job, tp.mechanism, tp.idx, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tp *prefixedTargetProvider) Sources() []string {
|
|
|
|
srcs := tp.TargetProvider.Sources()
|
|
|
|
for i, src := range srcs {
|
|
|
|
srcs[i] = tp.prefix(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
return srcs
|
|
|
|
}
|
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
func (tp *prefixedTargetProvider) Run(ch chan<- *config.TargetGroup, done <-chan struct{}) {
|
2015-08-07 11:18:19 +00:00
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
ch2 := make(chan *config.TargetGroup)
|
2015-08-10 14:44:32 +00:00
|
|
|
go tp.TargetProvider.Run(ch2, done)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case tg := <-ch2:
|
|
|
|
if tg == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tg.Source = tp.prefix(tg.Source)
|
|
|
|
ch <- tg
|
|
|
|
}
|
2015-08-07 11:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// providersFromConfig returns all TargetProviders configured in cfg.
|
|
|
|
func providersFromConfig(cfg *config.ScrapeConfig) []TargetProvider {
|
|
|
|
var providers []TargetProvider
|
|
|
|
|
|
|
|
app := func(mech string, i int, tp TargetProvider) {
|
|
|
|
providers = append(providers, &prefixedTargetProvider{
|
|
|
|
job: cfg.JobName,
|
|
|
|
mechanism: mech,
|
|
|
|
idx: i,
|
|
|
|
TargetProvider: tp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cfg.DNSSDConfigs {
|
|
|
|
app("dns", i, discovery.NewDNSDiscovery(c))
|
|
|
|
}
|
|
|
|
for i, c := range cfg.FileSDConfigs {
|
|
|
|
app("file", i, discovery.NewFileDiscovery(c))
|
|
|
|
}
|
|
|
|
for i, c := range cfg.ConsulSDConfigs {
|
|
|
|
app("consul", i, discovery.NewConsulDiscovery(c))
|
|
|
|
}
|
|
|
|
for i, c := range cfg.MarathonSDConfigs {
|
|
|
|
app("marathon", i, discovery.NewMarathonDiscovery(c))
|
|
|
|
}
|
2015-07-18 21:23:58 +00: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 11:18:19 +00:00
|
|
|
for i, c := range cfg.ServersetSDConfigs {
|
|
|
|
app("serverset", i, discovery.NewServersetDiscovery(c))
|
|
|
|
}
|
|
|
|
if len(cfg.TargetGroups) > 0 {
|
|
|
|
app("static", 0, NewStaticProvider(cfg.TargetGroups))
|
|
|
|
}
|
|
|
|
|
|
|
|
return providers
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
// targetsFromGroup builds targets based on the given TargetGroup and config.
|
2015-05-18 11:14:41 +00:00
|
|
|
func (tm *TargetManager) targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Target, error) {
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.RLock()
|
|
|
|
defer tm.mtx.RUnlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
targets := make([]*Target, 0, len(tg.Targets))
|
2015-04-20 10:24:25 +00:00
|
|
|
for i, labels := range tg.Targets {
|
2015-08-20 15:18:46 +00:00
|
|
|
addr := string(labels[model.AddressLabel])
|
2015-05-13 10:03:31 +00:00
|
|
|
// If no port was provided, infer it based on the used scheme.
|
|
|
|
if !strings.Contains(addr, ":") {
|
|
|
|
switch cfg.Scheme {
|
|
|
|
case "http":
|
|
|
|
addr = fmt.Sprintf("%s:80", addr)
|
|
|
|
case "https":
|
|
|
|
addr = fmt.Sprintf("%s:443", addr)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("targetsFromGroup: invalid scheme %q", cfg.Scheme))
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
2015-05-13 10:03:31 +00:00
|
|
|
}
|
2015-07-05 19:25:45 +00:00
|
|
|
for k, v := range cfg.Params {
|
|
|
|
if len(v) > 0 {
|
2015-08-20 15:18:46 +00:00
|
|
|
labels[model.LabelName(model.ParamLabelPrefix+k)] = model.LabelValue(v[0])
|
2015-07-05 19:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 10:59:05 +00: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 15:18:46 +00:00
|
|
|
labelsets := []model.LabelSet{
|
2015-04-25 10:59:05 +00:00
|
|
|
tg.Labels,
|
2015-05-24 18:48:56 +00:00
|
|
|
{
|
2015-08-20 15:18:46 +00:00
|
|
|
model.SchemeLabel: model.LabelValue(cfg.Scheme),
|
|
|
|
model.MetricsPathLabel: model.LabelValue(cfg.MetricsPath),
|
|
|
|
model.JobLabel: model.LabelValue(cfg.JobName),
|
2015-05-07 08:55:03 +00:00
|
|
|
},
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-04-25 10:59:05 +00:00
|
|
|
for _, lset := range labelsets {
|
|
|
|
for ln, lv := range lset {
|
|
|
|
if _, ok := labels[ln]; !ok {
|
|
|
|
labels[ln] = lv
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-04-25 10:59:05 +00:00
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
if _, ok := labels[model.AddressLabel]; !ok {
|
2015-04-28 22:08:58 +00:00
|
|
|
return nil, fmt.Errorf("instance %d in target group %s has no address", i, tg)
|
|
|
|
}
|
|
|
|
|
2015-06-05 20:42:39 +00:00
|
|
|
preRelabelLabels := labels
|
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
labels, err := Relabel(labels, cfg.RelabelConfigs...)
|
2015-04-28 22:08:58 +00:00
|
|
|
if err != nil {
|
2015-05-12 14:52:56 +00:00
|
|
|
return nil, fmt.Errorf("error while relabeling instance %d in target group %s: %s", i, tg, err)
|
2015-04-28 22:08:58 +00:00
|
|
|
}
|
|
|
|
// Check if the target was dropped.
|
|
|
|
if labels == nil {
|
|
|
|
continue
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
for ln := range labels {
|
2015-04-25 10:59:05 +00: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 15:18:46 +00:00
|
|
|
if strings.HasPrefix(string(ln), model.MetaLabelPrefix) {
|
2015-04-20 10:24:25 +00:00
|
|
|
delete(labels, ln)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-05 20:42:39 +00:00
|
|
|
tr := NewTarget(cfg, labels, preRelabelLabels)
|
2015-04-28 22:08:58 +00:00
|
|
|
targets = append(targets, tr)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2015-04-25 10:59:05 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
return targets, nil
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-02-22 20:07:35 +00:00
|
|
|
|
2015-04-20 10:24:25 +00: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 08:55:03 +00:00
|
|
|
func NewStaticProvider(groups []*config.TargetGroup) *StaticProvider {
|
2015-04-20 10:24:25 +00:00
|
|
|
for i, tg := range groups {
|
2015-08-07 11:18:19 +00:00
|
|
|
tg.Source = fmt.Sprintf("%d", i)
|
2015-05-07 08:55:03 +00:00
|
|
|
}
|
|
|
|
return &StaticProvider{
|
|
|
|
TargetGroups: groups,
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2015-08-10 14:44:32 +00:00
|
|
|
func (sd *StaticProvider) Run(ch chan<- *config.TargetGroup, done <-chan struct{}) {
|
|
|
|
defer close(ch)
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
for _, tg := range sd.TargetGroups {
|
2015-08-10 14:44:32 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case ch <- tg:
|
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-08-18 09:51:53 +00:00
|
|
|
<-done
|
2013-12-11 14:30:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 13:07:27 +00:00
|
|
|
// Sources returns the provider's sources.
|
2015-04-20 10:24:25 +00:00
|
|
|
func (sd *StaticProvider) Sources() (srcs []string) {
|
|
|
|
for _, tg := range sd.TargetGroups {
|
|
|
|
srcs = append(srcs, tg.Source)
|
2014-12-03 17:07:23 +00:00
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
return srcs
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|