2013-01-04 05:41:47 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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 (
|
2013-01-27 09:49:45 -08:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/model"
|
|
|
|
"github.com/prometheus/prometheus/retrieval/format"
|
2013-01-04 05:41:47 -08:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TargetManager interface {
|
|
|
|
acquire()
|
|
|
|
release()
|
2013-04-30 11:20:14 -07:00
|
|
|
AddTarget(job config.JobConfig, t Target, defaultScrapeInterval time.Duration)
|
|
|
|
ReplaceTargets(job config.JobConfig, newTargets []Target, defaultScrapeInterval time.Duration)
|
2013-01-13 01:46:55 -08:00
|
|
|
Remove(t Target)
|
2013-04-30 11:20:14 -07:00
|
|
|
AddTargetsFromConfig(config config.Config)
|
2013-02-22 12:07:35 -08:00
|
|
|
Pools() map[string]*TargetPool
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type targetManager struct {
|
|
|
|
requestAllowance chan bool
|
2013-02-22 12:07:35 -08:00
|
|
|
poolsByJob map[string]*TargetPool
|
2013-01-22 09:37:01 -08:00
|
|
|
results chan format.Result
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-01-22 09:37:01 -08:00
|
|
|
func NewTargetManager(results chan format.Result, requestAllowance int) TargetManager {
|
2013-01-15 08:06:17 -08:00
|
|
|
return &targetManager{
|
2013-01-04 05:41:47 -08:00
|
|
|
requestAllowance: make(chan bool, requestAllowance),
|
|
|
|
results: results,
|
2013-02-22 12:07:35 -08:00
|
|
|
poolsByJob: make(map[string]*TargetPool),
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 08:06:17 -08:00
|
|
|
func (m *targetManager) acquire() {
|
2013-01-04 05:41:47 -08:00
|
|
|
m.requestAllowance <- true
|
|
|
|
}
|
|
|
|
|
2013-01-15 08:06:17 -08:00
|
|
|
func (m *targetManager) release() {
|
2013-01-04 05:41:47 -08:00
|
|
|
<-m.requestAllowance
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:38:31 -07:00
|
|
|
func (m *targetManager) TargetPoolForJob(job config.JobConfig, defaultScrapeInterval time.Duration) *TargetPool {
|
2013-04-30 11:20:14 -07:00
|
|
|
targetPool, ok := m.poolsByJob[job.GetName()]
|
2013-01-04 05:41:47 -08:00
|
|
|
|
|
|
|
if !ok {
|
2013-01-15 08:06:17 -08:00
|
|
|
targetPool = NewTargetPool(m)
|
2013-04-30 11:20:14 -07:00
|
|
|
log.Printf("Pool for job %s does not exist; creating and starting...", job.GetName())
|
2013-02-22 12:07:35 -08:00
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
interval := job.ScrapeInterval()
|
|
|
|
m.poolsByJob[job.GetName()] = targetPool
|
2013-05-15 22:38:31 -07:00
|
|
|
// BUG(all): Investigate whether this auto-goroutine creation is desired.
|
2013-02-22 12:07:35 -08:00
|
|
|
go targetPool.Run(m.results, interval)
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2013-05-15 22:38:31 -07:00
|
|
|
|
|
|
|
return targetPool
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
func (m *targetManager) AddTarget(job config.JobConfig, t Target, defaultScrapeInterval time.Duration) {
|
2013-02-22 12:07:35 -08:00
|
|
|
targetPool := m.TargetPoolForJob(job, defaultScrapeInterval)
|
|
|
|
targetPool.AddTarget(t)
|
2013-04-30 11:20:14 -07:00
|
|
|
m.poolsByJob[job.GetName()] = targetPool
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
func (m *targetManager) ReplaceTargets(job config.JobConfig, newTargets []Target, defaultScrapeInterval time.Duration) {
|
2013-02-22 12:07:35 -08:00
|
|
|
targetPool := m.TargetPoolForJob(job, defaultScrapeInterval)
|
|
|
|
targetPool.replaceTargets(newTargets)
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
func (m targetManager) Remove(t Target) {
|
2013-01-04 05:41:47 -08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
func (m *targetManager) AddTargetsFromConfig(config config.Config) {
|
|
|
|
for _, job := range config.Jobs() {
|
|
|
|
for _, targetGroup := range job.TargetGroup {
|
2013-01-07 14:24:26 -08:00
|
|
|
baseLabels := model.LabelSet{
|
2013-04-30 11:20:14 -07:00
|
|
|
model.JobLabel: model.LabelValue(job.GetName()),
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
if targetGroup.Labels != nil {
|
|
|
|
for _, label := range targetGroup.Labels.Label {
|
|
|
|
baseLabels[model.LabelName(label.GetName())] = model.LabelValue(label.GetValue())
|
|
|
|
}
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
for _, endpoint := range targetGroup.Target {
|
2013-02-22 12:07:35 -08:00
|
|
|
target := NewTarget(endpoint, time.Second*5, baseLabels)
|
2013-04-30 11:20:14 -07:00
|
|
|
m.AddTarget(job, target, config.ScrapeInterval())
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-22 12:07:35 -08:00
|
|
|
|
|
|
|
// XXX: Not really thread-safe. Only used in /status page for now.
|
|
|
|
func (m *targetManager) Pools() map[string]*TargetPool {
|
|
|
|
return m.poolsByJob
|
|
|
|
}
|