prometheus/retrieval/targetmanager.go

94 lines
2.4 KiB
Go
Raw Normal View History

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 (
"container/heap"
"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()
Add(t Target)
Remove(t Target)
AddTargetsFromConfig(config *config.Config)
2013-01-04 05:41:47 -08:00
}
type targetManager struct {
requestAllowance chan bool
pools map[time.Duration]*TargetPool
results chan format.Result
2013-01-04 05:41:47 -08:00
}
func NewTargetManager(results chan format.Result, requestAllowance int) TargetManager {
return &targetManager{
2013-01-04 05:41:47 -08:00
requestAllowance: make(chan bool, requestAllowance),
results: results,
pools: make(map[time.Duration]*TargetPool),
2013-01-04 05:41:47 -08:00
}
}
func (m *targetManager) acquire() {
2013-01-04 05:41:47 -08:00
m.requestAllowance <- true
}
func (m *targetManager) release() {
2013-01-04 05:41:47 -08:00
<-m.requestAllowance
}
func (m *targetManager) Add(t Target) {
targetPool, ok := m.pools[t.Interval()]
2013-01-04 05:41:47 -08:00
if !ok {
targetPool = NewTargetPool(m)
log.Printf("Pool %s does not exist; creating and starting...", t.Interval())
go targetPool.Run(m.results, t.Interval())
2013-01-04 05:41:47 -08:00
}
heap.Push(targetPool, t)
m.pools[t.Interval()] = targetPool
2013-01-04 05:41:47 -08:00
}
func (m targetManager) Remove(t Target) {
2013-01-04 05:41:47 -08:00
panic("not implemented")
}
func (m *targetManager) AddTargetsFromConfig(config *config.Config) {
for _, job := range config.Jobs {
for _, configTargets := range job.Targets {
baseLabels := model.LabelSet{
model.LabelName("job"): model.LabelValue(job.Name),
}
for label, value := range configTargets.Labels {
baseLabels[label] = value
}
interval := job.ScrapeInterval
if interval == 0 {
interval = config.Global.ScrapeInterval
}
for _, endpoint := range configTargets.Endpoints {
target := NewTarget(endpoint, interval, time.Second*5, baseLabels)
m.Add(target)
}
}
}
}