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 (
|
2014-11-11 15:38:28 -08:00
|
|
|
"sync"
|
2014-12-26 04:37:30 -08:00
|
|
|
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
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-11-21 06:59:03 -08:00
|
|
|
"github.com/prometheus/prometheus/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
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-11-22 03:48:30 -08:00
|
|
|
type targetSet struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
|
|
|
|
ts *discovery.TargetSet
|
|
|
|
sp *scrapePool
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-11-22 03:48:30 -08: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 03:42:11 -08:00
|
|
|
tm.targetSets[scfg.JobName] = ts
|
|
|
|
|
|
|
|
tm.wg.Add(1)
|
|
|
|
|
|
|
|
go func(ts *targetSet) {
|
2016-11-22 03:48:30 -08: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 03:42:11 -08:00
|
|
|
tm.wg.Done()
|
|
|
|
}(ts)
|
2016-02-23 04:34:24 -08:00
|
|
|
} else {
|
2016-11-22 03:48:30 -08:00
|
|
|
ts.sp.reload(scfg)
|
2016-02-22 03:42:11 -08:00
|
|
|
}
|
2016-11-23 03:41:19 -08:00
|
|
|
ts.ts.UpdateProviders(discovery.ProvidersFromConfig(scfg.ServiceDiscoveryConfig))
|
2016-02-22 03:42:11 -08:00
|
|
|
}
|
|
|
|
|
2016-11-22 03:48:30 -08: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 03:42:11 -08:00
|
|
|
for name, ts := range tm.targetSets {
|
|
|
|
if _, ok := jobs[name]; !ok {
|
|
|
|
ts.cancel()
|
|
|
|
delete(tm.targetSets, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 13:45:18 -07:00
|
|
|
// Targets returns the targets currently being scraped.
|
2017-01-06 09:43:41 -08:00
|
|
|
func (tm *TargetManager) Targets() []*Target {
|
2015-08-10 07:44:32 -07:00
|
|
|
tm.mtx.RLock()
|
|
|
|
defer tm.mtx.RUnlock()
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2017-01-06 09:43:41 -08:00
|
|
|
targets := []*Target{}
|
2016-02-19 13:54:51 -08:00
|
|
|
for _, ps := range tm.targetSets {
|
2016-11-22 03:48:30 -08:00
|
|
|
ps.sp.mtx.RLock()
|
2016-02-23 05:37:25 -08:00
|
|
|
|
2016-11-22 03:48:30 -08:00
|
|
|
for _, t := range ps.sp.targets {
|
2017-01-06 09:43:41 -08:00
|
|
|
targets = append(targets, t)
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-02-23 05:37:25 -08:00
|
|
|
|
2016-11-22 03:48:30 -08:00
|
|
|
ps.sp.mtx.RUnlock()
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2016-12-02 04:28:58 -08:00
|
|
|
|
|
|
|
return targets
|
2015-04-20 03:24:25 -07: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 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
|
|
|
}
|