2016-02-17 10:33:17 -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.
|
|
|
|
|
|
|
|
package retrieval
|
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
import (
|
2016-02-28 10:21:50 -08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-02-22 07:46:55 -08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-02-28 10:21:50 -08:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2016-02-22 07:46:55 -08:00
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
"github.com/prometheus/common/model"
|
2017-02-28 05:59:33 -08:00
|
|
|
"github.com/prometheus/common/version"
|
2016-02-22 07:46:55 -08:00
|
|
|
"golang.org/x/net/context"
|
2016-02-28 10:21:50 -08:00
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-23 02:56:09 -08:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-02-22 07:46:55 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2017-03-20 06:17:04 -07:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2016-02-22 07:46:55 -08:00
|
|
|
)
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
const (
|
2016-12-13 09:32:11 -08:00
|
|
|
scrapeHealthMetricName = "up"
|
|
|
|
scrapeDurationMetricName = "scrape_duration_seconds"
|
|
|
|
scrapeSamplesMetricName = "scrape_samples_scraped"
|
|
|
|
samplesPostRelabelMetricName = "scrape_samples_post_metric_relabeling"
|
2016-02-22 07:46:55 -08:00
|
|
|
)
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
var (
|
|
|
|
targetIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2016-11-23 00:17:04 -08:00
|
|
|
Name: "prometheus_target_interval_length_seconds",
|
2016-02-22 07:46:55 -08:00
|
|
|
Help: "Actual intervals between scrapes.",
|
|
|
|
Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
|
|
|
|
},
|
2016-11-23 00:17:04 -08:00
|
|
|
[]string{"interval"},
|
2016-02-22 07:46:55 -08:00
|
|
|
)
|
2016-12-16 10:00:52 -08:00
|
|
|
targetSkippedScrapes = prometheus.NewCounter(
|
2016-02-22 07:46:55 -08:00
|
|
|
prometheus.CounterOpts{
|
2016-11-23 00:17:04 -08:00
|
|
|
Name: "prometheus_target_skipped_scrapes_total",
|
|
|
|
Help: "Total number of scrapes that were skipped because the metric storage was throttled.",
|
2016-02-22 07:46:55 -08:00
|
|
|
},
|
|
|
|
)
|
2016-03-09 07:33:10 -08:00
|
|
|
targetReloadIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2016-11-23 00:17:04 -08:00
|
|
|
Name: "prometheus_target_reload_length_seconds",
|
2016-03-09 07:33:10 -08:00
|
|
|
Help: "Actual interval to reload the scrape pool with a given configuration.",
|
|
|
|
Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
|
|
|
|
},
|
2016-11-23 00:17:04 -08:00
|
|
|
[]string{"interval"},
|
2016-03-09 07:33:10 -08:00
|
|
|
)
|
2016-03-11 03:22:23 -08:00
|
|
|
targetSyncIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2016-11-23 00:17:04 -08:00
|
|
|
Name: "prometheus_target_sync_length_seconds",
|
2016-03-11 03:22:23 -08:00
|
|
|
Help: "Actual interval to sync the scrape pool.",
|
|
|
|
Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
|
|
|
|
},
|
2016-11-23 00:17:04 -08:00
|
|
|
[]string{"scrape_job"},
|
2016-03-11 03:22:23 -08:00
|
|
|
)
|
|
|
|
targetScrapePoolSyncsCounter = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
2016-11-23 00:17:04 -08:00
|
|
|
Name: "prometheus_target_scrape_pool_sync_total",
|
|
|
|
Help: "Total number of syncs that were executed on a scrape pool.",
|
2016-03-11 03:22:23 -08:00
|
|
|
},
|
2016-11-23 00:17:04 -08:00
|
|
|
[]string{"scrape_job"},
|
2016-03-11 03:22:23 -08:00
|
|
|
)
|
2016-12-16 07:08:50 -08:00
|
|
|
targetScrapeSampleLimit = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrapes_exceeded_sample_limit_total",
|
|
|
|
Help: "Total number of scrapes that hit the sample limit and were rejected.",
|
|
|
|
},
|
|
|
|
)
|
2016-02-22 07:46:55 -08:00
|
|
|
)
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(targetIntervalLength)
|
|
|
|
prometheus.MustRegister(targetSkippedScrapes)
|
2016-03-09 07:33:10 -08:00
|
|
|
prometheus.MustRegister(targetReloadIntervalLength)
|
2016-03-11 03:22:23 -08:00
|
|
|
prometheus.MustRegister(targetSyncIntervalLength)
|
|
|
|
prometheus.MustRegister(targetScrapePoolSyncsCounter)
|
2016-12-16 07:08:50 -08:00
|
|
|
prometheus.MustRegister(targetScrapeSampleLimit)
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
// scrapePool manages scrapes for sets of targets.
|
|
|
|
type scrapePool struct {
|
|
|
|
appender storage.SampleAppender
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
ctx context.Context
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-28 10:21:50 -08:00
|
|
|
mtx sync.RWMutex
|
|
|
|
config *config.ScrapeConfig
|
|
|
|
client *http.Client
|
2016-02-28 00:51:02 -08:00
|
|
|
// Targets and loops must always be synchronized to have the same
|
2016-02-28 10:56:18 -08:00
|
|
|
// set of hashes.
|
|
|
|
targets map[uint64]*Target
|
|
|
|
loops map[uint64]loop
|
2016-02-28 00:51:02 -08:00
|
|
|
|
|
|
|
// Constructor for new scrape loops. This is settable for testing convenience.
|
2017-01-07 08:28:49 -08:00
|
|
|
newLoop func(context.Context, scraper, storage.SampleAppender, model.LabelSet, *config.ScrapeConfig) loop
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-11-22 03:48:30 -08:00
|
|
|
func newScrapePool(ctx context.Context, cfg *config.ScrapeConfig, app storage.SampleAppender) *scrapePool {
|
2017-03-20 06:17:04 -07:00
|
|
|
client, err := httputil.NewClientFromConfig(cfg.HTTPClientConfig)
|
2016-02-28 10:21:50 -08:00
|
|
|
if err != nil {
|
|
|
|
// Any errors that could occur here should be caught during config validation.
|
|
|
|
log.Errorf("Error creating HTTP client for job %q: %s", cfg.JobName, err)
|
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
return &scrapePool{
|
|
|
|
appender: app,
|
2016-02-23 02:56:09 -08:00
|
|
|
config: cfg,
|
2016-11-22 03:48:30 -08:00
|
|
|
ctx: ctx,
|
2016-02-28 10:21:50 -08:00
|
|
|
client: client,
|
2016-02-28 10:56:18 -08:00
|
|
|
targets: map[uint64]*Target{},
|
|
|
|
loops: map[uint64]loop{},
|
2016-02-28 00:51:02 -08:00
|
|
|
newLoop: newScrapeLoop,
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
// stop terminates all scrape loops and returns after they all terminated.
|
2016-02-22 07:46:55 -08:00
|
|
|
func (sp *scrapePool) stop() {
|
|
|
|
var wg sync.WaitGroup
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-28 00:51:02 -08:00
|
|
|
sp.mtx.Lock()
|
|
|
|
defer sp.mtx.Unlock()
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-28 00:51:02 -08:00
|
|
|
for fp, l := range sp.loops {
|
2016-02-23 05:37:25 -08:00
|
|
|
wg.Add(1)
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
go func(l loop) {
|
|
|
|
l.stop()
|
|
|
|
wg.Done()
|
|
|
|
}(l)
|
2016-02-28 00:51:02 -08:00
|
|
|
|
|
|
|
delete(sp.loops, fp)
|
|
|
|
delete(sp.targets, fp)
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
// reload the scrape pool with the given scrape configuration. The target state is preserved
|
|
|
|
// but all scrape loops are restarted with the new scrape configuration.
|
|
|
|
// This method returns after all scrape loops that were stopped have fully terminated.
|
2016-02-23 04:34:24 -08:00
|
|
|
func (sp *scrapePool) reload(cfg *config.ScrapeConfig) {
|
2016-03-09 07:33:10 -08:00
|
|
|
start := time.Now()
|
2016-11-22 03:48:30 -08:00
|
|
|
|
2016-02-23 04:34:24 -08:00
|
|
|
sp.mtx.Lock()
|
|
|
|
defer sp.mtx.Unlock()
|
|
|
|
|
2017-03-20 06:17:04 -07:00
|
|
|
client, err := httputil.NewClientFromConfig(cfg.HTTPClientConfig)
|
2016-02-28 10:21:50 -08:00
|
|
|
if err != nil {
|
|
|
|
// Any errors that could occur here should be caught during config validation.
|
|
|
|
log.Errorf("Error creating HTTP client for job %q: %s", cfg.JobName, err)
|
|
|
|
}
|
2016-02-23 04:34:24 -08:00
|
|
|
sp.config = cfg
|
2016-02-28 10:21:50 -08:00
|
|
|
sp.client = client
|
2016-02-23 04:34:24 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
interval = time.Duration(sp.config.ScrapeInterval)
|
|
|
|
timeout = time.Duration(sp.config.ScrapeTimeout)
|
|
|
|
)
|
2016-02-23 04:34:24 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
for fp, oldLoop := range sp.loops {
|
|
|
|
var (
|
2017-04-04 10:26:28 -07:00
|
|
|
t = sp.targets[fp]
|
|
|
|
s = &targetScraper{
|
|
|
|
Target: t,
|
|
|
|
client: sp.client,
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
newLoop = sp.newLoop(sp.ctx, s, sp.appender, t.Labels(), sp.config)
|
2016-02-23 05:37:25 -08:00
|
|
|
)
|
|
|
|
wg.Add(1)
|
2016-02-23 04:34:24 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
go func(oldLoop, newLoop loop) {
|
|
|
|
oldLoop.stop()
|
|
|
|
wg.Done()
|
2016-02-23 04:34:24 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
go newLoop.run(interval, timeout, nil)
|
|
|
|
}(oldLoop, newLoop)
|
|
|
|
|
|
|
|
sp.loops[fp] = newLoop
|
2016-02-23 04:34:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2016-03-09 07:33:10 -08:00
|
|
|
targetReloadIntervalLength.WithLabelValues(interval.String()).Observe(
|
2016-07-07 06:24:35 -07:00
|
|
|
time.Since(start).Seconds(),
|
2016-03-09 07:33:10 -08:00
|
|
|
)
|
2016-02-23 04:34:24 -08:00
|
|
|
}
|
|
|
|
|
2016-11-22 03:48:30 -08:00
|
|
|
// Sync converts target groups into actual scrape targets and synchronizes
|
|
|
|
// the currently running scraper with the resulting set.
|
|
|
|
func (sp *scrapePool) Sync(tgs []*config.TargetGroup) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var all []*Target
|
|
|
|
for _, tg := range tgs {
|
|
|
|
targets, err := targetsFromGroup(tg, sp.config)
|
|
|
|
if err != nil {
|
|
|
|
log.With("err", err).Error("creating targets failed")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
all = append(all, targets...)
|
|
|
|
}
|
|
|
|
sp.sync(all)
|
|
|
|
|
|
|
|
targetSyncIntervalLength.WithLabelValues(sp.config.JobName).Observe(
|
|
|
|
time.Since(start).Seconds(),
|
|
|
|
)
|
|
|
|
targetScrapePoolSyncsCounter.WithLabelValues(sp.config.JobName).Inc()
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
// sync takes a list of potentially duplicated targets, deduplicates them, starts
|
|
|
|
// scrape loops for new targets, and stops scrape loops for disappeared targets.
|
|
|
|
// It returns after all stopped scrape loops terminated.
|
|
|
|
func (sp *scrapePool) sync(targets []*Target) {
|
2016-02-22 07:46:55 -08:00
|
|
|
sp.mtx.Lock()
|
2016-02-23 04:34:24 -08:00
|
|
|
defer sp.mtx.Unlock()
|
2016-02-22 07:46:55 -08:00
|
|
|
|
|
|
|
var (
|
2016-02-28 10:56:18 -08:00
|
|
|
uniqueTargets = map[uint64]struct{}{}
|
|
|
|
interval = time.Duration(sp.config.ScrapeInterval)
|
|
|
|
timeout = time.Duration(sp.config.ScrapeTimeout)
|
2016-02-22 07:46:55 -08:00
|
|
|
)
|
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
for _, t := range targets {
|
2016-02-28 10:56:18 -08:00
|
|
|
hash := t.hash()
|
|
|
|
uniqueTargets[hash] = struct{}{}
|
2016-02-23 05:37:25 -08:00
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
if _, ok := sp.targets[hash]; !ok {
|
2017-04-04 10:26:28 -07:00
|
|
|
s := &targetScraper{
|
|
|
|
Target: t,
|
|
|
|
client: sp.client,
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
l := sp.newLoop(sp.ctx, s, sp.appender, t.Labels(), sp.config)
|
2016-02-23 05:37:25 -08:00
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
sp.targets[hash] = t
|
|
|
|
sp.loops[hash] = l
|
2016-02-23 05:37:25 -08:00
|
|
|
|
|
|
|
go l.run(interval, timeout, nil)
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
var wg sync.WaitGroup
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2016-02-23 05:37:25 -08:00
|
|
|
// Stop and remove old targets and scraper loops.
|
2016-02-28 10:56:18 -08:00
|
|
|
for hash := range sp.targets {
|
|
|
|
if _, ok := uniqueTargets[hash]; !ok {
|
2016-02-23 05:37:25 -08:00
|
|
|
wg.Add(1)
|
|
|
|
go func(l loop) {
|
|
|
|
l.stop()
|
2016-02-22 07:46:55 -08:00
|
|
|
wg.Done()
|
2016-02-28 10:56:18 -08:00
|
|
|
}(sp.loops[hash])
|
2016-02-23 05:37:25 -08:00
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
delete(sp.loops, hash)
|
|
|
|
delete(sp.targets, hash)
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all potentially stopped scrapers to terminate.
|
|
|
|
// This covers the case of flapping targets. If the server is under high load, a new scraper
|
|
|
|
// may be active and tries to insert. The old scraper that didn't terminate yet could still
|
|
|
|
// be inserting a previous sample set.
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:58:46 -08:00
|
|
|
// A scraper retrieves samples and accepts a status report at the end.
|
|
|
|
type scraper interface {
|
|
|
|
scrape(ctx context.Context, ts time.Time) (model.Samples, error)
|
|
|
|
report(start time.Time, dur time.Duration, err error)
|
|
|
|
offset(interval time.Duration) time.Duration
|
|
|
|
}
|
|
|
|
|
2016-02-28 10:21:50 -08:00
|
|
|
// targetScraper implements the scraper interface for a target.
|
|
|
|
type targetScraper struct {
|
|
|
|
*Target
|
2017-04-04 10:26:28 -07:00
|
|
|
client *http.Client
|
|
|
|
timeout time.Duration
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
|
|
|
|
2016-09-17 02:28:03 -07:00
|
|
|
const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3,*/*;q=0.1`
|
2016-02-28 10:21:50 -08:00
|
|
|
|
2017-02-28 05:59:33 -08:00
|
|
|
var userAgentHeader = fmt.Sprintf("Prometheus/%s", version.Version)
|
|
|
|
|
2016-02-28 10:21:50 -08:00
|
|
|
func (s *targetScraper) scrape(ctx context.Context, ts time.Time) (model.Samples, error) {
|
|
|
|
req, err := http.NewRequest("GET", s.URL().String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", acceptHeader)
|
2017-02-28 05:59:33 -08:00
|
|
|
req.Header.Set("User-Agent", userAgentHeader)
|
2017-04-04 10:26:28 -07:00
|
|
|
req.Header.Set("Scrape-Timeout-Seconds", fmt.Sprintf("%f", s.timeout.Seconds()))
|
2016-02-28 10:21:50 -08:00
|
|
|
|
|
|
|
resp, err := ctxhttp.Do(ctx, s.client, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("server returned HTTP status %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
allSamples = make(model.Samples, 0, 200)
|
|
|
|
decSamples = make(model.Vector, 0, 50)
|
|
|
|
)
|
|
|
|
sdec := expfmt.SampleDecoder{
|
|
|
|
Dec: expfmt.NewDecoder(resp.Body, expfmt.ResponseFormat(resp.Header)),
|
|
|
|
Opts: &expfmt.DecodeOptions{
|
|
|
|
Timestamp: model.TimeFromUnixNano(ts.UnixNano()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err = sdec.Decode(&decSamples); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
allSamples = append(allSamples, decSamples...)
|
|
|
|
decSamples = decSamples[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
// Set err to nil since it is used in the scrape health recording.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return allSamples, err
|
|
|
|
}
|
|
|
|
|
2016-02-28 00:51:02 -08:00
|
|
|
// A loop can run and be stopped again. It must not be reused after it was stopped.
|
2016-02-22 07:46:55 -08:00
|
|
|
type loop interface {
|
2016-02-22 09:49:26 -08:00
|
|
|
run(interval, timeout time.Duration, errc chan<- error)
|
2016-02-22 07:46:55 -08:00
|
|
|
stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type scrapeLoop struct {
|
|
|
|
scraper scraper
|
|
|
|
|
2016-12-13 07:01:35 -08:00
|
|
|
// Where samples are ultimately sent.
|
|
|
|
appender storage.SampleAppender
|
2017-01-07 08:28:49 -08:00
|
|
|
|
|
|
|
targetLabels model.LabelSet
|
|
|
|
metricRelabelConfigs []*config.RelabelConfig
|
|
|
|
honorLabels bool
|
|
|
|
sampleLimit uint
|
2016-02-22 07:46:55 -08:00
|
|
|
|
|
|
|
done chan struct{}
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
func newScrapeLoop(
|
|
|
|
ctx context.Context,
|
|
|
|
sc scraper,
|
|
|
|
appender storage.SampleAppender,
|
|
|
|
targetLabels model.LabelSet,
|
|
|
|
config *config.ScrapeConfig,
|
|
|
|
) loop {
|
2016-02-22 07:46:55 -08:00
|
|
|
sl := &scrapeLoop{
|
2017-01-07 08:28:49 -08:00
|
|
|
scraper: sc,
|
|
|
|
appender: appender,
|
|
|
|
targetLabels: targetLabels,
|
|
|
|
metricRelabelConfigs: config.MetricRelabelConfigs,
|
|
|
|
honorLabels: config.HonorLabels,
|
|
|
|
sampleLimit: config.SampleLimit,
|
|
|
|
done: make(chan struct{}),
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
sl.ctx, sl.cancel = context.WithCancel(ctx)
|
|
|
|
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
|
2016-02-22 09:49:26 -08:00
|
|
|
func (sl *scrapeLoop) run(interval, timeout time.Duration, errc chan<- error) {
|
2016-02-22 07:46:55 -08:00
|
|
|
defer close(sl.done)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(sl.scraper.offset(interval)):
|
|
|
|
// Continue after a scraping offset.
|
|
|
|
case <-sl.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var last time.Time
|
|
|
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-02-22 09:49:26 -08:00
|
|
|
if !sl.appender.NeedsThrottling() {
|
|
|
|
var (
|
2017-01-07 08:28:49 -08:00
|
|
|
start = time.Now()
|
|
|
|
scrapeCtx, _ = context.WithTimeout(sl.ctx, timeout)
|
|
|
|
numPostRelabelSamples = 0
|
2016-02-22 09:49:26 -08:00
|
|
|
)
|
|
|
|
|
2016-02-25 05:05:42 -08:00
|
|
|
// Only record after the first scrape.
|
|
|
|
if !last.IsZero() {
|
|
|
|
targetIntervalLength.WithLabelValues(interval.String()).Observe(
|
2016-07-07 06:24:35 -07:00
|
|
|
time.Since(last).Seconds(),
|
2016-02-25 05:05:42 -08:00
|
|
|
)
|
|
|
|
}
|
2016-02-22 09:49:26 -08:00
|
|
|
|
2016-02-25 04:58:46 -08:00
|
|
|
samples, err := sl.scraper.scrape(scrapeCtx, start)
|
2016-02-22 09:49:26 -08:00
|
|
|
if err == nil {
|
2017-01-07 08:28:49 -08:00
|
|
|
numPostRelabelSamples, err = sl.append(samples)
|
|
|
|
}
|
2016-12-13 08:18:17 -08:00
|
|
|
if err != nil && errc != nil {
|
2016-02-22 09:49:26 -08:00
|
|
|
errc <- err
|
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
sl.report(start, time.Since(start), len(samples), numPostRelabelSamples, err)
|
2016-02-22 09:49:26 -08:00
|
|
|
last = start
|
|
|
|
} else {
|
2016-12-16 10:00:52 -08:00
|
|
|
targetSkippedScrapes.Inc()
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sl *scrapeLoop) stop() {
|
|
|
|
sl.cancel()
|
|
|
|
<-sl.done
|
|
|
|
}
|
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
// wrapAppender wraps a SampleAppender for relabeling. It returns the wrappend
|
|
|
|
// appender and an innermost countingAppender that counts the samples actually
|
|
|
|
// appended in the end.
|
|
|
|
func (sl *scrapeLoop) wrapAppender(app storage.SampleAppender) (storage.SampleAppender, *countingAppender) {
|
|
|
|
// Innermost appender is a countingAppender to count how many samples
|
|
|
|
// are left in the end.
|
|
|
|
countingAppender := &countingAppender{
|
|
|
|
SampleAppender: app,
|
|
|
|
}
|
|
|
|
app = countingAppender
|
|
|
|
|
|
|
|
// The relabelAppender has to be inside the label-modifying appenders so
|
|
|
|
// the relabeling rules are applied to the correct label set.
|
|
|
|
if len(sl.metricRelabelConfigs) > 0 {
|
|
|
|
app = relabelAppender{
|
|
|
|
SampleAppender: app,
|
|
|
|
relabelings: sl.metricRelabelConfigs,
|
2016-12-13 08:18:17 -08:00
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
}
|
2016-12-13 08:18:17 -08:00
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
if sl.honorLabels {
|
|
|
|
app = honorLabelsAppender{
|
|
|
|
SampleAppender: app,
|
|
|
|
labels: sl.targetLabels,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app = ruleLabelsAppender{
|
|
|
|
SampleAppender: app,
|
|
|
|
labels: sl.targetLabels,
|
2016-12-16 07:08:50 -08:00
|
|
|
}
|
2016-12-13 08:18:17 -08:00
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
return app, countingAppender
|
2016-12-13 08:18:17 -08:00
|
|
|
}
|
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
func (sl *scrapeLoop) append(samples model.Samples) (int, error) {
|
2016-04-25 07:43:52 -07:00
|
|
|
var (
|
|
|
|
numOutOfOrder = 0
|
|
|
|
numDuplicates = 0
|
2017-01-07 08:28:49 -08:00
|
|
|
app = sl.appender
|
|
|
|
countingApp *countingAppender
|
2016-04-25 07:43:52 -07:00
|
|
|
)
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
if sl.sampleLimit > 0 {
|
|
|
|
// We need to check for the sample limit, so append everything
|
|
|
|
// to a wrapped bufferAppender first. Then point samples to the
|
|
|
|
// result.
|
|
|
|
bufApp := &bufferAppender{buffer: make(model.Samples, 0, len(samples))}
|
|
|
|
var wrappedBufApp storage.SampleAppender
|
|
|
|
wrappedBufApp, countingApp = sl.wrapAppender(bufApp)
|
|
|
|
for _, s := range samples {
|
|
|
|
// Ignore errors as bufferedAppender always succeds.
|
|
|
|
wrappedBufApp.Append(s)
|
|
|
|
}
|
|
|
|
samples = bufApp.buffer
|
|
|
|
if uint(countingApp.count) > sl.sampleLimit {
|
|
|
|
targetScrapeSampleLimit.Inc()
|
|
|
|
return countingApp.count, fmt.Errorf(
|
|
|
|
"%d samples exceeded limit of %d", countingApp.count, sl.sampleLimit,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No need to check for sample limit. Wrap sl.appender directly.
|
|
|
|
app, countingApp = sl.wrapAppender(sl.appender)
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
for _, s := range samples {
|
2017-01-07 08:28:49 -08:00
|
|
|
if err := app.Append(s); err != nil {
|
2016-04-25 07:43:52 -07:00
|
|
|
switch err {
|
|
|
|
case local.ErrOutOfOrderSample:
|
2016-02-22 07:46:55 -08:00
|
|
|
numOutOfOrder++
|
2016-04-25 07:43:52 -07:00
|
|
|
log.With("sample", s).With("error", err).Debug("Sample discarded")
|
|
|
|
case local.ErrDuplicateSampleForTimestamp:
|
|
|
|
numDuplicates++
|
|
|
|
log.With("sample", s).With("error", err).Debug("Sample discarded")
|
|
|
|
default:
|
|
|
|
log.With("sample", s).With("error", err).Warn("Sample discarded")
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if numOutOfOrder > 0 {
|
|
|
|
log.With("numDropped", numOutOfOrder).Warn("Error on ingesting out-of-order samples")
|
|
|
|
}
|
2016-04-25 07:43:52 -07:00
|
|
|
if numDuplicates > 0 {
|
|
|
|
log.With("numDropped", numDuplicates).Warn("Error on ingesting samples with different value but same timestamp")
|
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
return countingApp.count, nil
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
2016-12-13 09:32:11 -08:00
|
|
|
func (sl *scrapeLoop) report(start time.Time, duration time.Duration, scrapedSamples, postRelabelSamples int, err error) {
|
2016-02-22 07:46:55 -08:00
|
|
|
sl.scraper.report(start, duration, err)
|
|
|
|
|
|
|
|
ts := model.TimeFromUnixNano(start.UnixNano())
|
|
|
|
|
|
|
|
var health model.SampleValue
|
|
|
|
if err == nil {
|
|
|
|
health = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
healthSample := &model.Sample{
|
|
|
|
Metric: model.Metric{
|
|
|
|
model.MetricNameLabel: scrapeHealthMetricName,
|
|
|
|
},
|
|
|
|
Timestamp: ts,
|
|
|
|
Value: health,
|
|
|
|
}
|
|
|
|
durationSample := &model.Sample{
|
|
|
|
Metric: model.Metric{
|
|
|
|
model.MetricNameLabel: scrapeDurationMetricName,
|
|
|
|
},
|
|
|
|
Timestamp: ts,
|
2016-07-07 06:24:35 -07:00
|
|
|
Value: model.SampleValue(duration.Seconds()),
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2016-10-26 09:43:01 -07:00
|
|
|
countSample := &model.Sample{
|
|
|
|
Metric: model.Metric{
|
|
|
|
model.MetricNameLabel: scrapeSamplesMetricName,
|
|
|
|
},
|
|
|
|
Timestamp: ts,
|
|
|
|
Value: model.SampleValue(scrapedSamples),
|
|
|
|
}
|
2016-12-13 09:32:11 -08:00
|
|
|
postRelabelSample := &model.Sample{
|
|
|
|
Metric: model.Metric{
|
|
|
|
model.MetricNameLabel: samplesPostRelabelMetricName,
|
|
|
|
},
|
|
|
|
Timestamp: ts,
|
|
|
|
Value: model.SampleValue(postRelabelSamples),
|
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2017-01-07 08:28:49 -08:00
|
|
|
reportAppender := ruleLabelsAppender{
|
|
|
|
SampleAppender: sl.appender,
|
|
|
|
labels: sl.targetLabels,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := reportAppender.Append(healthSample); err != nil {
|
2016-05-19 07:22:49 -07:00
|
|
|
log.With("sample", healthSample).With("error", err).Warn("Scrape health sample discarded")
|
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
if err := reportAppender.Append(durationSample); err != nil {
|
2016-05-19 07:22:49 -07:00
|
|
|
log.With("sample", durationSample).With("error", err).Warn("Scrape duration sample discarded")
|
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
if err := reportAppender.Append(countSample); err != nil {
|
2016-10-26 09:43:01 -07:00
|
|
|
log.With("sample", durationSample).With("error", err).Warn("Scrape sample count sample discarded")
|
|
|
|
}
|
2017-01-07 08:28:49 -08:00
|
|
|
if err := reportAppender.Append(postRelabelSample); err != nil {
|
|
|
|
log.With("sample", durationSample).With("error", err).Warn("Scrape sample count post-relabeling sample discarded")
|
2016-12-13 09:32:11 -08:00
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|