2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-15 08:06:17 -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 (
|
2013-06-25 05:02:27 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-02-13 11:24:17 -08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
pb "github.com/prometheus/prometheus/config/generated"
|
2015-03-14 19:36:15 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2013-01-15 08:06:17 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeTarget struct {
|
2014-07-29 11:31:11 -07:00
|
|
|
scrapeCount int
|
|
|
|
lastScrape time.Time
|
|
|
|
interval time.Duration
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2013-05-21 06:31:27 -07:00
|
|
|
func (t fakeTarget) LastError() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-17 10:40:59 -08:00
|
|
|
func (t fakeTarget) URL() string {
|
2013-01-15 08:06:17 -08:00
|
|
|
return "fake"
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:21:13 -08:00
|
|
|
func (t fakeTarget) InstanceIdentifier() string {
|
2015-02-19 09:58:47 -08:00
|
|
|
return "fake"
|
|
|
|
}
|
|
|
|
|
2014-12-17 10:40:59 -08:00
|
|
|
func (t fakeTarget) GlobalURL() string {
|
|
|
|
return t.URL()
|
2013-04-10 05:26:07 -07:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t fakeTarget) BaseLabels() clientmodel.LabelSet {
|
|
|
|
return clientmodel.LabelSet{}
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
|
|
|
|
2015-03-18 10:53:43 -07:00
|
|
|
func (t fakeTarget) BaseLabelsWithoutJobAndInstance() clientmodel.LabelSet {
|
|
|
|
return clientmodel.LabelSet{}
|
|
|
|
}
|
|
|
|
|
2013-01-15 08:06:17 -08:00
|
|
|
func (t fakeTarget) Interval() time.Duration {
|
|
|
|
return t.interval
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:31:11 -07:00
|
|
|
func (t fakeTarget) LastScrape() time.Time {
|
|
|
|
return t.lastScrape
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
func (t fakeTarget) scrape(storage.SampleAppender) error {
|
2013-01-15 08:06:17 -08:00
|
|
|
t.scrapeCount++
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
func (t fakeTarget) RunScraper(storage.SampleAppender, time.Duration) {
|
2014-07-29 11:31:11 -07:00
|
|
|
return
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2014-07-29 11:31:11 -07:00
|
|
|
func (t fakeTarget) StopScraper() {
|
|
|
|
return
|
2014-07-29 09:28:48 -07:00
|
|
|
}
|
|
|
|
|
2014-07-29 11:31:11 -07:00
|
|
|
func (t fakeTarget) State() TargetState {
|
2015-03-07 12:50:02 -08:00
|
|
|
return Healthy
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2014-10-10 05:19:02 -07:00
|
|
|
func (t *fakeTarget) SetBaseLabelsFrom(newTarget Target) {}
|
2013-02-22 12:07:35 -08:00
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
func (t *fakeTarget) Ingest(clientmodel.Samples) error { return nil }
|
|
|
|
|
2014-05-21 10:27:24 -07:00
|
|
|
func testTargetManager(t testing.TB) {
|
2015-03-14 19:36:15 -07:00
|
|
|
targetManager := NewTargetManager(nopAppender{}, nil)
|
2013-04-30 11:20:14 -07:00
|
|
|
testJob1 := config.JobConfig{
|
|
|
|
JobConfig: pb.JobConfig{
|
|
|
|
Name: proto.String("test_job1"),
|
|
|
|
ScrapeInterval: proto.String("1m"),
|
|
|
|
},
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
2013-04-30 11:20:14 -07:00
|
|
|
testJob2 := config.JobConfig{
|
|
|
|
JobConfig: pb.JobConfig{
|
|
|
|
Name: proto.String("test_job2"),
|
|
|
|
ScrapeInterval: proto.String("1m"),
|
|
|
|
},
|
2013-02-22 12:07:35 -08:00
|
|
|
}
|
2013-01-15 08:06:17 -08:00
|
|
|
|
|
|
|
target1GroupA := &fakeTarget{
|
2014-07-29 11:31:11 -07:00
|
|
|
interval: time.Minute,
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
target2GroupA := &fakeTarget{
|
2014-07-29 11:31:11 -07:00
|
|
|
interval: time.Minute,
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2013-06-11 08:54:58 -07:00
|
|
|
targetManager.AddTarget(testJob1, target1GroupA)
|
|
|
|
targetManager.AddTarget(testJob1, target2GroupA)
|
2013-01-15 08:06:17 -08:00
|
|
|
|
|
|
|
target1GroupB := &fakeTarget{
|
2014-07-29 11:31:11 -07:00
|
|
|
interval: time.Minute * 2,
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
2013-06-11 08:54:58 -07:00
|
|
|
targetManager.AddTarget(testJob2, target1GroupB)
|
2013-01-15 08:06:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTargetManager(t *testing.T) {
|
|
|
|
testTargetManager(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTargetManager(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
testTargetManager(b)
|
|
|
|
}
|
|
|
|
}
|