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 (
|
2017-02-22 04:00:51 -08:00
|
|
|
"bufio"
|
2017-01-15 08:33:07 -08:00
|
|
|
"bytes"
|
2017-02-22 04:00:51 -08:00
|
|
|
"compress/gzip"
|
2016-02-28 10:21:50 -08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-04-13 10:07:23 -07:00
|
|
|
"math"
|
2016-02-28 10:21:50 -08:00
|
|
|
"net/http"
|
2016-02-22 07:46:55 -08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-02-22 04:00:51 -08:00
|
|
|
"unsafe"
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/log"
|
2017-03-17 08:27:07 -07:00
|
|
|
|
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-12-29 00:27:30 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2017-01-15 08:33:07 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/textparse"
|
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2017-04-13 10:07:23 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/value"
|
2016-02-22 07:46:55 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
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-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.",
|
|
|
|
},
|
|
|
|
)
|
2017-08-02 05:10:18 -07:00
|
|
|
targetScrapeSampleDuplicate = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrapes_sample_duplicate_timestamp_total",
|
|
|
|
Help: "Total number of samples rejected due to duplicate timestamps but different values",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
targetScrapeSampleOutOfOrder = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrapes_sample_out_of_order_total",
|
|
|
|
Help: "Total number of samples rejected due to not being out of the expected order",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
targetScrapeSampleOutOfBounds = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrapes_sample_out_of_bounds_total",
|
|
|
|
Help: "Total number of samples rejected due to timestamp falling outside of the time bounds",
|
|
|
|
},
|
|
|
|
)
|
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)
|
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)
|
2017-08-02 05:10:18 -07:00
|
|
|
prometheus.MustRegister(targetScrapeSampleDuplicate)
|
|
|
|
prometheus.MustRegister(targetScrapeSampleOutOfOrder)
|
|
|
|
prometheus.MustRegister(targetScrapeSampleOutOfBounds)
|
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 {
|
2016-12-30 12:35:35 -08:00
|
|
|
appendable Appendable
|
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-05-16 06:04:37 -07:00
|
|
|
newLoop func(context.Context, scraper, func() storage.Appender, func() storage.Appender, log.Logger) loop
|
2017-06-16 03:22:44 -07:00
|
|
|
|
2017-07-04 05:55:33 -07:00
|
|
|
logger log.Logger
|
|
|
|
maxAheadTime time.Duration
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2016-02-17 10:33:17 -08:00
|
|
|
|
2017-06-16 03:22:44 -07:00
|
|
|
func newScrapePool(ctx context.Context, cfg *config.ScrapeConfig, app Appendable, logger log.Logger) *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.
|
2017-06-16 03:22:44 -07:00
|
|
|
logger.Errorf("Error creating HTTP client for job %q: %s", cfg.JobName, err)
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
2017-05-26 01:44:48 -07:00
|
|
|
|
|
|
|
newLoop := func(
|
|
|
|
ctx context.Context,
|
|
|
|
s scraper,
|
|
|
|
app, reportApp func() storage.Appender,
|
|
|
|
l log.Logger,
|
|
|
|
) loop {
|
|
|
|
return newScrapeLoop(ctx, s, app, reportApp, l)
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
return &scrapePool{
|
2017-07-04 05:55:33 -07:00
|
|
|
appendable: app,
|
|
|
|
config: cfg,
|
|
|
|
ctx: ctx,
|
|
|
|
client: client,
|
|
|
|
targets: map[uint64]*Target{},
|
|
|
|
loops: map[uint64]loop{},
|
|
|
|
newLoop: newLoop,
|
|
|
|
logger: logger,
|
|
|
|
maxAheadTime: 10 * time.Minute,
|
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.
|
2017-05-10 08:59:02 -07:00
|
|
|
// This method returns after all scrape loops that were stopped have stopped scraping.
|
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.
|
2017-06-16 03:22:44 -07:00
|
|
|
sp.logger.Errorf("Error creating HTTP client for job %q: %s", cfg.JobName, err)
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
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 (
|
|
|
|
t = sp.targets[fp]
|
2017-04-27 01:19:55 -07:00
|
|
|
s = &targetScraper{Target: t, client: sp.client, timeout: timeout}
|
2017-01-13 05:48:01 -08:00
|
|
|
newLoop = sp.newLoop(sp.ctx, s,
|
|
|
|
func() storage.Appender {
|
|
|
|
return sp.sampleAppender(t)
|
|
|
|
},
|
|
|
|
func() storage.Appender {
|
|
|
|
return sp.reportAppender(t)
|
|
|
|
},
|
2017-06-16 03:22:44 -07:00
|
|
|
sp.logger.With("target", t.labels.String()),
|
2017-01-13 05:48:01 -08:00
|
|
|
)
|
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 {
|
2017-06-16 03:22:44 -07:00
|
|
|
sp.logger.With("err", err).Error("creating targets failed")
|
2016-11-22 03:48:30 -08:00
|
|
|
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 {
|
2017-01-13 05:48:01 -08:00
|
|
|
t := t
|
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-27 01:19:55 -07:00
|
|
|
s := &targetScraper{Target: t, client: sp.client, timeout: timeout}
|
2017-01-13 05:48:01 -08:00
|
|
|
l := sp.newLoop(sp.ctx, s,
|
|
|
|
func() storage.Appender {
|
|
|
|
return sp.sampleAppender(t)
|
|
|
|
},
|
|
|
|
func() storage.Appender {
|
|
|
|
return sp.reportAppender(t)
|
|
|
|
},
|
2017-06-16 03:22:44 -07:00
|
|
|
sp.logger.With("target", t.labels.String()),
|
2017-01-13 05:48:01 -08:00
|
|
|
)
|
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-28 00:51:02 -08:00
|
|
|
// sampleAppender returns an appender for ingested samples from the target.
|
2016-12-29 00:27:30 -08:00
|
|
|
func (sp *scrapePool) sampleAppender(target *Target) storage.Appender {
|
2016-12-30 12:35:35 -08:00
|
|
|
app, err := sp.appendable.Appender()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-01-30 08:30:28 -08:00
|
|
|
|
2017-07-04 05:55:33 -07:00
|
|
|
if sp.maxAheadTime > 0 {
|
|
|
|
app = &timeLimitAppender{
|
|
|
|
Appender: app,
|
|
|
|
maxTime: timestamp.FromTime(time.Now().Add(sp.maxAheadTime)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 08:30:28 -08:00
|
|
|
// The limit is applied after metrics are potentially dropped via relabeling.
|
|
|
|
if sp.config.SampleLimit > 0 {
|
|
|
|
app = &limitAppender{
|
|
|
|
Appender: app,
|
|
|
|
limit: int(sp.config.SampleLimit),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 00:51:02 -08:00
|
|
|
// The relabelAppender has to be inside the label-modifying appenders
|
|
|
|
// so the relabeling rules are applied to the correct label set.
|
|
|
|
if mrc := sp.config.MetricRelabelConfigs; len(mrc) > 0 {
|
|
|
|
app = relabelAppender{
|
2016-12-29 00:27:30 -08:00
|
|
|
Appender: app,
|
|
|
|
relabelings: mrc,
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sp.config.HonorLabels {
|
|
|
|
app = honorLabelsAppender{
|
2016-12-29 00:27:30 -08:00
|
|
|
Appender: app,
|
|
|
|
labels: target.Labels(),
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app = ruleLabelsAppender{
|
2016-12-29 00:27:30 -08:00
|
|
|
Appender: app,
|
|
|
|
labels: target.Labels(),
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// reportAppender returns an appender for reporting samples for the target.
|
2016-12-29 00:27:30 -08:00
|
|
|
func (sp *scrapePool) reportAppender(target *Target) storage.Appender {
|
2016-12-30 12:35:35 -08:00
|
|
|
app, err := sp.appendable.Appender()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-02-28 00:51:02 -08:00
|
|
|
return ruleLabelsAppender{
|
2016-12-30 12:35:35 -08:00
|
|
|
Appender: app,
|
2016-12-29 00:27:30 -08:00
|
|
|
labels: target.Labels(),
|
2016-02-28 00:51:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:58:46 -08:00
|
|
|
// A scraper retrieves samples and accepts a status report at the end.
|
|
|
|
type scraper interface {
|
2017-01-15 08:33:07 -08:00
|
|
|
scrape(ctx context.Context, w io.Writer) error
|
2016-02-25 04:58:46 -08:00
|
|
|
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
|
2017-04-27 01:19:55 -07:00
|
|
|
req *http.Request
|
2017-04-04 10:26:28 -07:00
|
|
|
timeout time.Duration
|
2017-01-15 08:33:07 -08:00
|
|
|
|
2017-02-22 04:00:51 -08:00
|
|
|
gzipr *gzip.Reader
|
|
|
|
buf *bufio.Reader
|
2017-01-15 08:33:07 -08:00
|
|
|
}
|
|
|
|
|
2017-02-22 04:00:51 -08: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`
|
2017-01-15 08:33:07 -08:00
|
|
|
|
2017-02-28 05:59:33 -08:00
|
|
|
var userAgentHeader = fmt.Sprintf("Prometheus/%s", version.Version)
|
|
|
|
|
2017-01-15 08:33:07 -08:00
|
|
|
func (s *targetScraper) scrape(ctx context.Context, w io.Writer) error {
|
2017-02-22 04:00:51 -08:00
|
|
|
if s.req == nil {
|
|
|
|
req, err := http.NewRequest("GET", s.URL().String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Disable accept header to always negotiate for text format.
|
2017-04-27 02:46:08 -07:00
|
|
|
// req.Header.Add("Accept", acceptHeader)
|
2017-02-22 04:00:51 -08:00
|
|
|
req.Header.Add("Accept-Encoding", "gzip")
|
2017-03-17 08:27:07 -07:00
|
|
|
req.Header.Set("User-Agent", userAgentHeader)
|
2017-04-27 01:19:55 -07:00
|
|
|
req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", fmt.Sprintf("%f", s.timeout.Seconds()))
|
2017-02-22 04:00:51 -08:00
|
|
|
|
|
|
|
s.req = req
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
2017-08-09 07:30:49 -07:00
|
|
|
|
2017-02-22 04:00:51 -08:00
|
|
|
resp, err := ctxhttp.Do(ctx, s.client, s.req)
|
2016-02-28 10:21:50 -08:00
|
|
|
if err != nil {
|
2017-01-15 08:33:07 -08:00
|
|
|
return err
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-01-15 08:33:07 -08:00
|
|
|
return fmt.Errorf("server returned HTTP status %s", resp.Status)
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
|
|
|
|
2017-02-22 04:00:51 -08:00
|
|
|
if resp.Header.Get("Content-Encoding") != "gzip" {
|
|
|
|
_, err = io.Copy(w, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.gzipr == nil {
|
|
|
|
s.buf = bufio.NewReader(resp.Body)
|
|
|
|
s.gzipr, err = gzip.NewReader(s.buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.buf.Reset(resp.Body)
|
|
|
|
s.gzipr.Reset(s.buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, s.gzipr)
|
|
|
|
s.gzipr.Close()
|
2017-01-15 08:33:07 -08:00
|
|
|
return err
|
2016-02-28 10:21:50 -08:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:07:23 -07:00
|
|
|
type lsetCacheEntry struct {
|
2017-06-25 23:56:40 -07:00
|
|
|
metric string
|
|
|
|
lset labels.Labels
|
|
|
|
hash uint64
|
2017-05-25 23:44:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type refEntry struct {
|
|
|
|
ref string
|
|
|
|
lastIter uint64
|
2017-04-13 10:07:23 -07:00
|
|
|
}
|
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
type scrapeLoop struct {
|
|
|
|
scraper scraper
|
2017-05-16 06:04:37 -07:00
|
|
|
l log.Logger
|
2017-05-26 01:44:48 -07:00
|
|
|
cache *scrapeCache
|
2017-05-25 23:44:24 -07:00
|
|
|
|
2017-01-13 05:48:01 -08:00
|
|
|
appender func() storage.Appender
|
|
|
|
reportAppender func() storage.Appender
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2017-05-26 01:44:48 -07:00
|
|
|
ctx context.Context
|
|
|
|
scrapeCtx context.Context
|
|
|
|
cancel func()
|
|
|
|
stopped chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scrapeCache tracks mappings of exposed metric strings to label sets and
|
|
|
|
// storage references. Additionally, it tracks staleness of series between
|
|
|
|
// scrapes.
|
|
|
|
type scrapeCache struct {
|
|
|
|
iter uint64 // Current scrape iteration.
|
|
|
|
|
|
|
|
refs map[string]*refEntry // Parsed string to ref.
|
|
|
|
lsets map[string]*lsetCacheEntry // Ref to labelset and string.
|
2017-05-24 07:23:48 -07:00
|
|
|
|
|
|
|
// seriesCur and seriesPrev store the labels of series that were seen
|
|
|
|
// in the current and previous scrape.
|
2017-05-24 08:05:42 -07:00
|
|
|
// We hold two maps and swap them out to save allocations.
|
2017-05-25 23:44:24 -07:00
|
|
|
seriesCur map[uint64]labels.Labels
|
|
|
|
seriesPrev map[uint64]labels.Labels
|
2017-05-26 01:44:48 -07:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
|
2017-05-26 01:44:48 -07:00
|
|
|
func newScrapeCache() *scrapeCache {
|
|
|
|
return &scrapeCache{
|
|
|
|
refs: map[string]*refEntry{},
|
|
|
|
lsets: map[string]*lsetCacheEntry{},
|
|
|
|
seriesCur: map[uint64]labels.Labels{},
|
|
|
|
seriesPrev: map[uint64]labels.Labels{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scrapeCache) iterDone() {
|
|
|
|
// refCache and lsetCache may grow over time through series churn
|
|
|
|
// or multiple string representations of the same metric. Clean up entries
|
|
|
|
// that haven't appeared in the last scrape.
|
|
|
|
for s, e := range c.refs {
|
|
|
|
if e.lastIter < c.iter {
|
|
|
|
delete(c.refs, s)
|
|
|
|
delete(c.lsets, e.ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap current and previous series.
|
|
|
|
c.seriesPrev, c.seriesCur = c.seriesCur, c.seriesPrev
|
|
|
|
|
|
|
|
// We have to delete every single key in the map.
|
|
|
|
for k := range c.seriesCur {
|
|
|
|
delete(c.seriesCur, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.iter++
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
2017-05-26 01:44:48 -07:00
|
|
|
func (c *scrapeCache) getRef(met string) (string, bool) {
|
|
|
|
e, ok := c.refs[met]
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
e.lastIter = c.iter
|
|
|
|
return e.ref, true
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:56:40 -07:00
|
|
|
func (c *scrapeCache) addRef(met, ref string, lset labels.Labels, hash uint64) {
|
|
|
|
if ref == "" {
|
|
|
|
return
|
|
|
|
}
|
2017-06-25 15:24:54 -07:00
|
|
|
// Clean up the label set cache before overwriting the ref for a previously seen
|
|
|
|
// metric representation. It won't be caught by the cleanup in iterDone otherwise.
|
|
|
|
if e, ok := c.refs[met]; ok {
|
|
|
|
delete(c.lsets, e.ref)
|
|
|
|
}
|
2017-05-26 01:44:48 -07:00
|
|
|
c.refs[met] = &refEntry{ref: ref, lastIter: c.iter}
|
|
|
|
// met is the raw string the metric was ingested as. The label set is not ordered
|
|
|
|
// and thus it's not suitable to uniquely identify cache entries.
|
|
|
|
// We store a hash over the label set instead.
|
2017-06-25 23:56:40 -07:00
|
|
|
c.lsets[ref] = &lsetCacheEntry{metric: met, lset: lset, hash: hash}
|
2017-05-26 01:44:48 -07:00
|
|
|
}
|
|
|
|
|
2017-06-25 23:56:40 -07:00
|
|
|
func (c *scrapeCache) trackStaleness(hash uint64, lset labels.Labels) {
|
|
|
|
c.seriesCur[hash] = lset
|
2017-05-26 01:44:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scrapeCache) forEachStale(f func(labels.Labels) bool) {
|
|
|
|
for h, lset := range c.seriesPrev {
|
|
|
|
if _, ok := c.seriesCur[h]; !ok {
|
|
|
|
if !f(lset) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newScrapeLoop(
|
|
|
|
ctx context.Context,
|
|
|
|
sc scraper,
|
|
|
|
app, reportApp func() storage.Appender,
|
|
|
|
l log.Logger,
|
|
|
|
) *scrapeLoop {
|
2017-05-16 06:04:37 -07:00
|
|
|
if l == nil {
|
|
|
|
l = log.Base()
|
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
sl := &scrapeLoop{
|
|
|
|
scraper: sc,
|
|
|
|
appender: app,
|
2017-05-26 01:44:48 -07:00
|
|
|
cache: newScrapeCache(),
|
2016-02-22 07:46:55 -08:00
|
|
|
reportAppender: reportApp,
|
2017-05-10 08:59:02 -07:00
|
|
|
stopped: make(chan struct{}),
|
|
|
|
ctx: ctx,
|
2017-05-16 06:04:37 -07:00
|
|
|
l: l,
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2017-05-10 08:59:02 -07:00
|
|
|
sl.scrapeCtx, sl.cancel = context.WithCancel(ctx)
|
2016-02-22 07:46:55 -08:00
|
|
|
|
|
|
|
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
|
|
|
select {
|
|
|
|
case <-time.After(sl.scraper.offset(interval)):
|
|
|
|
// Continue after a scraping offset.
|
2017-05-10 08:59:02 -07:00
|
|
|
case <-sl.scrapeCtx.Done():
|
|
|
|
close(sl.stopped)
|
2016-02-22 07:46:55 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var last time.Time
|
|
|
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2017-02-22 04:00:51 -08:00
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, 16000))
|
|
|
|
|
2017-05-10 08:59:02 -07:00
|
|
|
mainLoop:
|
2016-02-22 07:46:55 -08:00
|
|
|
for {
|
2017-02-22 04:00:51 -08:00
|
|
|
buf.Reset()
|
2016-02-22 07:46:55 -08:00
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
2017-05-10 08:59:02 -07:00
|
|
|
close(sl.stopped)
|
2016-02-22 07:46:55 -08:00
|
|
|
return
|
2017-05-10 08:59:02 -07:00
|
|
|
case <-sl.scrapeCtx.Done():
|
|
|
|
break mainLoop
|
2016-02-22 07:46:55 -08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-12-29 00:27:30 -08:00
|
|
|
var (
|
2017-05-11 08:20:03 -07:00
|
|
|
start = time.Now()
|
|
|
|
scrapeCtx, cancel = context.WithTimeout(sl.ctx, timeout)
|
2016-12-29 00:27:30 -08:00
|
|
|
)
|
2016-02-22 09:49:26 -08:00
|
|
|
|
2016-12-29 00:27:30 -08:00
|
|
|
// Only record after the first scrape.
|
|
|
|
if !last.IsZero() {
|
|
|
|
targetIntervalLength.WithLabelValues(interval.String()).Observe(
|
|
|
|
time.Since(last).Seconds(),
|
|
|
|
)
|
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2017-06-14 19:08:03 -07:00
|
|
|
scrapeErr := sl.scraper.scrape(scrapeCtx, buf)
|
2017-05-11 08:20:03 -07:00
|
|
|
cancel()
|
2017-05-03 06:55:35 -07:00
|
|
|
var b []byte
|
2017-06-14 19:08:03 -07:00
|
|
|
if scrapeErr == nil {
|
2017-05-03 06:55:35 -07:00
|
|
|
b = buf.Bytes()
|
2016-12-29 00:27:30 -08:00
|
|
|
} else if errc != nil {
|
2017-06-14 19:08:03 -07:00
|
|
|
errc <- scrapeErr
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2017-06-14 19:08:03 -07:00
|
|
|
|
2017-05-03 06:55:35 -07:00
|
|
|
// A failed scrape is the same as an empty scrape,
|
|
|
|
// we still call sl.append to trigger stale markers.
|
2017-06-14 19:08:03 -07:00
|
|
|
total, added, appErr := sl.append(b, start)
|
|
|
|
if appErr != nil {
|
|
|
|
sl.l.With("err", appErr).Warn("append failed")
|
2017-05-29 06:08:55 -07:00
|
|
|
// The append failed, probably due to a parse error or sample limit.
|
2017-05-03 08:51:45 -07:00
|
|
|
// Call sl.append again with an empty scrape to trigger stale markers.
|
2017-05-29 06:08:55 -07:00
|
|
|
if _, _, err := sl.append([]byte{}, start); err != nil {
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("err", err).Error("append failed")
|
2017-05-03 08:51:45 -07:00
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
2017-06-14 19:08:03 -07:00
|
|
|
if scrapeErr == nil {
|
|
|
|
scrapeErr = appErr
|
|
|
|
}
|
|
|
|
|
|
|
|
sl.report(start, time.Since(start), total, added, scrapeErr)
|
2016-12-29 00:27:30 -08:00
|
|
|
last = start
|
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
2017-05-10 08:59:02 -07:00
|
|
|
close(sl.stopped)
|
2016-02-22 07:46:55 -08:00
|
|
|
return
|
2017-05-10 08:59:02 -07:00
|
|
|
case <-sl.scrapeCtx.Done():
|
|
|
|
break mainLoop
|
2016-02-22 07:46:55 -08:00
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 08:59:02 -07:00
|
|
|
|
|
|
|
close(sl.stopped)
|
|
|
|
|
2017-05-16 05:12:21 -07:00
|
|
|
sl.endOfRunStaleness(last, ticker, interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sl *scrapeLoop) endOfRunStaleness(last time.Time, ticker *time.Ticker, interval time.Duration) {
|
2017-05-10 08:59:02 -07:00
|
|
|
// Scraping has stopped. We want to write stale markers but
|
|
|
|
// the target may be recreated, so we wait just over 2 scrape intervals
|
|
|
|
// before creating them.
|
|
|
|
// If the context is cancelled, we presume the server is shutting down
|
|
|
|
// and will restart where is was. We do not attempt to write stale markers
|
|
|
|
// in this case.
|
|
|
|
|
|
|
|
if last.IsZero() {
|
|
|
|
// There never was a scrape, so there will be no stale markers.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for when the next scrape would have been, record its timestamp.
|
|
|
|
var staleTime time.Time
|
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
staleTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for when the next scrape would have been, if the target was recreated
|
|
|
|
// samples should have been ingested by now.
|
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for an extra 10% of the interval, just to be safe.
|
|
|
|
select {
|
|
|
|
case <-sl.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(interval / 10):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call sl.append again with an empty scrape to trigger stale markers.
|
|
|
|
// If the target has since been recreated and scraped, the
|
|
|
|
// stale markers will be out of order and ignored.
|
|
|
|
if _, _, err := sl.append([]byte{}, staleTime); err != nil {
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("err", err).Error("stale append failed")
|
2017-05-10 08:59:02 -07:00
|
|
|
}
|
2017-05-11 06:43:43 -07:00
|
|
|
if err := sl.reportStale(staleTime); err != nil {
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("err", err).Error("stale report failed")
|
2017-05-11 06:43:43 -07:00
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
2017-05-10 08:59:02 -07:00
|
|
|
// Stop the scraping. May still write data and stale markers after it has
|
|
|
|
// returned. Cancel the context to stop all writes.
|
2016-02-22 07:46:55 -08:00
|
|
|
func (sl *scrapeLoop) stop() {
|
|
|
|
sl.cancel()
|
2017-05-10 08:59:02 -07:00
|
|
|
<-sl.stopped
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 00:27:30 -08:00
|
|
|
type sample struct {
|
|
|
|
metric labels.Labels
|
|
|
|
t int64
|
|
|
|
v float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type samples []sample
|
|
|
|
|
|
|
|
func (s samples) Len() int { return len(s) }
|
|
|
|
func (s samples) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
func (s samples) Less(i, j int) bool {
|
|
|
|
d := labels.Compare(s[i].metric, s[j].metric)
|
|
|
|
if d < 0 {
|
|
|
|
return true
|
|
|
|
} else if d > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return s[i].t < s[j].t
|
|
|
|
}
|
|
|
|
|
2017-01-30 08:30:28 -08:00
|
|
|
func (sl *scrapeLoop) append(b []byte, ts time.Time) (total, added int, err error) {
|
2016-04-25 07:43:52 -07:00
|
|
|
var (
|
2017-07-04 02:24:13 -07:00
|
|
|
app = sl.appender()
|
|
|
|
p = textparse.New(b)
|
|
|
|
defTime = timestamp.FromTime(ts)
|
|
|
|
numOutOfOrder = 0
|
|
|
|
numDuplicates = 0
|
|
|
|
numOutOfBounds = 0
|
2016-04-25 07:43:52 -07:00
|
|
|
)
|
2017-05-29 06:08:55 -07:00
|
|
|
var sampleLimitErr error
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2017-02-01 06:59:37 -08:00
|
|
|
loop:
|
2017-01-15 08:33:07 -08:00
|
|
|
for p.Next() {
|
2017-01-30 08:30:28 -08:00
|
|
|
total++
|
|
|
|
|
2017-01-15 08:33:07 -08:00
|
|
|
t := defTime
|
|
|
|
met, tp, v := p.At()
|
|
|
|
if tp != nil {
|
|
|
|
t = *tp
|
|
|
|
}
|
|
|
|
|
2017-05-26 01:44:48 -07:00
|
|
|
ref, ok := sl.cache.getRef(yoloString(met))
|
2017-01-15 08:33:07 -08:00
|
|
|
if ok {
|
2017-07-12 07:54:38 -07:00
|
|
|
lset := sl.cache.lsets[ref].lset
|
2017-07-12 04:41:27 -07:00
|
|
|
switch err = app.AddFast(lset, ref, t, v); err {
|
2017-02-01 06:59:37 -08:00
|
|
|
case nil:
|
2017-05-24 07:23:48 -07:00
|
|
|
if tp == nil {
|
2017-06-25 23:56:40 -07:00
|
|
|
e := sl.cache.lsets[ref]
|
|
|
|
sl.cache.trackStaleness(e.hash, e.lset)
|
2017-05-24 07:23:48 -07:00
|
|
|
}
|
2017-02-01 06:59:37 -08:00
|
|
|
case storage.ErrNotFound:
|
|
|
|
ok = false
|
|
|
|
case errSeriesDropped:
|
2017-05-05 05:58:36 -07:00
|
|
|
err = nil
|
2017-01-16 23:39:18 -08:00
|
|
|
continue
|
2017-05-10 06:44:13 -07:00
|
|
|
case storage.ErrOutOfOrderSample:
|
2017-05-25 23:44:24 -07:00
|
|
|
numOutOfOrder++
|
2017-08-02 05:10:18 -07:00
|
|
|
sl.l.With("timeseries", string(met)).Debug("Out of order sample")
|
|
|
|
targetScrapeSampleOutOfOrder.Inc()
|
2017-05-10 06:44:13 -07:00
|
|
|
continue
|
|
|
|
case storage.ErrDuplicateSampleForTimestamp:
|
2017-05-25 23:44:24 -07:00
|
|
|
numDuplicates++
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("timeseries", string(met)).Debug("Duplicate sample for timestamp")
|
2017-08-02 05:10:18 -07:00
|
|
|
targetScrapeSampleDuplicate.Inc()
|
2017-01-16 23:39:18 -08:00
|
|
|
continue
|
2017-07-04 02:24:13 -07:00
|
|
|
case storage.ErrOutOfBounds:
|
|
|
|
numOutOfBounds++
|
|
|
|
sl.l.With("timeseries", string(met)).Debug("Out of bounds metric")
|
2017-08-02 05:10:18 -07:00
|
|
|
targetScrapeSampleOutOfBounds.Inc()
|
2017-07-04 02:24:13 -07:00
|
|
|
continue
|
2017-05-29 06:08:55 -07:00
|
|
|
case errSampleLimit:
|
|
|
|
// Keep on parsing output if we hit the limit, so we report the correct
|
|
|
|
// total number of samples scraped.
|
|
|
|
sampleLimitErr = err
|
|
|
|
added++
|
|
|
|
continue
|
2017-02-01 06:59:37 -08:00
|
|
|
default:
|
|
|
|
break loop
|
2017-01-15 08:33:07 -08:00
|
|
|
}
|
2017-01-13 05:48:01 -08:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
if !ok {
|
2017-06-25 23:56:40 -07:00
|
|
|
var (
|
|
|
|
lset labels.Labels
|
|
|
|
mets string
|
|
|
|
hash uint64
|
|
|
|
)
|
|
|
|
if e, ok := sl.cache.lsets[ref]; ok {
|
|
|
|
mets = e.metric
|
|
|
|
lset = e.lset
|
|
|
|
hash = e.hash
|
|
|
|
} else {
|
|
|
|
mets = p.Metric(&lset)
|
|
|
|
hash = lset.Hash()
|
|
|
|
}
|
2017-01-30 08:30:28 -08:00
|
|
|
|
2017-05-25 23:44:24 -07:00
|
|
|
var ref string
|
2017-02-01 06:59:37 -08:00
|
|
|
ref, err = app.Add(lset, t, v)
|
2017-01-30 08:30:28 -08:00
|
|
|
// TODO(fabxc): also add a dropped-cache?
|
2017-02-01 06:59:37 -08:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case errSeriesDropped:
|
2017-05-05 05:58:36 -07:00
|
|
|
err = nil
|
2017-01-30 08:30:28 -08:00
|
|
|
continue
|
2017-05-10 06:44:13 -07:00
|
|
|
case storage.ErrOutOfOrderSample:
|
|
|
|
err = nil
|
2017-05-25 23:44:24 -07:00
|
|
|
numOutOfOrder++
|
2017-08-02 05:10:18 -07:00
|
|
|
sl.l.With("timeseries", string(met)).Debug("Out of order sample")
|
|
|
|
targetScrapeSampleOutOfOrder.Inc()
|
2017-05-10 06:44:13 -07:00
|
|
|
continue
|
|
|
|
case storage.ErrDuplicateSampleForTimestamp:
|
|
|
|
err = nil
|
2017-05-25 23:44:24 -07:00
|
|
|
numDuplicates++
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("timeseries", string(met)).Debug("Duplicate sample for timestamp")
|
2017-08-02 05:10:18 -07:00
|
|
|
targetScrapeSampleDuplicate.Inc()
|
2017-01-30 08:30:28 -08:00
|
|
|
continue
|
2017-07-04 02:24:13 -07:00
|
|
|
case storage.ErrOutOfBounds:
|
2017-07-04 05:55:33 -07:00
|
|
|
err = nil
|
2017-07-04 02:24:13 -07:00
|
|
|
numOutOfBounds++
|
|
|
|
sl.l.With("timeseries", string(met)).Debug("Out of bounds metric")
|
2017-08-02 05:10:18 -07:00
|
|
|
targetScrapeSampleOutOfBounds.Inc()
|
2017-07-04 02:24:13 -07:00
|
|
|
continue
|
2017-05-29 06:08:55 -07:00
|
|
|
case errSampleLimit:
|
|
|
|
sampleLimitErr = err
|
|
|
|
added++
|
|
|
|
continue
|
2017-02-01 06:59:37 -08:00
|
|
|
default:
|
|
|
|
break loop
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2017-04-28 08:36:36 -07:00
|
|
|
if tp == nil {
|
|
|
|
// Bypass staleness logic if there is an explicit timestamp.
|
2017-06-25 23:56:40 -07:00
|
|
|
sl.cache.trackStaleness(hash, lset)
|
2017-04-28 08:36:36 -07:00
|
|
|
}
|
2017-06-25 23:56:40 -07:00
|
|
|
sl.cache.addRef(mets, ref, lset, hash)
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2017-02-01 06:59:37 -08:00
|
|
|
added++
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
if err == nil {
|
|
|
|
err = p.Err()
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
2017-05-29 06:08:55 -07:00
|
|
|
if err == nil && sampleLimitErr != nil {
|
|
|
|
targetScrapeSampleLimit.Inc()
|
|
|
|
err = sampleLimitErr
|
|
|
|
}
|
2017-05-16 05:30:40 -07:00
|
|
|
if numOutOfOrder > 0 {
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("numDropped", numOutOfOrder).Warn("Error on ingesting out-of-order samples")
|
2017-05-16 05:30:40 -07:00
|
|
|
}
|
|
|
|
if numDuplicates > 0 {
|
2017-05-16 06:04:37 -07:00
|
|
|
sl.l.With("numDropped", numDuplicates).Warn("Error on ingesting samples with different value but same timestamp")
|
2017-05-16 05:30:40 -07:00
|
|
|
}
|
2017-07-04 02:24:13 -07:00
|
|
|
if numOutOfBounds > 0 {
|
2017-07-06 05:18:31 -07:00
|
|
|
sl.l.With("numOutOfBounds", numOutOfBounds).Warn("Error on ingesting samples that are too old or are too far into the future")
|
2017-07-04 02:24:13 -07:00
|
|
|
}
|
2017-04-13 10:07:23 -07:00
|
|
|
if err == nil {
|
2017-05-26 01:44:48 -07:00
|
|
|
sl.cache.forEachStale(func(lset labels.Labels) bool {
|
|
|
|
// Series no longer exposed, mark it stale.
|
|
|
|
_, err = app.Add(lset, defTime, math.Float64frombits(value.StaleNaN))
|
|
|
|
switch err {
|
|
|
|
case errSeriesDropped:
|
|
|
|
err = nil
|
|
|
|
case storage.ErrOutOfOrderSample, storage.ErrDuplicateSampleForTimestamp:
|
|
|
|
// Do not count these in logging, as this is expected if a target
|
|
|
|
// goes away and comes back again with a new scrape loop.
|
|
|
|
err = nil
|
2017-04-13 10:07:23 -07:00
|
|
|
}
|
2017-05-26 01:44:48 -07:00
|
|
|
return err == nil
|
|
|
|
})
|
2017-04-13 10:07:23 -07:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
if err != nil {
|
|
|
|
app.Rollback()
|
2017-05-29 06:08:55 -07:00
|
|
|
return total, added, err
|
2016-04-25 07:43:52 -07:00
|
|
|
}
|
2017-01-13 05:48:01 -08:00
|
|
|
if err := app.Commit(); err != nil {
|
2017-05-29 06:08:55 -07:00
|
|
|
return total, added, err
|
2016-12-29 00:27:30 -08:00
|
|
|
}
|
2017-05-24 07:23:48 -07:00
|
|
|
|
2017-05-26 01:44:48 -07:00
|
|
|
sl.cache.iterDone()
|
2017-05-24 07:23:48 -07:00
|
|
|
|
2017-01-30 08:30:28 -08:00
|
|
|
return total, added, nil
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|
|
|
|
|
2017-02-22 04:00:51 -08:00
|
|
|
func yoloString(b []byte) string {
|
2017-03-07 02:41:11 -08:00
|
|
|
return *((*string)(unsafe.Pointer(&b)))
|
2017-02-22 04:00:51 -08:00
|
|
|
}
|
|
|
|
|
2017-01-30 08:30:28 -08:00
|
|
|
func (sl *scrapeLoop) report(start time.Time, duration time.Duration, scraped, appended int, err error) error {
|
2016-02-22 07:46:55 -08:00
|
|
|
sl.scraper.report(start, duration, err)
|
|
|
|
|
2017-01-15 08:33:07 -08:00
|
|
|
ts := timestamp.FromTime(start)
|
2016-02-22 07:46:55 -08:00
|
|
|
|
2016-12-29 00:27:30 -08:00
|
|
|
var health float64
|
2016-02-22 07:46:55 -08:00
|
|
|
if err == nil {
|
|
|
|
health = 1
|
|
|
|
}
|
|
|
|
|
2017-01-13 05:48:01 -08:00
|
|
|
app := sl.reportAppender()
|
|
|
|
|
2017-01-15 08:33:07 -08:00
|
|
|
if err := sl.addReportSample(app, scrapeHealthMetricName, ts, health); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
2017-01-13 05:48:01 -08:00
|
|
|
}
|
2017-01-15 08:33:07 -08:00
|
|
|
if err := sl.addReportSample(app, scrapeDurationMetricName, ts, duration.Seconds()); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
2016-05-19 07:22:49 -07:00
|
|
|
}
|
2017-01-30 08:30:28 -08:00
|
|
|
if err := sl.addReportSample(app, scrapeSamplesMetricName, ts, float64(scraped)); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
2016-10-26 09:43:01 -07:00
|
|
|
}
|
2017-01-30 08:30:28 -08:00
|
|
|
if err := sl.addReportSample(app, samplesPostRelabelMetricName, ts, float64(appended)); err != nil {
|
2017-01-15 08:33:07 -08:00
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return app.Commit()
|
|
|
|
}
|
|
|
|
|
2017-05-11 06:43:43 -07:00
|
|
|
func (sl *scrapeLoop) reportStale(start time.Time) error {
|
|
|
|
ts := timestamp.FromTime(start)
|
|
|
|
app := sl.reportAppender()
|
|
|
|
stale := math.Float64frombits(value.StaleNaN)
|
|
|
|
|
|
|
|
if err := sl.addReportSample(app, scrapeHealthMetricName, ts, stale); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sl.addReportSample(app, scrapeDurationMetricName, ts, stale); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sl.addReportSample(app, scrapeSamplesMetricName, ts, stale); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := sl.addReportSample(app, samplesPostRelabelMetricName, ts, stale); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return app.Commit()
|
|
|
|
}
|
|
|
|
|
2017-01-15 08:33:07 -08:00
|
|
|
func (sl *scrapeLoop) addReportSample(app storage.Appender, s string, t int64, v float64) error {
|
2017-05-26 01:44:48 -07:00
|
|
|
// Suffix s with the invalid \xff unicode rune to avoid collisions
|
|
|
|
// with scraped metrics.
|
|
|
|
s2 := s + "\xff"
|
2017-01-15 08:33:07 -08:00
|
|
|
|
2017-05-26 01:44:48 -07:00
|
|
|
ref, ok := sl.cache.getRef(s2)
|
2017-01-15 08:33:07 -08:00
|
|
|
if ok {
|
2017-07-18 03:42:29 -07:00
|
|
|
lset := sl.cache.lsets[ref].lset
|
|
|
|
err := app.AddFast(lset, ref, t, v)
|
2017-05-11 06:43:43 -07:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-02-01 06:59:37 -08:00
|
|
|
return nil
|
2017-05-11 06:43:43 -07:00
|
|
|
case storage.ErrNotFound:
|
|
|
|
// Try an Add.
|
|
|
|
case storage.ErrOutOfOrderSample, storage.ErrDuplicateSampleForTimestamp:
|
|
|
|
// Do not log here, as this is expected if a target goes away and comes back
|
|
|
|
// again with a new scrape loop.
|
2017-02-01 06:59:37 -08:00
|
|
|
return nil
|
2017-05-11 06:43:43 -07:00
|
|
|
default:
|
2017-01-15 08:33:07 -08:00
|
|
|
return err
|
|
|
|
}
|
2017-01-13 05:48:01 -08:00
|
|
|
}
|
2017-07-18 03:42:29 -07:00
|
|
|
lset := labels.Labels{
|
2017-01-15 08:33:07 -08:00
|
|
|
labels.Label{Name: labels.MetricName, Value: s},
|
2016-05-19 07:22:49 -07:00
|
|
|
}
|
2017-07-18 03:42:29 -07:00
|
|
|
ref, err := app.Add(lset, t, v)
|
2017-05-11 06:43:43 -07:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-07-18 03:42:29 -07:00
|
|
|
sl.cache.addRef(s2, ref, lset, lset.Hash())
|
2017-05-11 06:43:43 -07:00
|
|
|
return nil
|
|
|
|
case storage.ErrOutOfOrderSample, storage.ErrDuplicateSampleForTimestamp:
|
|
|
|
return nil
|
|
|
|
default:
|
2017-01-15 08:33:07 -08:00
|
|
|
return err
|
2017-01-13 05:48:01 -08:00
|
|
|
}
|
2016-02-22 07:46:55 -08:00
|
|
|
}
|