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 (
|
2014-11-11 23:38:28 +00:00
|
|
|
"sync"
|
2014-12-26 12:37:30 +00:00
|
|
|
|
2015-10-03 08:21:43 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2016-02-17 18:33:17 +00:00
|
|
|
"golang.org/x/net/context"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-11-21 14:59:03 +00:00
|
|
|
"github.com/prometheus/prometheus/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
|
|
|
// 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 21:54:51 +00:00
|
|
|
appender storage.SampleAppender
|
|
|
|
scrapeConfigs []*config.ScrapeConfig
|
2016-02-17 18:33:17 +00:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
wg sync.WaitGroup
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2016-02-19 21:54:51 +00:00
|
|
|
// Set of unqiue targets by scrape configuration.
|
|
|
|
targetSets map[string]*targetSet
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 11:48:30 +00:00
|
|
|
type targetSet struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
|
|
|
|
ts *discovery.TargetSet
|
|
|
|
sp *scrapePool
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
// NewTargetManager creates a new TargetManager.
|
2016-02-17 18:33:17 +00:00
|
|
|
func NewTargetManager(app storage.SampleAppender) *TargetManager {
|
|
|
|
return &TargetManager{
|
2016-02-19 21:54:51 +00:00
|
|
|
appender: app,
|
|
|
|
targetSets: map[string]*targetSet{},
|
2015-08-10 14:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.mtx.Lock()
|
2016-02-19 21:54:51 +00:00
|
|
|
|
2016-02-22 11:42:11 +00:00
|
|
|
tm.ctx, tm.cancel = context.WithCancel(context.Background())
|
|
|
|
tm.reload()
|
2016-02-17 18:33:17 +00:00
|
|
|
|
|
|
|
tm.mtx.Unlock()
|
2016-02-19 21:54:51 +00:00
|
|
|
|
2016-02-17 18:33:17 +00:00
|
|
|
tm.wg.Wait()
|
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() {
|
2016-02-17 18:33:17 +00:00
|
|
|
log.Infoln("Stopping target manager...")
|
2015-05-27 10:32:39 +00:00
|
|
|
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.Lock()
|
2016-02-19 21:54:51 +00: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 18:33:17 +00:00
|
|
|
// Started inserts will be finished before terminating.
|
|
|
|
tm.cancel()
|
|
|
|
tm.mtx.Unlock()
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2016-02-19 21:54:51 +00:00
|
|
|
// Wait for all scrape inserts to complete.
|
2016-02-17 18:33:17 +00:00
|
|
|
tm.wg.Wait()
|
2016-02-19 21:54:51 +00:00
|
|
|
|
|
|
|
log.Debugln("Target manager stopped")
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 11:42:11 +00: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 {
|
2016-11-22 11:48:30 +00:00
|
|
|
ctx, cancel := context.WithCancel(tm.ctx)
|
|
|
|
ts = &targetSet{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
sp: newScrapePool(ctx, scfg, tm.appender),
|
|
|
|
}
|
|
|
|
ts.ts = discovery.NewTargetSet(ts.sp)
|
|
|
|
|
2016-02-22 11:42:11 +00:00
|
|
|
tm.targetSets[scfg.JobName] = ts
|
|
|
|
|
|
|
|
tm.wg.Add(1)
|
|
|
|
|
|
|
|
go func(ts *targetSet) {
|
2016-11-22 11:48:30 +00:00
|
|
|
// Run target set, which blocks until its context is canceled.
|
|
|
|
// Gracefully shut down pending scrapes in the scrape pool afterwards.
|
|
|
|
ts.ts.Run(ctx)
|
|
|
|
ts.sp.stop()
|
2016-02-22 11:42:11 +00:00
|
|
|
tm.wg.Done()
|
|
|
|
}(ts)
|
2016-02-23 12:34:24 +00:00
|
|
|
} else {
|
2016-11-22 11:48:30 +00:00
|
|
|
ts.sp.reload(scfg)
|
2016-02-22 11:42:11 +00:00
|
|
|
}
|
2016-11-23 11:41:19 +00:00
|
|
|
ts.ts.UpdateProviders(discovery.ProvidersFromConfig(scfg.ServiceDiscoveryConfig))
|
2016-02-22 11:42:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 11:48:30 +00:00
|
|
|
// Remove old target sets. Waiting for scrape pools to complete pending
|
|
|
|
// scrape inserts is already guaranteed by the goroutine that started the target set.
|
2016-02-22 11:42:11 +00:00
|
|
|
for name, ts := range tm.targetSets {
|
|
|
|
if _, ok := jobs[name]; !ok {
|
|
|
|
ts.cancel()
|
|
|
|
delete(tm.targetSets, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 12:28:58 +00:00
|
|
|
// Targets returns the targets currently being scraped bucketed by their job name.
|
2017-01-06 17:43:41 +00:00
|
|
|
func (tm *TargetManager) Targets() []*Target {
|
2015-08-10 14:44:32 +00:00
|
|
|
tm.mtx.RLock()
|
|
|
|
defer tm.mtx.RUnlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2017-01-06 17:43:41 +00:00
|
|
|
targets := []*Target{}
|
2016-02-19 21:54:51 +00:00
|
|
|
for _, ps := range tm.targetSets {
|
2016-11-22 11:48:30 +00:00
|
|
|
ps.sp.mtx.RLock()
|
2016-02-23 13:37:25 +00:00
|
|
|
|
2016-11-22 11:48:30 +00:00
|
|
|
for _, t := range ps.sp.targets {
|
2017-01-06 17:43:41 +00:00
|
|
|
targets = append(targets, t)
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2016-02-23 13:37:25 +00:00
|
|
|
|
2016-11-22 11:48:30 +00:00
|
|
|
ps.sp.mtx.RUnlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2016-12-02 12:28:58 +00:00
|
|
|
|
|
|
|
return targets
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 14:24:54 +00:00
|
|
|
func (tm *TargetManager) ApplyConfig(cfg *config.Config) error {
|
2016-02-17 18:33:17 +00:00
|
|
|
tm.mtx.Lock()
|
2016-02-22 11:42:11 +00:00
|
|
|
defer tm.mtx.Unlock()
|
|
|
|
|
2016-02-19 21:54:51 +00:00
|
|
|
tm.scrapeConfigs = cfg.ScrapeConfigs
|
2015-05-27 10:32:39 +00:00
|
|
|
|
2016-02-22 11:42:11 +00:00
|
|
|
if tm.ctx != nil {
|
|
|
|
tm.reload()
|
|
|
|
}
|
2016-07-11 14:24:54 +00:00
|
|
|
return nil
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|