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
|
|
|
|
2013-08-12 15:18:02 +00:00
|
|
|
"github.com/golang/glog"
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-03-15 02:36:15 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2013-01-04 13:41:47 +00:00
|
|
|
)
|
|
|
|
|
2014-12-03 17:07:23 +00:00
|
|
|
// TargetManager manages all scrape targets. All methods are goroutine-safe.
|
2013-01-04 13:41:47 +00:00
|
|
|
type TargetManager interface {
|
2013-06-11 15:54:58 +00:00
|
|
|
AddTarget(job config.JobConfig, t Target)
|
|
|
|
ReplaceTargets(job config.JobConfig, newTargets []Target)
|
2013-01-13 09:46:55 +00:00
|
|
|
Remove(t Target)
|
2013-04-30 18:20:14 +00:00
|
|
|
AddTargetsFromConfig(config config.Config)
|
2013-12-11 14:30:27 +00:00
|
|
|
Stop()
|
2014-12-03 17:07:23 +00:00
|
|
|
Pools() map[string]*TargetPool // Returns a copy of the name -> TargetPool mapping.
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type targetManager struct {
|
2015-03-15 02:36:15 +00:00
|
|
|
sync.Mutex // Protects poolByJob.
|
|
|
|
globalLabels clientmodel.LabelSet
|
|
|
|
sampleAppender storage.SampleAppender
|
|
|
|
poolsByJob map[string]*TargetPool
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// NewTargetManager returns a newly initialized TargetManager ready to use.
|
2015-03-15 02:36:15 +00:00
|
|
|
func NewTargetManager(sampleAppender storage.SampleAppender, globalLabels clientmodel.LabelSet) TargetManager {
|
2013-01-15 16:06:17 +00:00
|
|
|
return &targetManager{
|
2015-03-15 02:36:15 +00:00
|
|
|
sampleAppender: sampleAppender,
|
|
|
|
globalLabels: globalLabels,
|
|
|
|
poolsByJob: make(map[string]*TargetPool),
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 17:07:23 +00:00
|
|
|
func (m *targetManager) targetPoolForJob(job config.JobConfig) *TargetPool {
|
2013-04-30 18:20:14 +00:00
|
|
|
targetPool, ok := m.poolsByJob[job.GetName()]
|
2013-01-04 13:41:47 +00:00
|
|
|
|
|
|
|
if !ok {
|
2014-12-10 15:16:49 +00:00
|
|
|
var provider TargetProvider
|
2013-06-11 20:59:27 +00:00
|
|
|
if job.SdName != nil {
|
2015-03-15 02:36:15 +00:00
|
|
|
provider = NewSdTargetProvider(job, m.globalLabels)
|
2013-06-11 20:59:27 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 18:31:11 +00:00
|
|
|
interval := job.ScrapeInterval()
|
2015-03-15 02:36:15 +00:00
|
|
|
targetPool = NewTargetPool(provider, m.sampleAppender, interval)
|
2013-08-12 15:18:02 +00:00
|
|
|
glog.Infof("Pool for job %s does not exist; creating and starting...", job.GetName())
|
2013-02-22 20:07:35 +00:00
|
|
|
|
2013-04-30 18:20:14 +00:00
|
|
|
m.poolsByJob[job.GetName()] = targetPool
|
2014-07-29 18:31:11 +00:00
|
|
|
go targetPool.Run()
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2013-05-16 05:38:31 +00:00
|
|
|
|
|
|
|
return targetPool
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 15:54:58 +00:00
|
|
|
func (m *targetManager) AddTarget(job config.JobConfig, t Target) {
|
2014-12-03 17:07:23 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
targetPool := m.targetPoolForJob(job)
|
2013-02-22 20:07:35 +00:00
|
|
|
targetPool.AddTarget(t)
|
2013-04-30 18:20:14 +00:00
|
|
|
m.poolsByJob[job.GetName()] = targetPool
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2013-06-11 15:54:58 +00:00
|
|
|
func (m *targetManager) ReplaceTargets(job config.JobConfig, newTargets []Target) {
|
2014-12-03 17:07:23 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
targetPool := m.targetPoolForJob(job)
|
2014-07-29 18:31:11 +00:00
|
|
|
targetPool.ReplaceTargets(newTargets)
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 17:07:23 +00:00
|
|
|
func (m *targetManager) Remove(t Target) {
|
2013-01-04 13:41:47 +00:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2013-04-30 18:20:14 +00:00
|
|
|
func (m *targetManager) AddTargetsFromConfig(config config.Config) {
|
|
|
|
for _, job := range config.Jobs() {
|
2013-06-11 20:59:27 +00:00
|
|
|
if job.SdName != nil {
|
2014-12-03 17:07:23 +00:00
|
|
|
m.Lock()
|
|
|
|
m.targetPoolForJob(job)
|
|
|
|
m.Unlock()
|
2013-06-11 20:59:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-04-30 18:20:14 +00:00
|
|
|
for _, targetGroup := range job.TargetGroup {
|
2013-06-25 12:02:27 +00:00
|
|
|
baseLabels := clientmodel.LabelSet{
|
|
|
|
clientmodel.JobLabel: clientmodel.LabelValue(job.GetName()),
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2015-03-15 02:36:15 +00:00
|
|
|
for n, v := range m.globalLabels {
|
|
|
|
baseLabels[n] = v
|
|
|
|
}
|
2013-04-30 18:20:14 +00:00
|
|
|
if targetGroup.Labels != nil {
|
|
|
|
for _, label := range targetGroup.Labels.Label {
|
2013-06-25 12:02:27 +00:00
|
|
|
baseLabels[clientmodel.LabelName(label.GetName())] = clientmodel.LabelValue(label.GetValue())
|
2013-04-30 18:20:14 +00:00
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 18:20:14 +00:00
|
|
|
for _, endpoint := range targetGroup.Target {
|
2013-08-16 16:17:48 +00:00
|
|
|
target := NewTarget(endpoint, job.ScrapeTimeout(), baseLabels)
|
2013-06-11 15:54:58 +00:00
|
|
|
m.AddTarget(job, target)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-22 20:07:35 +00:00
|
|
|
|
2013-12-11 14:30:27 +00:00
|
|
|
func (m *targetManager) Stop() {
|
2014-12-03 17:07:23 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2014-11-11 23:38:28 +00:00
|
|
|
glog.Info("Stopping target manager...")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for j, p := range m.poolsByJob {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(j string, p *TargetPool) {
|
|
|
|
defer wg.Done()
|
|
|
|
glog.Infof("Stopping target pool %q...", j)
|
|
|
|
p.Stop()
|
|
|
|
glog.Infof("Target pool %q stopped.", j)
|
|
|
|
}(j, p)
|
2013-12-11 14:30:27 +00:00
|
|
|
}
|
2014-11-11 23:38:28 +00:00
|
|
|
wg.Wait()
|
|
|
|
glog.Info("Target manager stopped.")
|
2013-12-11 14:30:27 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 20:07:35 +00:00
|
|
|
func (m *targetManager) Pools() map[string]*TargetPool {
|
2014-12-03 17:07:23 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
result := make(map[string]*TargetPool, len(m.poolsByJob))
|
|
|
|
for k, v := range m.poolsByJob {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
return result
|
2013-02-22 20:07:35 +00:00
|
|
|
}
|