2016-02-23 01:58:16 -08:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2018-02-01 01:55:07 -08:00
|
|
|
package scrape
|
2016-02-23 01:58:16 -08:00
|
|
|
|
|
|
|
import (
|
2017-01-15 08:33:07 -08:00
|
|
|
"bytes"
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2016-02-28 00:51:02 -08:00
|
|
|
"fmt"
|
2017-01-15 08:33:07 -08:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-04-13 10:07:23 -07:00
|
|
|
"math"
|
2016-02-28 14:59:03 -08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2016-02-28 00:51:02 -08:00
|
|
|
"reflect"
|
2016-02-28 14:59:03 -08:00
|
|
|
"strings"
|
2016-02-28 00:51:02 -08:00
|
|
|
"sync"
|
2016-02-23 01:58:16 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
2017-01-15 08:33:07 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-23 01:58:16 -08:00
|
|
|
|
2018-01-09 07:43:28 -08:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
2016-02-23 02:56:09 -08:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2018-01-04 06:13:31 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2016-12-29 00:27:30 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2018-05-18 00:32:11 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/textparse"
|
2017-04-11 07:42:17 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2017-04-13 02:33:08 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/value"
|
2016-02-28 00:51:02 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2017-09-15 02:08:51 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2016-02-23 01:58:16 -08:00
|
|
|
)
|
|
|
|
|
2016-02-28 00:51:02 -08:00
|
|
|
func TestNewScrapePool(t *testing.T) {
|
|
|
|
var (
|
2016-12-30 12:35:35 -08:00
|
|
|
app = &nopAppendable{}
|
2016-02-28 00:51:02 -08:00
|
|
|
cfg = &config.ScrapeConfig{}
|
2017-11-26 07:15:15 -08:00
|
|
|
sp = newScrapePool(cfg, app, nil)
|
2016-02-28 00:51:02 -08:00
|
|
|
)
|
|
|
|
|
2016-12-30 12:35:35 -08:00
|
|
|
if a, ok := sp.appendable.(*nopAppendable); !ok || a != app {
|
2016-02-28 00:51:02 -08:00
|
|
|
t.Fatalf("Wrong sample appender")
|
|
|
|
}
|
|
|
|
if sp.config != cfg {
|
|
|
|
t.Fatalf("Wrong scrape config")
|
|
|
|
}
|
|
|
|
if sp.newLoop == nil {
|
|
|
|
t.Fatalf("newLoop function not initialized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 06:13:31 -08:00
|
|
|
func TestDroppedTargetsList(t *testing.T) {
|
|
|
|
var (
|
|
|
|
app = &nopAppendable{}
|
|
|
|
cfg = &config.ScrapeConfig{
|
|
|
|
JobName: "dropMe",
|
|
|
|
ScrapeInterval: model.Duration(1),
|
|
|
|
RelabelConfigs: []*config.RelabelConfig{
|
|
|
|
{
|
|
|
|
Action: config.RelabelDrop,
|
|
|
|
Regex: mustNewRegexp("dropMe"),
|
|
|
|
SourceLabels: model.LabelNames{"job"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tgs = []*targetgroup.Group{
|
|
|
|
{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.1:9090"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sp = newScrapePool(cfg, app, nil)
|
|
|
|
expectedLabelSetString = "{__address__=\"127.0.0.1:9090\", __metrics_path__=\"\", __scheme__=\"\", job=\"dropMe\"}"
|
|
|
|
expectedLength = 1
|
|
|
|
)
|
|
|
|
sp.Sync(tgs)
|
|
|
|
sp.Sync(tgs)
|
|
|
|
if len(sp.droppedTargets) != expectedLength {
|
|
|
|
t.Fatalf("Length of dropped targets exceeded expected length, expected %v, got %v", expectedLength, len(sp.droppedTargets))
|
|
|
|
}
|
|
|
|
if sp.droppedTargets[0].DiscoveredLabels().String() != expectedLabelSetString {
|
|
|
|
t.Fatalf("Got %v, expected %v", sp.droppedTargets[0].DiscoveredLabels().String(), expectedLabelSetString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 02:29:27 -08:00
|
|
|
// TestDiscoveredLabelsUpdate checks that DiscoveredLabels are updated
|
|
|
|
// even when new labels don't affect the target `hash`.
|
|
|
|
func TestDiscoveredLabelsUpdate(t *testing.T) {
|
|
|
|
|
|
|
|
sp := &scrapePool{}
|
|
|
|
// These are used when syncing so need this to avoid a panic.
|
|
|
|
sp.config = &config.ScrapeConfig{
|
|
|
|
ScrapeInterval: model.Duration(1),
|
|
|
|
ScrapeTimeout: model.Duration(1),
|
|
|
|
}
|
2018-09-26 02:20:56 -07:00
|
|
|
sp.activeTargets = make(map[uint64]*Target)
|
2018-02-07 02:29:27 -08:00
|
|
|
t1 := &Target{
|
|
|
|
discoveredLabels: labels.Labels{
|
|
|
|
labels.Label{
|
|
|
|
Name: "label",
|
|
|
|
Value: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-26 02:20:56 -07:00
|
|
|
sp.activeTargets[t1.hash()] = t1
|
2018-02-07 02:29:27 -08:00
|
|
|
|
|
|
|
t2 := &Target{
|
|
|
|
discoveredLabels: labels.Labels{
|
|
|
|
labels.Label{
|
|
|
|
Name: "labelNew",
|
|
|
|
Value: "nameNew",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sp.sync([]*Target{t2})
|
|
|
|
|
2018-09-26 02:20:56 -07:00
|
|
|
testutil.Equals(t, t2.DiscoveredLabels(), sp.activeTargets[t1.hash()].DiscoveredLabels())
|
2018-02-07 02:29:27 -08:00
|
|
|
}
|
|
|
|
|
2016-02-28 00:51:02 -08:00
|
|
|
type testLoop struct {
|
|
|
|
startFunc func(interval, timeout time.Duration, errc chan<- error)
|
|
|
|
stopFunc func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *testLoop) run(interval, timeout time.Duration, errc chan<- error) {
|
|
|
|
l.startFunc(interval, timeout, errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *testLoop) stop() {
|
|
|
|
l.stopFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScrapePoolStop(t *testing.T) {
|
|
|
|
sp := &scrapePool{
|
2018-09-26 02:20:56 -07:00
|
|
|
activeTargets: map[uint64]*Target{},
|
|
|
|
loops: map[uint64]loop{},
|
|
|
|
cancel: func() {},
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
var mtx sync.Mutex
|
2016-02-28 10:56:18 -08:00
|
|
|
stopped := map[uint64]bool{}
|
2016-02-28 00:51:02 -08:00
|
|
|
numTargets := 20
|
|
|
|
|
|
|
|
// Stopping the scrape pool must call stop() on all scrape loops,
|
|
|
|
// clean them and the respective targets up. It must wait until each loop's
|
|
|
|
// stop function returned before returning itself.
|
|
|
|
|
|
|
|
for i := 0; i < numTargets; i++ {
|
|
|
|
t := &Target{
|
2016-12-29 00:27:30 -08:00
|
|
|
labels: labels.FromStrings(model.AddressLabel, fmt.Sprintf("example.com:%d", i)),
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
l := &testLoop{}
|
|
|
|
l.stopFunc = func() {
|
|
|
|
time.Sleep(time.Duration(i*20) * time.Millisecond)
|
|
|
|
|
|
|
|
mtx.Lock()
|
2016-02-28 10:56:18 -08:00
|
|
|
stopped[t.hash()] = true
|
2016-02-28 00:51:02 -08:00
|
|
|
mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-09-26 02:20:56 -07:00
|
|
|
sp.activeTargets[t.hash()] = t
|
2016-02-28 10:56:18 -08:00
|
|
|
sp.loops[t.hash()] = l
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
stopTime := time.Now()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sp.stop()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("scrapeLoop.stop() did not return as expected")
|
|
|
|
case <-done:
|
|
|
|
// This should have taken at least as long as the last target slept.
|
|
|
|
if time.Since(stopTime) < time.Duration(numTargets*20)*time.Millisecond {
|
|
|
|
t.Fatalf("scrapeLoop.stop() exited before all targets stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx.Lock()
|
|
|
|
if len(stopped) != numTargets {
|
|
|
|
t.Fatalf("Expected 20 stopped loops, got %d", len(stopped))
|
|
|
|
}
|
|
|
|
mtx.Unlock()
|
|
|
|
|
2018-09-26 02:20:56 -07:00
|
|
|
if len(sp.activeTargets) > 0 {
|
|
|
|
t.Fatalf("Targets were not cleared on stopping: %d left", len(sp.activeTargets))
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
if len(sp.loops) > 0 {
|
|
|
|
t.Fatalf("Loops were not cleared on stopping: %d left", len(sp.loops))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScrapePoolReload(t *testing.T) {
|
|
|
|
var mtx sync.Mutex
|
|
|
|
numTargets := 20
|
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
stopped := map[uint64]bool{}
|
2016-02-28 00:51:02 -08:00
|
|
|
|
|
|
|
reloadCfg := &config.ScrapeConfig{
|
|
|
|
ScrapeInterval: model.Duration(3 * time.Second),
|
|
|
|
ScrapeTimeout: model.Duration(2 * time.Second),
|
|
|
|
}
|
2016-09-14 20:23:28 -07:00
|
|
|
// On starting to run, new loops created on reload check whether their preceding
|
2016-02-28 00:51:02 -08:00
|
|
|
// equivalents have been stopped.
|
2018-04-12 07:54:53 -07:00
|
|
|
newLoop := func(_ *Target, s scraper, _ int, _ bool, _ []*config.RelabelConfig) loop {
|
2016-02-28 00:51:02 -08:00
|
|
|
l := &testLoop{}
|
|
|
|
l.startFunc = func(interval, timeout time.Duration, errc chan<- error) {
|
|
|
|
if interval != 3*time.Second {
|
|
|
|
t.Errorf("Expected scrape interval %d but got %d", 3*time.Second, interval)
|
|
|
|
}
|
|
|
|
if timeout != 2*time.Second {
|
|
|
|
t.Errorf("Expected scrape timeout %d but got %d", 2*time.Second, timeout)
|
|
|
|
}
|
|
|
|
mtx.Lock()
|
2016-02-28 10:56:18 -08:00
|
|
|
if !stopped[s.(*targetScraper).hash()] {
|
2016-02-28 10:21:50 -08:00
|
|
|
t.Errorf("Scrape loop for %v not stopped yet", s.(*targetScraper))
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
mtx.Unlock()
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
sp := &scrapePool{
|
2018-09-26 02:20:56 -07:00
|
|
|
appendable: &nopAppendable{},
|
|
|
|
activeTargets: map[uint64]*Target{},
|
|
|
|
loops: map[uint64]loop{},
|
|
|
|
newLoop: newLoop,
|
|
|
|
logger: nil,
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reloading a scrape pool with a new scrape configuration must stop all scrape
|
2016-09-14 20:23:28 -07:00
|
|
|
// loops and start new ones. A new loop must not be started before the preceding
|
2016-02-28 00:51:02 -08:00
|
|
|
// one terminated.
|
|
|
|
|
|
|
|
for i := 0; i < numTargets; i++ {
|
|
|
|
t := &Target{
|
2016-12-29 00:27:30 -08:00
|
|
|
labels: labels.FromStrings(model.AddressLabel, fmt.Sprintf("example.com:%d", i)),
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
l := &testLoop{}
|
|
|
|
l.stopFunc = func() {
|
|
|
|
time.Sleep(time.Duration(i*20) * time.Millisecond)
|
|
|
|
|
|
|
|
mtx.Lock()
|
2016-02-28 10:56:18 -08:00
|
|
|
stopped[t.hash()] = true
|
2016-02-28 00:51:02 -08:00
|
|
|
mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-09-26 02:20:56 -07:00
|
|
|
sp.activeTargets[t.hash()] = t
|
2016-02-28 10:56:18 -08:00
|
|
|
sp.loops[t.hash()] = l
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
beforeTargets := map[uint64]*Target{}
|
2018-09-26 02:20:56 -07:00
|
|
|
for h, t := range sp.activeTargets {
|
2016-02-28 10:56:18 -08:00
|
|
|
beforeTargets[h] = t
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
reloadTime := time.Now()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sp.reload(reloadCfg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("scrapeLoop.reload() did not return as expected")
|
|
|
|
case <-done:
|
|
|
|
// This should have taken at least as long as the last target slept.
|
|
|
|
if time.Since(reloadTime) < time.Duration(numTargets*20)*time.Millisecond {
|
|
|
|
t.Fatalf("scrapeLoop.stop() exited before all targets stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx.Lock()
|
|
|
|
if len(stopped) != numTargets {
|
2016-05-01 14:37:45 -07:00
|
|
|
t.Fatalf("Expected 20 stopped loops, got %d", len(stopped))
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
mtx.Unlock()
|
|
|
|
|
2018-09-26 02:20:56 -07:00
|
|
|
if !reflect.DeepEqual(sp.activeTargets, beforeTargets) {
|
2016-02-28 00:51:02 -08:00
|
|
|
t.Fatalf("Reloading affected target states unexpectedly")
|
|
|
|
}
|
|
|
|
if len(sp.loops) != numTargets {
|
|
|
|
t.Fatalf("Expected %d loops after reload but got %d", numTargets, len(sp.loops))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
func TestScrapePoolAppender(t *testing.T) {
|
|
|
|
cfg := &config.ScrapeConfig{}
|
2016-12-30 12:35:35 -08:00
|
|
|
app := &nopAppendable{}
|
2017-11-26 07:15:15 -08:00
|
|
|
sp := newScrapePool(cfg, app, nil)
|
2016-02-23 02:56:09 -08:00
|
|
|
|
2018-05-18 00:32:11 -07:00
|
|
|
loop := sp.newLoop(&Target{}, nil, 0, false, nil)
|
2018-04-12 07:54:53 -07:00
|
|
|
appl, ok := loop.(*scrapeLoop)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected scrapeLoop but got %T", loop)
|
|
|
|
}
|
|
|
|
wrapped := appl.appender()
|
2016-02-23 02:56:09 -08:00
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
tl, ok := wrapped.(*timeLimitAppender)
|
2016-02-23 02:56:09 -08:00
|
|
|
if !ok {
|
2017-09-08 05:34:45 -07:00
|
|
|
t.Fatalf("Expected timeLimitAppender but got %T", wrapped)
|
2016-02-23 02:56:09 -08:00
|
|
|
}
|
2017-09-08 05:34:45 -07:00
|
|
|
if _, ok := tl.Appender.(nopAppender); !ok {
|
|
|
|
t.Fatalf("Expected base appender but got %T", tl.Appender)
|
2016-02-23 02:56:09 -08:00
|
|
|
}
|
|
|
|
|
2018-05-18 00:32:11 -07:00
|
|
|
loop = sp.newLoop(&Target{}, nil, 100, false, nil)
|
2018-04-12 07:54:53 -07:00
|
|
|
appl, ok = loop.(*scrapeLoop)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected scrapeLoop but got %T", loop)
|
|
|
|
}
|
|
|
|
wrapped = appl.appender()
|
2017-09-08 05:34:45 -07:00
|
|
|
|
|
|
|
sl, ok := wrapped.(*limitAppender)
|
2016-02-23 02:56:09 -08:00
|
|
|
if !ok {
|
2017-09-08 05:34:45 -07:00
|
|
|
t.Fatalf("Expected limitAppender but got %T", wrapped)
|
2016-02-23 02:56:09 -08:00
|
|
|
}
|
2017-09-08 05:34:45 -07:00
|
|
|
tl, ok = sl.Appender.(*timeLimitAppender)
|
2017-01-30 08:30:28 -08:00
|
|
|
if !ok {
|
2017-09-08 05:34:45 -07:00
|
|
|
t.Fatalf("Expected limitAppender but got %T", sl.Appender)
|
2017-01-30 08:30:28 -08:00
|
|
|
}
|
2017-09-08 05:34:45 -07:00
|
|
|
if _, ok := tl.Appender.(nopAppender); !ok {
|
|
|
|
t.Fatalf("Expected base appender but got %T", tl.Appender)
|
2016-02-23 02:56:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 05:21:41 -07:00
|
|
|
func TestScrapePoolRaces(t *testing.T) {
|
|
|
|
interval, _ := model.ParseDuration("500ms")
|
|
|
|
timeout, _ := model.ParseDuration("1s")
|
|
|
|
newConfig := func() *config.ScrapeConfig {
|
|
|
|
return &config.ScrapeConfig{ScrapeInterval: interval, ScrapeTimeout: timeout}
|
|
|
|
}
|
|
|
|
sp := newScrapePool(newConfig(), &nopAppendable{}, nil)
|
|
|
|
tgts := []*targetgroup.Group{
|
|
|
|
&targetgroup.Group{
|
|
|
|
Targets: []model.LabelSet{
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.1:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.2:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.3:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.4:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.5:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.6:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.7:9090"},
|
|
|
|
model.LabelSet{model.AddressLabel: "127.0.0.8:9090"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-26 02:20:56 -07:00
|
|
|
sp.Sync(tgts)
|
|
|
|
active := sp.ActiveTargets()
|
|
|
|
dropped := sp.DroppedTargets()
|
2018-04-13 05:21:41 -07:00
|
|
|
expectedActive, expectedDropped := len(tgts[0].Targets), 0
|
2018-09-26 02:20:56 -07:00
|
|
|
if len(sp.ActiveTargets()) != expectedActive {
|
2018-04-13 05:21:41 -07:00
|
|
|
t.Fatalf("Invalid number of active targets: expected %v, got %v", expectedActive, len(active))
|
|
|
|
}
|
|
|
|
if len(dropped) != expectedDropped {
|
|
|
|
t.Fatalf("Invalid number of dropped targets: expected %v, got %v", expectedDropped, len(dropped))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
time.Sleep(time.Duration(10 * time.Millisecond))
|
|
|
|
sp.reload(newConfig())
|
|
|
|
}
|
|
|
|
sp.stop()
|
|
|
|
}
|
|
|
|
|
2017-05-10 08:59:02 -07:00
|
|
|
func TestScrapeLoopStopBeforeRun(t *testing.T) {
|
2016-02-28 00:51:02 -08:00
|
|
|
scraper := &testScraper{}
|
2017-09-08 05:34:45 -07:00
|
|
|
|
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil, nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2016-02-28 00:51:02 -08:00
|
|
|
|
|
|
|
// The scrape pool synchronizes on stopping scrape loops. However, new scrape
|
2017-01-07 08:28:49 -08:00
|
|
|
// loops are started asynchronously. Thus it's possible, that a loop is stopped
|
2016-02-28 00:51:02 -08:00
|
|
|
// again before having started properly.
|
|
|
|
// Stopping not-yet-started loops must block until the run method was called and exited.
|
|
|
|
// The run method must exit immediately.
|
|
|
|
|
|
|
|
stopDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
sl.stop()
|
|
|
|
close(stopDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopDone:
|
|
|
|
t.Fatalf("Stopping terminated before run exited successfully")
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Running the scrape loop must exit before calling the scraper even once.
|
2017-01-15 08:33:07 -08:00
|
|
|
scraper.scrapeFunc = func(context.Context, io.Writer) error {
|
2016-02-28 00:51:02 -08:00
|
|
|
t.Fatalf("scraper was called for terminated scrape loop")
|
2017-01-15 08:33:07 -08:00
|
|
|
return nil
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
runDone := make(chan struct{})
|
|
|
|
go func() {
|
2016-08-18 00:33:52 -07:00
|
|
|
sl.run(1, 0, nil)
|
2016-02-28 00:51:02 -08:00
|
|
|
close(runDone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-runDone:
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("Running terminated scrape loop did not exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopDone:
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("Stopping did not terminate after running exited")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
func nopMutator(l labels.Labels) labels.Labels { return l }
|
|
|
|
|
2017-05-10 08:59:02 -07:00
|
|
|
func TestScrapeLoopStop(t *testing.T) {
|
|
|
|
var (
|
2017-09-08 05:34:45 -07:00
|
|
|
signal = make(chan struct{})
|
|
|
|
appender = &collectResultAppender{}
|
|
|
|
scraper = &testScraper{}
|
|
|
|
app = func() storage.Appender { return appender }
|
2017-05-10 08:59:02 -07:00
|
|
|
)
|
|
|
|
defer close(signal)
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Terminate loop after 2 scrapes.
|
|
|
|
numScrapes := 0
|
2017-05-10 08:59:02 -07:00
|
|
|
|
|
|
|
scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error {
|
2017-05-26 01:44:48 -07:00
|
|
|
numScrapes++
|
2017-05-10 08:59:02 -07:00
|
|
|
if numScrapes == 2 {
|
2017-05-26 01:44:48 -07:00
|
|
|
go sl.stop()
|
2017-05-10 08:59:02 -07:00
|
|
|
}
|
|
|
|
w.Write([]byte("metric_a 42\n"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sl.run(10*time.Millisecond, time.Hour, nil)
|
|
|
|
signal <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-signal:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Scrape wasn't stopped.")
|
|
|
|
}
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
// We expected 1 actual sample for each scrape plus 4 for report samples.
|
|
|
|
// At least 2 scrapes were made, plus the final stale markers.
|
|
|
|
if len(appender.result) < 5*3 || len(appender.result)%5 != 0 {
|
|
|
|
t.Fatalf("Expected at least 3 scrapes with 4 samples each, got %d samples", len(appender.result))
|
2017-05-11 06:43:43 -07:00
|
|
|
}
|
2018-04-08 02:51:54 -07:00
|
|
|
// All samples in a scrape must have the same timestamp.
|
2017-09-08 05:34:45 -07:00
|
|
|
var ts int64
|
|
|
|
for i, s := range appender.result {
|
|
|
|
if i%5 == 0 {
|
|
|
|
ts = s.t
|
|
|
|
} else if s.t != ts {
|
|
|
|
t.Fatalf("Unexpected multiple timestamps within single scrape")
|
|
|
|
}
|
2017-05-11 06:43:43 -07:00
|
|
|
}
|
2017-09-08 05:34:45 -07:00
|
|
|
// All samples from the last scrape must be stale markers.
|
|
|
|
for _, s := range appender.result[len(appender.result)-5:] {
|
|
|
|
if !value.IsStaleNaN(s.v) {
|
|
|
|
t.Fatalf("Appended last sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(s.v))
|
|
|
|
}
|
2017-05-11 06:43:43 -07:00
|
|
|
}
|
2017-05-10 08:59:02 -07:00
|
|
|
}
|
|
|
|
|
2016-02-23 01:58:16 -08:00
|
|
|
func TestScrapeLoopRun(t *testing.T) {
|
|
|
|
var (
|
|
|
|
signal = make(chan struct{})
|
|
|
|
errc = make(chan error)
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
scraper = &testScraper{}
|
|
|
|
app = func() storage.Appender { return &nopAppender{} }
|
2016-02-23 01:58:16 -08:00
|
|
|
)
|
|
|
|
defer close(signal)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2016-02-23 01:58:16 -08:00
|
|
|
|
|
|
|
// The loop must terminate during the initial offset if the context
|
|
|
|
// is canceled.
|
|
|
|
scraper.offsetDur = time.Hour
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sl.run(time.Second, time.Hour, errc)
|
|
|
|
signal <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait to make sure we are actually waiting on the offset.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
select {
|
|
|
|
case <-signal:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Cancelation during initial offset failed")
|
|
|
|
case err := <-errc:
|
|
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The provided timeout must cause cancelation of the context passed down to the
|
|
|
|
// scraper. The scraper has to respect the context.
|
|
|
|
scraper.offsetDur = 0
|
|
|
|
|
|
|
|
block := make(chan struct{})
|
2017-01-15 08:33:07 -08:00
|
|
|
scraper.scrapeFunc = func(ctx context.Context, _ io.Writer) error {
|
2016-02-23 01:58:16 -08:00
|
|
|
select {
|
|
|
|
case <-block:
|
|
|
|
case <-ctx.Done():
|
2017-01-15 08:33:07 -08:00
|
|
|
return ctx.Err()
|
2016-02-23 01:58:16 -08:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
return nil
|
2016-02-23 01:58:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
2017-09-08 05:34:45 -07:00
|
|
|
sl = newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2016-02-23 01:58:16 -08:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
sl.run(time.Second, 100*time.Millisecond, errc)
|
|
|
|
signal <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err != context.DeadlineExceeded {
|
|
|
|
t.Fatalf("Expected timeout error but got: %s", err)
|
|
|
|
}
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Fatalf("Expected timeout error but got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already caught the timeout error and are certainly in the loop.
|
|
|
|
// Let the scrapes returns immediately to cause no further timeout errors
|
|
|
|
// and check whether canceling the parent context terminates the loop.
|
|
|
|
close(block)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-signal:
|
|
|
|
// Loop terminated as expected.
|
|
|
|
case err := <-errc:
|
|
|
|
t.Fatalf("Unexpected error: %s", err)
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
t.Fatalf("Loop did not terminate on context cancelation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 00:32:11 -07:00
|
|
|
func TestScrapeLoopMetadata(t *testing.T) {
|
|
|
|
var (
|
|
|
|
signal = make(chan struct{})
|
|
|
|
scraper = &testScraper{}
|
|
|
|
cache = newScrapeCache()
|
|
|
|
)
|
|
|
|
defer close(signal)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
sl := newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
func() storage.Appender { return nopAppender{} },
|
|
|
|
cache,
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-10-05 09:11:16 -07:00
|
|
|
total, _, err := sl.append([]byte(`# TYPE test_metric counter
|
2018-05-18 00:32:11 -07:00
|
|
|
# HELP test_metric some help text
|
2018-10-05 09:11:16 -07:00
|
|
|
# UNIT test_metric metric
|
2018-05-18 00:32:11 -07:00
|
|
|
test_metric 1
|
|
|
|
# TYPE test_metric_no_help gauge
|
2018-10-05 09:11:16 -07:00
|
|
|
# HELP test_metric_no_type other help text
|
|
|
|
# EOF`), "application/openmetrics-text", time.Now())
|
2018-05-18 00:32:11 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, 1, total)
|
|
|
|
|
|
|
|
md, ok := cache.getMetadata("test_metric")
|
|
|
|
testutil.Assert(t, ok, "expected metadata to be present")
|
|
|
|
testutil.Assert(t, textparse.MetricTypeCounter == md.Type, "unexpected metric type")
|
|
|
|
testutil.Equals(t, "some help text", md.Help)
|
2018-10-05 09:11:16 -07:00
|
|
|
testutil.Equals(t, "metric", md.Unit)
|
2018-05-18 00:32:11 -07:00
|
|
|
|
|
|
|
md, ok = cache.getMetadata("test_metric_no_help")
|
|
|
|
testutil.Assert(t, ok, "expected metadata to be present")
|
|
|
|
testutil.Assert(t, textparse.MetricTypeGauge == md.Type, "unexpected metric type")
|
|
|
|
testutil.Equals(t, "", md.Help)
|
2018-10-05 09:11:16 -07:00
|
|
|
testutil.Equals(t, "", md.Unit)
|
2018-05-18 00:32:11 -07:00
|
|
|
|
|
|
|
md, ok = cache.getMetadata("test_metric_no_type")
|
|
|
|
testutil.Assert(t, ok, "expected metadata to be present")
|
2018-10-05 09:11:16 -07:00
|
|
|
testutil.Assert(t, textparse.MetricTypeUnknown == md.Type, "unexpected metric type")
|
2018-05-18 00:32:11 -07:00
|
|
|
testutil.Equals(t, "other help text", md.Help)
|
2018-10-05 09:11:16 -07:00
|
|
|
testutil.Equals(t, "", md.Unit)
|
2018-05-18 00:32:11 -07:00
|
|
|
}
|
|
|
|
|
2017-05-03 06:55:35 -07:00
|
|
|
func TestScrapeLoopRunCreatesStaleMarkersOnFailedScrape(t *testing.T) {
|
|
|
|
appender := &collectResultAppender{}
|
|
|
|
var (
|
2017-09-08 05:34:45 -07:00
|
|
|
signal = make(chan struct{})
|
|
|
|
scraper = &testScraper{}
|
|
|
|
app = func() storage.Appender { return appender }
|
2017-05-03 06:55:35 -07:00
|
|
|
)
|
|
|
|
defer close(signal)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2017-05-03 06:55:35 -07:00
|
|
|
// Succeed once, several failures, then stop.
|
2017-09-08 05:34:45 -07:00
|
|
|
numScrapes := 0
|
|
|
|
|
2017-05-03 06:55:35 -07:00
|
|
|
scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error {
|
2017-05-26 01:44:48 -07:00
|
|
|
numScrapes++
|
|
|
|
|
2017-05-03 06:55:35 -07:00
|
|
|
if numScrapes == 1 {
|
|
|
|
w.Write([]byte("metric_a 42\n"))
|
|
|
|
return nil
|
|
|
|
} else if numScrapes == 5 {
|
|
|
|
cancel()
|
|
|
|
}
|
2017-09-15 10:45:27 -07:00
|
|
|
return fmt.Errorf("scrape failed")
|
2017-05-03 06:55:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sl.run(10*time.Millisecond, time.Hour, nil)
|
|
|
|
signal <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-signal:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Scrape wasn't stopped.")
|
|
|
|
}
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
// 1 successfully scraped sample, 1 stale marker after first fail, 4 report samples for
|
|
|
|
// each scrape successful or not.
|
|
|
|
if len(appender.result) != 22 {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %d samples Got: %d", 22, len(appender.result))
|
2017-05-03 06:55:35 -07:00
|
|
|
}
|
|
|
|
if appender.result[0].v != 42.0 {
|
2017-09-15 10:45:27 -07:00
|
|
|
t.Fatalf("Appended first sample not as expected. Wanted: %f Got: %f", appender.result[0].v, 42.0)
|
2017-05-03 06:55:35 -07:00
|
|
|
}
|
2017-09-08 05:34:45 -07:00
|
|
|
if !value.IsStaleNaN(appender.result[5].v) {
|
|
|
|
t.Fatalf("Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[5].v))
|
2017-05-03 08:51:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) {
|
|
|
|
appender := &collectResultAppender{}
|
|
|
|
var (
|
2017-09-08 05:34:45 -07:00
|
|
|
signal = make(chan struct{})
|
2017-05-03 08:51:45 -07:00
|
|
|
scraper = &testScraper{}
|
|
|
|
app = func() storage.Appender { return appender }
|
|
|
|
numScrapes = 0
|
|
|
|
)
|
|
|
|
defer close(signal)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2017-05-03 08:51:45 -07:00
|
|
|
|
|
|
|
// Succeed once, several failures, then stop.
|
|
|
|
scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error {
|
2017-05-26 01:44:48 -07:00
|
|
|
numScrapes++
|
|
|
|
|
2017-05-03 08:51:45 -07:00
|
|
|
if numScrapes == 1 {
|
|
|
|
w.Write([]byte("metric_a 42\n"))
|
|
|
|
return nil
|
|
|
|
} else if numScrapes == 2 {
|
|
|
|
w.Write([]byte("7&-\n"))
|
|
|
|
return nil
|
|
|
|
} else if numScrapes == 3 {
|
|
|
|
cancel()
|
|
|
|
}
|
2017-09-15 10:45:27 -07:00
|
|
|
return fmt.Errorf("scrape failed")
|
2017-05-03 08:51:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sl.run(10*time.Millisecond, time.Hour, nil)
|
|
|
|
signal <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-signal:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Scrape wasn't stopped.")
|
|
|
|
}
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
// 1 successfully scraped sample, 1 stale marker after first fail, 4 report samples for
|
|
|
|
// each scrape successful or not.
|
|
|
|
if len(appender.result) != 14 {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %d samples Got: %d", 22, len(appender.result))
|
2017-05-03 08:51:45 -07:00
|
|
|
}
|
|
|
|
if appender.result[0].v != 42.0 {
|
2017-09-15 10:45:27 -07:00
|
|
|
t.Fatalf("Appended first sample not as expected. Wanted: %f Got: %f", appender.result[0].v, 42.0)
|
2017-05-03 08:51:45 -07:00
|
|
|
}
|
2017-09-08 05:34:45 -07:00
|
|
|
if !value.IsStaleNaN(appender.result[5].v) {
|
|
|
|
t.Fatalf("Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[5].v))
|
2017-05-03 06:55:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 07:42:17 -07:00
|
|
|
func TestScrapeLoopAppend(t *testing.T) {
|
|
|
|
|
2018-02-15 06:26:24 -08:00
|
|
|
tests := []struct {
|
|
|
|
title string
|
|
|
|
honorLabels bool
|
|
|
|
scrapeLabels string
|
|
|
|
discoveryLabels []string
|
|
|
|
expLset labels.Labels
|
|
|
|
expValue float64
|
|
|
|
}{
|
2017-04-11 07:42:17 -07:00
|
|
|
{
|
2018-02-15 06:26:24 -08:00
|
|
|
// When "honor_labels" is not set
|
|
|
|
// label name collision is handler by adding a prefix.
|
|
|
|
title: "Label name collision",
|
|
|
|
honorLabels: false,
|
|
|
|
scrapeLabels: `metric{n="1"} 0`,
|
|
|
|
discoveryLabels: []string{"n", "2"},
|
|
|
|
expLset: labels.FromStrings("__name__", "metric", "exported_n", "1", "n", "2"),
|
|
|
|
expValue: 0,
|
|
|
|
}, {
|
|
|
|
// Labels with no value need to be removed as these should not be ingested.
|
|
|
|
title: "Delete Empty labels",
|
|
|
|
honorLabels: false,
|
|
|
|
scrapeLabels: `metric{n=""} 0`,
|
|
|
|
discoveryLabels: nil,
|
|
|
|
expLset: labels.FromStrings("__name__", "metric"),
|
|
|
|
expValue: 0,
|
|
|
|
}, {
|
|
|
|
// Honor Labels should ignore labels with the same name.
|
|
|
|
title: "Honor Labels",
|
|
|
|
honorLabels: true,
|
|
|
|
scrapeLabels: `metric{n1="1" n2="2"} 0`,
|
|
|
|
discoveryLabels: []string{"n1", "0"},
|
|
|
|
expLset: labels.FromStrings("__name__", "metric", "n1", "1", "n2", "2"),
|
|
|
|
expValue: 0,
|
|
|
|
}, {
|
|
|
|
title: "Stale - NaN",
|
|
|
|
honorLabels: false,
|
|
|
|
scrapeLabels: `metric NaN`,
|
|
|
|
discoveryLabels: nil,
|
|
|
|
expLset: labels.FromStrings("__name__", "metric"),
|
|
|
|
expValue: float64(value.NormalNaN),
|
2017-04-11 07:42:17 -07:00
|
|
|
},
|
|
|
|
}
|
2018-02-15 06:26:24 -08:00
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
app := &collectResultAppender{}
|
|
|
|
|
|
|
|
discoveryLabels := &Target{
|
|
|
|
labels: labels.FromStrings(test.discoveryLabels...),
|
|
|
|
}
|
|
|
|
|
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
nil, nil, nil,
|
|
|
|
func(l labels.Labels) labels.Labels {
|
2018-04-12 07:54:53 -07:00
|
|
|
return mutateSampleLabels(l, discoveryLabels, test.honorLabels, nil)
|
2018-02-15 06:26:24 -08:00
|
|
|
},
|
|
|
|
func(l labels.Labels) labels.Labels {
|
2018-04-12 07:54:53 -07:00
|
|
|
return mutateReportSampleLabels(l, discoveryLabels)
|
2018-02-15 06:26:24 -08:00
|
|
|
},
|
|
|
|
func() storage.Appender { return app },
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2018-02-15 06:26:24 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err := sl.append([]byte(test.scrapeLabels), "", now)
|
2018-02-15 06:26:24 -08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []sample{
|
|
|
|
{
|
|
|
|
metric: test.expLset,
|
|
|
|
t: timestamp.FromTime(now),
|
|
|
|
v: test.expValue,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the expected value is NaN
|
|
|
|
// DeepEqual will report NaNs as being different,
|
|
|
|
// so replace it with the expected one.
|
|
|
|
if test.expValue == float64(value.NormalNaN) {
|
|
|
|
app.result[0].v = expected[0].v
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Test:%s", test.title)
|
|
|
|
testutil.Equals(t, expected, app.result)
|
2017-04-11 07:42:17 -07:00
|
|
|
}
|
2017-04-14 02:41:18 -07:00
|
|
|
}
|
|
|
|
|
2018-01-09 07:43:28 -08:00
|
|
|
func TestScrapeLoopAppendSampleLimit(t *testing.T) {
|
|
|
|
resApp := &collectResultAppender{}
|
|
|
|
app := &limitAppender{Appender: resApp, limit: 1}
|
|
|
|
|
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
nil, nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
func() storage.Appender { return app },
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2018-01-09 07:43:28 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Get the value of the Counter before performing the append.
|
|
|
|
beforeMetric := dto.Metric{}
|
|
|
|
err := targetScrapeSampleLimit.Write(&beforeMetric)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
beforeMetricValue := beforeMetric.GetCounter().GetValue()
|
|
|
|
|
|
|
|
now := time.Now()
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err = sl.append([]byte("metric_a 1\nmetric_b 1\nmetric_c 1\n"), "", now)
|
2018-01-09 07:43:28 -08:00
|
|
|
if err != errSampleLimit {
|
|
|
|
t.Fatalf("Did not see expected sample limit error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the Counter has been incremented a simgle time for the scrape,
|
|
|
|
// not multiple times for each sample.
|
|
|
|
metric := dto.Metric{}
|
|
|
|
err = targetScrapeSampleLimit.Write(&metric)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
value := metric.GetCounter().GetValue()
|
|
|
|
if (value - beforeMetricValue) != 1 {
|
2018-01-19 04:07:19 -08:00
|
|
|
t.Fatalf("Unexpected change of sample limit metric: %f", (value - beforeMetricValue))
|
2018-01-09 07:43:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// And verify that we got the samples that fit under the limit.
|
|
|
|
want := []sample{
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
|
|
|
|
t: timestamp.FromTime(now),
|
|
|
|
v: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, resApp.result) {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %+v Got: %+v", want, resApp.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 02:08:51 -07:00
|
|
|
func TestScrapeLoop_ChangingMetricString(t *testing.T) {
|
|
|
|
// This is a regression test for the scrape loop cache not properly maintaining
|
|
|
|
// IDs when the string representation of a metric changes across a scrape. Thus
|
|
|
|
// we use a real storage appender here.
|
|
|
|
s := testutil.NewStorage(t)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
app, err := s.Appender()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
capp := &collectResultAppender{next: app}
|
|
|
|
|
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
nil, nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
func() storage.Appender { return capp },
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-15 02:08:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now()
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err = sl.append([]byte(`metric_a{a="1",b="1"} 1`), "", now)
|
2017-09-15 02:08:51 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err = sl.append([]byte(`metric_a{b="1",a="1"} 2`), "", now.Add(time.Minute))
|
2017-09-15 02:08:51 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeepEqual will report NaNs as being different, so replace with a different value.
|
|
|
|
want := []sample{
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"),
|
|
|
|
t: timestamp.FromTime(now),
|
|
|
|
v: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"),
|
|
|
|
t: timestamp.FromTime(now.Add(time.Minute)),
|
|
|
|
v: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, capp.result) {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %+v Got: %+v", want, capp.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 02:41:18 -07:00
|
|
|
func TestScrapeLoopAppendStaleness(t *testing.T) {
|
|
|
|
app := &collectResultAppender{}
|
2017-09-08 05:34:45 -07:00
|
|
|
|
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
nil, nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
2017-05-26 01:44:48 -07:00
|
|
|
func() storage.Appender { return app },
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-05-26 01:44:48 -07:00
|
|
|
)
|
2017-04-14 02:41:18 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err := sl.append([]byte("metric_a 1\n"), "", now)
|
2017-04-14 02:41:18 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err = sl.append([]byte(""), "", now.Add(time.Second))
|
2017-04-14 02:41:18 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ingestedNaN := math.Float64bits(app.result[1].v)
|
|
|
|
if ingestedNaN != value.StaleNaN {
|
|
|
|
t.Fatalf("Appended stale sample wasn't as expected. Wanted: %x Got: %x", value.StaleNaN, ingestedNaN)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeepEqual will report NaNs as being different, so replace with a different value.
|
|
|
|
app.result[1].v = 42
|
|
|
|
want := []sample{
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
|
|
|
|
t: timestamp.FromTime(now),
|
|
|
|
v: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
|
|
|
|
t: timestamp.FromTime(now.Add(time.Second)),
|
|
|
|
v: 42,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, app.result) {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %+v Got: %+v", want, app.result)
|
|
|
|
}
|
2017-04-11 07:42:17 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-28 08:36:36 -07:00
|
|
|
func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) {
|
|
|
|
app := &collectResultAppender{}
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
nil, nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
2017-05-26 01:44:48 -07:00
|
|
|
func() storage.Appender { return app },
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-05-26 01:44:48 -07:00
|
|
|
)
|
2017-04-28 08:36:36 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err := sl.append([]byte("metric_a 1 1000\n"), "", now)
|
2017-04-28 08:36:36 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err = sl.append([]byte(""), "", now.Add(time.Second))
|
2017-04-28 08:36:36 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := []sample{
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
|
|
|
|
t: 1000,
|
|
|
|
v: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, app.result) {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %+v Got: %+v", want, app.result)
|
|
|
|
}
|
2017-05-29 06:08:55 -07:00
|
|
|
}
|
|
|
|
|
2017-06-14 19:08:03 -07:00
|
|
|
func TestScrapeLoopRunReportsTargetDownOnScrapeError(t *testing.T) {
|
|
|
|
var (
|
2017-09-08 05:34:45 -07:00
|
|
|
scraper = &testScraper{}
|
|
|
|
appender = &collectResultAppender{}
|
|
|
|
app = func() storage.Appender { return appender }
|
2017-06-14 19:08:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2017-06-14 19:08:03 -07:00
|
|
|
|
|
|
|
scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error {
|
|
|
|
cancel()
|
|
|
|
return fmt.Errorf("scrape failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
sl.run(10*time.Millisecond, time.Hour, nil)
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
if appender.result[0].v != 0 {
|
|
|
|
t.Fatalf("bad 'up' value; want 0, got %v", appender.result[0].v)
|
2017-06-14 19:08:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 05:09:50 -07:00
|
|
|
func TestScrapeLoopRunReportsTargetDownOnInvalidUTF8(t *testing.T) {
|
|
|
|
var (
|
2017-09-08 05:34:45 -07:00
|
|
|
scraper = &testScraper{}
|
|
|
|
appender = &collectResultAppender{}
|
|
|
|
app = func() storage.Appender { return appender }
|
2017-06-16 05:09:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(ctx,
|
|
|
|
scraper,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
app,
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
)
|
2017-06-16 05:09:50 -07:00
|
|
|
|
|
|
|
scraper.scrapeFunc = func(ctx context.Context, w io.Writer) error {
|
|
|
|
cancel()
|
2017-09-08 05:34:45 -07:00
|
|
|
w.Write([]byte("a{l=\"\xff\"} 1\n"))
|
2017-06-16 05:09:50 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sl.run(10*time.Millisecond, time.Hour, nil)
|
|
|
|
|
2017-09-08 05:34:45 -07:00
|
|
|
if appender.result[0].v != 0 {
|
|
|
|
t.Fatalf("bad 'up' value; want 0, got %v", appender.result[0].v)
|
2017-06-16 05:09:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 09:20:07 -07:00
|
|
|
type errorAppender struct {
|
|
|
|
collectResultAppender
|
|
|
|
}
|
|
|
|
|
2017-09-07 05:14:41 -07:00
|
|
|
func (app *errorAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
|
2017-07-04 05:55:33 -07:00
|
|
|
switch lset.Get(model.MetricNameLabel) {
|
|
|
|
case "out_of_order":
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrOutOfOrderSample
|
2017-07-04 05:55:33 -07:00
|
|
|
case "amend":
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrDuplicateSampleForTimestamp
|
2017-07-04 05:55:33 -07:00
|
|
|
case "out_of_bounds":
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrOutOfBounds
|
2017-07-04 05:55:33 -07:00
|
|
|
default:
|
|
|
|
return app.collectResultAppender.Add(lset, t, v)
|
2017-05-03 09:20:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 05:14:41 -07:00
|
|
|
func (app *errorAppender) AddFast(lset labels.Labels, ref uint64, t int64, v float64) error {
|
2017-07-12 04:41:27 -07:00
|
|
|
return app.collectResultAppender.AddFast(lset, ref, t, v)
|
2017-05-03 09:20:07 -07:00
|
|
|
}
|
|
|
|
|
2017-07-04 05:55:33 -07:00
|
|
|
func TestScrapeLoopAppendGracefullyIfAmendOrOutOfOrderOrOutOfBounds(t *testing.T) {
|
2017-05-03 09:20:07 -07:00
|
|
|
app := &errorAppender{}
|
2017-09-08 05:34:45 -07:00
|
|
|
|
|
|
|
sl := newScrapeLoop(context.Background(),
|
2017-05-26 01:44:48 -07:00
|
|
|
nil,
|
2017-09-08 05:34:45 -07:00
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
|
|
|
func() storage.Appender { return app },
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-05-26 01:44:48 -07:00
|
|
|
)
|
2017-05-03 09:20:07 -07:00
|
|
|
|
|
|
|
now := time.Unix(1, 0)
|
2018-10-04 06:52:03 -07:00
|
|
|
_, _, err := sl.append([]byte("out_of_order 1\namend 1\nnormal 1\nout_of_bounds 1\n"), "", now)
|
2017-05-03 09:20:07 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected append error: %s", err)
|
|
|
|
}
|
|
|
|
want := []sample{
|
|
|
|
{
|
|
|
|
metric: labels.FromStrings(model.MetricNameLabel, "normal"),
|
|
|
|
t: timestamp.FromTime(now),
|
|
|
|
v: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(want, app.result) {
|
|
|
|
t.Fatalf("Appended samples not as expected. Wanted: %+v Got: %+v", want, app.result)
|
|
|
|
}
|
2017-07-04 05:55:33 -07:00
|
|
|
}
|
2017-05-03 09:20:07 -07:00
|
|
|
|
2017-07-04 05:55:33 -07:00
|
|
|
func TestScrapeLoopOutOfBoundsTimeError(t *testing.T) {
|
|
|
|
app := &collectResultAppender{}
|
2017-09-08 05:34:45 -07:00
|
|
|
sl := newScrapeLoop(context.Background(),
|
|
|
|
nil,
|
|
|
|
nil, nil,
|
|
|
|
nopMutator,
|
|
|
|
nopMutator,
|
2017-07-04 05:55:33 -07:00
|
|
|
func() storage.Appender {
|
|
|
|
return &timeLimitAppender{
|
|
|
|
Appender: app,
|
|
|
|
maxTime: timestamp.FromTime(time.Now().Add(10 * time.Minute)),
|
|
|
|
}
|
|
|
|
},
|
2018-05-18 00:32:11 -07:00
|
|
|
nil,
|
2017-07-04 05:55:33 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now().Add(20 * time.Minute)
|
2018-10-04 06:52:03 -07:00
|
|
|
total, added, err := sl.append([]byte("normal 1\n"), "", now)
|
2017-07-04 05:55:33 -07:00
|
|
|
if total != 1 {
|
|
|
|
t.Error("expected 1 metric")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if added != 0 {
|
|
|
|
t.Error("no metric should be added")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expect no error, got %s", err.Error())
|
|
|
|
}
|
2017-05-03 09:20:07 -07:00
|
|
|
}
|
|
|
|
|
2016-02-28 14:59:03 -08:00
|
|
|
func TestTargetScraperScrapeOK(t *testing.T) {
|
2017-04-04 10:26:28 -07:00
|
|
|
const (
|
|
|
|
configTimeout = 1500 * time.Millisecond
|
|
|
|
expectedTimeout = "1.500000"
|
|
|
|
)
|
|
|
|
|
2016-02-28 14:59:03 -08:00
|
|
|
server := httptest.NewServer(
|
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-09-22 09:06:43 -07:00
|
|
|
accept := r.Header.Get("Accept")
|
2018-10-05 09:11:16 -07:00
|
|
|
if !strings.HasPrefix(accept, "application/openmetrics-text;") {
|
|
|
|
t.Errorf("Expected Accept header to prefer application/openmetrics-text, got %q", accept)
|
2017-09-22 09:06:43 -07:00
|
|
|
}
|
|
|
|
|
2017-04-05 11:56:22 -07:00
|
|
|
timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds")
|
2017-04-04 10:26:28 -07:00
|
|
|
if timeout != expectedTimeout {
|
2017-09-22 09:06:43 -07:00
|
|
|
t.Errorf("Expected scrape timeout header %q, got %q", expectedTimeout, timeout)
|
2017-04-04 10:26:28 -07:00
|
|
|
}
|
|
|
|
|
2016-02-28 14:59:03 -08:00
|
|
|
w.Header().Set("Content-Type", `text/plain; version=0.0.4`)
|
|
|
|
w.Write([]byte("metric_a 1\nmetric_b 2\n"))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
serverURL, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := &targetScraper{
|
|
|
|
Target: &Target{
|
2016-12-29 00:27:30 -08:00
|
|
|
labels: labels.FromStrings(
|
|
|
|
model.SchemeLabel, serverURL.Scheme,
|
|
|
|
model.AddressLabel, serverURL.Host,
|
|
|
|
),
|
2016-02-28 14:59:03 -08:00
|
|
|
},
|
2017-04-04 10:26:28 -07:00
|
|
|
client: http.DefaultClient,
|
|
|
|
timeout: configTimeout,
|
2016-02-28 14:59:03 -08:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
var buf bytes.Buffer
|
2016-02-28 14:59:03 -08:00
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
if _, err := ts.scrape(context.Background(), &buf); err != nil {
|
2016-02-28 14:59:03 -08:00
|
|
|
t.Fatalf("Unexpected scrape error: %s", err)
|
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
require.Equal(t, "metric_a 1\nmetric_b 2\n", buf.String())
|
2016-02-28 14:59:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTargetScrapeScrapeCancel(t *testing.T) {
|
|
|
|
block := make(chan struct{})
|
|
|
|
|
|
|
|
server := httptest.NewServer(
|
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
<-block
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
serverURL, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := &targetScraper{
|
|
|
|
Target: &Target{
|
2016-12-29 00:27:30 -08:00
|
|
|
labels: labels.FromStrings(
|
|
|
|
model.SchemeLabel, serverURL.Scheme,
|
|
|
|
model.AddressLabel, serverURL.Host,
|
|
|
|
),
|
2016-02-28 14:59:03 -08:00
|
|
|
},
|
|
|
|
client: http.DefaultClient,
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2016-11-13 09:21:42 -08:00
|
|
|
errc := make(chan error)
|
2016-02-28 14:59:03 -08:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2018-10-04 06:52:03 -07:00
|
|
|
if _, err := ts.scrape(ctx, ioutil.Discard); err != context.Canceled {
|
2016-11-13 09:21:42 -08:00
|
|
|
errc <- fmt.Errorf("Expected context cancelation error but got: %s", err)
|
2016-02-28 14:59:03 -08:00
|
|
|
}
|
2016-11-13 09:21:42 -08:00
|
|
|
close(errc)
|
2016-02-28 14:59:03 -08:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Scrape function did not return unexpectedly")
|
2016-11-13 09:21:42 -08:00
|
|
|
case err := <-errc:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2016-02-28 14:59:03 -08:00
|
|
|
}
|
|
|
|
// If this is closed in a defer above the function the test server
|
2018-04-27 05:04:02 -07:00
|
|
|
// doesn't terminate and the test doesn't complete.
|
2016-02-28 14:59:03 -08:00
|
|
|
close(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTargetScrapeScrapeNotFound(t *testing.T) {
|
|
|
|
server := httptest.NewServer(
|
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
serverURL, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := &targetScraper{
|
|
|
|
Target: &Target{
|
2016-12-29 00:27:30 -08:00
|
|
|
labels: labels.FromStrings(
|
|
|
|
model.SchemeLabel, serverURL.Scheme,
|
|
|
|
model.AddressLabel, serverURL.Host,
|
|
|
|
),
|
2016-02-28 14:59:03 -08:00
|
|
|
},
|
|
|
|
client: http.DefaultClient,
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
if _, err := ts.scrape(context.Background(), ioutil.Discard); !strings.Contains(err.Error(), "404") {
|
2016-02-28 14:59:03 -08:00
|
|
|
t.Fatalf("Expected \"404 NotFound\" error but got: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 01:58:16 -08:00
|
|
|
// testScraper implements the scraper interface and allows setting values
|
|
|
|
// returned by its methods. It also allows setting a custom scrape function.
|
|
|
|
type testScraper struct {
|
|
|
|
offsetDur time.Duration
|
|
|
|
|
|
|
|
lastStart time.Time
|
|
|
|
lastDuration time.Duration
|
|
|
|
lastError error
|
|
|
|
|
|
|
|
scrapeErr error
|
2017-01-15 08:33:07 -08:00
|
|
|
scrapeFunc func(context.Context, io.Writer) error
|
2016-02-23 01:58:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *testScraper) offset(interval time.Duration) time.Duration {
|
|
|
|
return ts.offsetDur
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *testScraper) report(start time.Time, duration time.Duration, err error) {
|
|
|
|
ts.lastStart = start
|
|
|
|
ts.lastDuration = duration
|
|
|
|
ts.lastError = err
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:52:03 -07:00
|
|
|
func (ts *testScraper) scrape(ctx context.Context, w io.Writer) (string, error) {
|
2016-02-23 01:58:16 -08:00
|
|
|
if ts.scrapeFunc != nil {
|
2018-10-04 06:52:03 -07:00
|
|
|
return "", ts.scrapeFunc(ctx, w)
|
2016-02-23 01:58:16 -08:00
|
|
|
}
|
2018-10-04 06:52:03 -07:00
|
|
|
return "", ts.scrapeErr
|
2016-02-23 01:58:16 -08:00
|
|
|
}
|