2017-11-25 05:13:54 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
|
|
|
// 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 (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Appendable returns an Appender.
|
|
|
|
type Appendable interface {
|
|
|
|
Appender() (storage.Appender, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewScrapeManager is the ScrapeManager constructor
|
2017-11-26 07:15:15 -08:00
|
|
|
func NewScrapeManager(logger log.Logger, app Appendable) *ScrapeManager {
|
2017-11-25 05:13:54 -08:00
|
|
|
|
|
|
|
return &ScrapeManager{
|
|
|
|
append: app,
|
|
|
|
logger: logger,
|
|
|
|
actionCh: make(chan func()),
|
|
|
|
scrapeConfigs: make(map[string]*config.ScrapeConfig),
|
|
|
|
scrapePools: make(map[string]*scrapePool),
|
2017-11-26 07:15:15 -08:00
|
|
|
graceShut: make(chan struct{}),
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 04:59:24 -08:00
|
|
|
// ScrapeManager maintains a set of scrape pools and manages start/stop cycles
|
2017-11-25 05:13:54 -08:00
|
|
|
// when receiving new target groups form the discovery manager.
|
|
|
|
type ScrapeManager struct {
|
|
|
|
logger log.Logger
|
|
|
|
append Appendable
|
|
|
|
scrapeConfigs map[string]*config.ScrapeConfig
|
|
|
|
scrapePools map[string]*scrapePool
|
|
|
|
actionCh chan func()
|
2017-11-26 07:15:15 -08:00
|
|
|
graceShut chan struct{}
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts background processing to handle target updates and reload the scraping loops.
|
|
|
|
func (m *ScrapeManager) Run(tsets <-chan map[string][]*config.TargetGroup) error {
|
|
|
|
level.Info(m.logger).Log("msg", "Starting scrape manager...")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case f := <-m.actionCh:
|
|
|
|
f()
|
|
|
|
case ts := <-tsets:
|
2017-11-29 15:56:57 -08:00
|
|
|
if err := m.reload(ts); err != nil {
|
|
|
|
level.Error(m.logger).Log("msg", "error reloading the scrape manager", "err", err)
|
|
|
|
}
|
2017-11-26 07:15:15 -08:00
|
|
|
case <-m.graceShut:
|
|
|
|
return nil
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:15:15 -08:00
|
|
|
// Stop cancels all running scrape pools and blocks until all have exited.
|
|
|
|
func (m *ScrapeManager) Stop() {
|
|
|
|
for _, sp := range m.scrapePools {
|
|
|
|
sp.stop()
|
|
|
|
}
|
|
|
|
close(m.graceShut)
|
|
|
|
}
|
|
|
|
|
2017-11-25 05:13:54 -08:00
|
|
|
// ApplyConfig resets the manager's target providers and job configurations as defined by the new cfg.
|
|
|
|
func (m *ScrapeManager) ApplyConfig(cfg *config.Config) error {
|
|
|
|
done := make(chan struct{})
|
|
|
|
m.actionCh <- func() {
|
|
|
|
for _, scfg := range cfg.ScrapeConfigs {
|
|
|
|
m.scrapeConfigs[scfg.JobName] = scfg
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
<-done
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetMap returns map of active and dropped targets and their corresponding scrape config job name.
|
2017-12-18 11:41:31 -08:00
|
|
|
func (m *ScrapeManager) TargetMap() map[string][]*Target {
|
|
|
|
targetsMap := make(chan map[string][]*Target)
|
|
|
|
m.actionCh <- func() {
|
|
|
|
targets := make(map[string][]*Target)
|
|
|
|
for jobName, sp := range m.scrapePools {
|
|
|
|
sp.mtx.RLock()
|
|
|
|
for _, t := range sp.targets {
|
|
|
|
targets[jobName] = append(targets[jobName], t)
|
|
|
|
}
|
|
|
|
targets[jobName] = append(targets[jobName], sp.droppedTargets...)
|
|
|
|
sp.mtx.RUnlock()
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2017-12-18 11:41:31 -08:00
|
|
|
targetsMap <- targets
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
2017-12-18 11:41:31 -08:00
|
|
|
return <-targetsMap
|
2017-11-25 05:13:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Targets returns the targets currently being scraped.
|
|
|
|
func (m *ScrapeManager) Targets() []*Target {
|
|
|
|
targets := make(chan []*Target)
|
|
|
|
m.actionCh <- func() {
|
|
|
|
var t []*Target
|
|
|
|
for _, p := range m.scrapePools {
|
|
|
|
p.mtx.RLock()
|
|
|
|
for _, tt := range p.targets {
|
|
|
|
t = append(t, tt)
|
|
|
|
}
|
|
|
|
p.mtx.RUnlock()
|
|
|
|
}
|
|
|
|
targets <- t
|
|
|
|
}
|
|
|
|
return <-targets
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ScrapeManager) reload(t map[string][]*config.TargetGroup) error {
|
|
|
|
for tsetName, tgroup := range t {
|
|
|
|
scrapeConfig, ok := m.scrapeConfigs[tsetName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("target set '%v' doesn't have valid config", tsetName)
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:15:15 -08:00
|
|
|
// Scrape pool doesn't exist so start a new one.
|
2017-11-25 05:13:54 -08:00
|
|
|
existing, ok := m.scrapePools[tsetName]
|
|
|
|
if !ok {
|
2017-11-26 07:15:15 -08:00
|
|
|
sp := newScrapePool(scrapeConfig, m.append, log.With(m.logger, "scrape_pool", tsetName))
|
2017-11-25 05:13:54 -08:00
|
|
|
m.scrapePools[tsetName] = sp
|
|
|
|
sp.Sync(tgroup)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
existing.Sync(tgroup)
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:15:15 -08:00
|
|
|
// Cleanup - check the config and cancel the scrape loops if it don't exist in the scrape config.
|
2017-11-25 05:13:54 -08:00
|
|
|
jobs := make(map[string]struct{})
|
|
|
|
|
|
|
|
for k := range m.scrapeConfigs {
|
|
|
|
jobs[k] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, sp := range m.scrapePools {
|
|
|
|
if _, ok := jobs[name]; !ok {
|
|
|
|
sp.stop()
|
|
|
|
delete(m.scrapePools, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|