2016-02-17 18:33:17 +00: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 09:55:07 +00:00
|
|
|
package scrape
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
import (
|
2017-02-22 12:00:51 +00:00
|
|
|
"bufio"
|
2017-01-15 16:33:07 +00:00
|
|
|
"bytes"
|
2017-02-22 12:00:51 +00:00
|
|
|
"compress/gzip"
|
2017-10-25 04:21:42 +00:00
|
|
|
"context"
|
2016-02-28 18:21:50 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-04-18 08:50:37 +00:00
|
|
|
"io/ioutil"
|
2017-04-13 17:07:23 +00:00
|
|
|
"math"
|
2016-02-28 18:21:50 +00:00
|
|
|
"net/http"
|
2016-02-22 15:46:55 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-02-22 12:00:51 +00:00
|
|
|
"unsafe"
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2019-02-13 13:24:22 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-02-22 15:46:55 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-04-25 17:19:06 +00:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2017-09-08 12:34:45 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2017-02-28 13:59:33 +00:00
|
|
|
"github.com/prometheus/common/version"
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-23 10:56:09 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 20:01:34 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2016-12-29 08:27:30 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2017-09-07 12:43:21 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/pool"
|
2017-09-08 12:34:45 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/relabel"
|
2017-01-15 16:33:07 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/textparse"
|
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2017-04-13 17:07:23 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/value"
|
2016-02-22 15:46:55 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
)
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
var (
|
|
|
|
targetIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2016-11-23 08:17:04 +00:00
|
|
|
Name: "prometheus_target_interval_length_seconds",
|
2016-02-22 15:46:55 +00: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 08:17:04 +00:00
|
|
|
[]string{"interval"},
|
2016-02-22 15:46:55 +00:00
|
|
|
)
|
2016-03-09 15:33:10 +00:00
|
|
|
targetReloadIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2016-11-23 08:17:04 +00:00
|
|
|
Name: "prometheus_target_reload_length_seconds",
|
2016-03-09 15:33:10 +00: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 08:17:04 +00:00
|
|
|
[]string{"interval"},
|
2016-03-09 15:33:10 +00:00
|
|
|
)
|
2019-02-13 13:24:22 +00:00
|
|
|
targetScrapePools = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrape_pools_total",
|
2019-07-29 16:08:54 +00:00
|
|
|
Help: "Total number of scrape pool creation attempts.",
|
2019-02-13 13:24:22 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
targetScrapePoolsFailed = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrape_pools_failed_total",
|
|
|
|
Help: "Total number of scrape pool creations that failed.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
targetScrapePoolReloads = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrape_pool_reloads_total",
|
|
|
|
Help: "Total number of scrape loop reloads.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
targetScrapePoolReloadsFailed = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrape_pool_reloads_failed_total",
|
|
|
|
Help: "Total number of failed scrape loop reloads.",
|
|
|
|
},
|
|
|
|
)
|
2016-03-11 11:22:23 +00:00
|
|
|
targetSyncIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2016-11-23 08:17:04 +00:00
|
|
|
Name: "prometheus_target_sync_length_seconds",
|
2016-03-11 11:22:23 +00: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 08:17:04 +00:00
|
|
|
[]string{"scrape_job"},
|
2016-03-11 11:22:23 +00:00
|
|
|
)
|
|
|
|
targetScrapePoolSyncsCounter = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
2016-11-23 08:17:04 +00:00
|
|
|
Name: "prometheus_target_scrape_pool_sync_total",
|
|
|
|
Help: "Total number of syncs that were executed on a scrape pool.",
|
2016-03-11 11:22:23 +00:00
|
|
|
},
|
2016-11-23 08:17:04 +00:00
|
|
|
[]string{"scrape_job"},
|
2016-03-11 11:22:23 +00:00
|
|
|
)
|
2016-12-16 15:08:50 +00: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 12:10:18 +00: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",
|
|
|
|
},
|
|
|
|
)
|
2019-03-28 17:52:46 +00:00
|
|
|
targetScrapeCacheFlushForced = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_target_scrapes_cache_flush_forced_total",
|
|
|
|
Help: "How many times a scrape cache was flushed due to getting big while scrapes are failing.",
|
|
|
|
},
|
|
|
|
)
|
2016-02-22 15:46:55 +00:00
|
|
|
)
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(targetIntervalLength)
|
2016-03-09 15:33:10 +00:00
|
|
|
prometheus.MustRegister(targetReloadIntervalLength)
|
2019-02-13 13:24:22 +00:00
|
|
|
prometheus.MustRegister(targetScrapePools)
|
|
|
|
prometheus.MustRegister(targetScrapePoolsFailed)
|
|
|
|
prometheus.MustRegister(targetScrapePoolReloads)
|
|
|
|
prometheus.MustRegister(targetScrapePoolReloadsFailed)
|
2016-03-11 11:22:23 +00:00
|
|
|
prometheus.MustRegister(targetSyncIntervalLength)
|
|
|
|
prometheus.MustRegister(targetScrapePoolSyncsCounter)
|
2016-12-16 15:08:50 +00:00
|
|
|
prometheus.MustRegister(targetScrapeSampleLimit)
|
2017-08-02 12:10:18 +00:00
|
|
|
prometheus.MustRegister(targetScrapeSampleDuplicate)
|
|
|
|
prometheus.MustRegister(targetScrapeSampleOutOfOrder)
|
|
|
|
prometheus.MustRegister(targetScrapeSampleOutOfBounds)
|
2019-03-28 17:52:46 +00:00
|
|
|
prometheus.MustRegister(targetScrapeCacheFlushForced)
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
// scrapePool manages scrapes for sets of targets.
|
|
|
|
type scrapePool struct {
|
2016-12-30 20:35:35 +00:00
|
|
|
appendable Appendable
|
2017-09-08 12:34:45 +00:00
|
|
|
logger log.Logger
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-28 18:21:50 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
config *config.ScrapeConfig
|
|
|
|
client *http.Client
|
2016-02-28 08:51:02 +00:00
|
|
|
// Targets and loops must always be synchronized to have the same
|
2016-02-28 18:56:18 +00:00
|
|
|
// set of hashes.
|
2018-09-26 09:20:56 +00:00
|
|
|
activeTargets map[uint64]*Target
|
2017-12-04 15:12:28 +00:00
|
|
|
droppedTargets []*Target
|
|
|
|
loops map[uint64]loop
|
2017-12-03 17:14:08 +00:00
|
|
|
cancel context.CancelFunc
|
2016-02-28 08:51:02 +00:00
|
|
|
|
|
|
|
// Constructor for new scrape loops. This is settable for testing convenience.
|
2019-03-12 10:26:18 +00:00
|
|
|
newLoop func(scrapeLoopOptions) loop
|
|
|
|
}
|
|
|
|
|
|
|
|
type scrapeLoopOptions struct {
|
2019-03-15 10:04:15 +00:00
|
|
|
target *Target
|
|
|
|
scraper scraper
|
|
|
|
limit int
|
|
|
|
honorLabels bool
|
|
|
|
honorTimestamps bool
|
|
|
|
mrc []*relabel.Config
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
const maxAheadTime = 10 * time.Minute
|
|
|
|
|
|
|
|
type labelsMutator func(labels.Labels) labels.Labels
|
|
|
|
|
2019-03-12 10:46:15 +00:00
|
|
|
func newScrapePool(cfg *config.ScrapeConfig, app Appendable, jitterSeed uint64, logger log.Logger) (*scrapePool, error) {
|
2019-02-13 13:24:22 +00:00
|
|
|
targetScrapePools.Inc()
|
2017-09-15 17:45:27 +00:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
|
|
|
|
2019-08-14 09:00:39 +00:00
|
|
|
client, err := config_util.NewClientFromConfig(cfg.HTTPClientConfig, cfg.JobName, false)
|
2016-02-28 18:21:50 +00:00
|
|
|
if err != nil {
|
2019-02-13 13:24:22 +00:00
|
|
|
targetScrapePoolsFailed.Inc()
|
|
|
|
return nil, errors.Wrap(err, "error creating HTTP client")
|
2016-02-28 18:21:50 +00:00
|
|
|
}
|
2017-05-26 08:44:48 +00:00
|
|
|
|
2018-03-15 02:15:35 +00:00
|
|
|
buffers := pool.New(1e3, 100e6, 3, func(sz int) interface{} { return make([]byte, 0, sz) })
|
2017-05-26 08:44:48 +00:00
|
|
|
|
2017-12-03 17:14:08 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-09-08 12:34:45 +00:00
|
|
|
sp := &scrapePool{
|
2018-09-26 09:20:56 +00:00
|
|
|
cancel: cancel,
|
|
|
|
appendable: app,
|
|
|
|
config: cfg,
|
|
|
|
client: client,
|
|
|
|
activeTargets: map[uint64]*Target{},
|
|
|
|
loops: map[uint64]loop{},
|
|
|
|
logger: logger,
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2019-03-12 10:26:18 +00:00
|
|
|
sp.newLoop = func(opts scrapeLoopOptions) loop {
|
2018-05-18 07:32:11 +00:00
|
|
|
// Update the targets retrieval function for metadata to a new scrape cache.
|
|
|
|
cache := newScrapeCache()
|
2019-03-12 10:26:18 +00:00
|
|
|
opts.target.setMetadataStore(cache)
|
2018-05-18 07:32:11 +00:00
|
|
|
|
2017-11-26 15:15:15 +00:00
|
|
|
return newScrapeLoop(
|
2017-12-03 17:14:08 +00:00
|
|
|
ctx,
|
2019-03-12 10:26:18 +00:00
|
|
|
opts.scraper,
|
|
|
|
log.With(logger, "target", opts.target),
|
2017-09-08 12:34:45 +00:00
|
|
|
buffers,
|
2019-03-12 10:26:18 +00:00
|
|
|
func(l labels.Labels) labels.Labels {
|
|
|
|
return mutateSampleLabels(l, opts.target, opts.honorLabels, opts.mrc)
|
|
|
|
},
|
|
|
|
func(l labels.Labels) labels.Labels { return mutateReportSampleLabels(l, opts.target) },
|
2018-04-12 14:54:53 +00:00
|
|
|
func() storage.Appender {
|
|
|
|
app, err := app.Appender()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-12 10:26:18 +00:00
|
|
|
return appender(app, opts.limit)
|
2018-04-12 14:54:53 +00:00
|
|
|
},
|
2018-05-18 07:32:11 +00:00
|
|
|
cache,
|
2019-03-12 10:46:15 +00:00
|
|
|
jitterSeed,
|
2019-03-15 10:04:15 +00:00
|
|
|
opts.honorTimestamps,
|
2017-09-08 12:34:45 +00:00
|
|
|
)
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
|
2019-02-13 13:24:22 +00:00
|
|
|
return sp, nil
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2018-09-26 09:20:56 +00:00
|
|
|
func (sp *scrapePool) ActiveTargets() []*Target {
|
|
|
|
sp.mtx.Lock()
|
|
|
|
defer sp.mtx.Unlock()
|
|
|
|
|
|
|
|
var tActive []*Target
|
|
|
|
for _, t := range sp.activeTargets {
|
|
|
|
tActive = append(tActive, t)
|
|
|
|
}
|
|
|
|
return tActive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *scrapePool) DroppedTargets() []*Target {
|
|
|
|
sp.mtx.Lock()
|
|
|
|
defer sp.mtx.Unlock()
|
|
|
|
return sp.droppedTargets
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
// stop terminates all scrape loops and returns after they all terminated.
|
2016-02-22 15:46:55 +00:00
|
|
|
func (sp *scrapePool) stop() {
|
2017-12-03 17:14:08 +00:00
|
|
|
sp.cancel()
|
2016-02-22 15:46:55 +00:00
|
|
|
var wg sync.WaitGroup
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-28 08:51:02 +00:00
|
|
|
sp.mtx.Lock()
|
|
|
|
defer sp.mtx.Unlock()
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-28 08:51:02 +00:00
|
|
|
for fp, l := range sp.loops {
|
2016-02-23 13:37:25 +00:00
|
|
|
wg.Add(1)
|
2016-02-17 18:33:17 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
go func(l loop) {
|
|
|
|
l.stop()
|
|
|
|
wg.Done()
|
|
|
|
}(l)
|
2016-02-28 08:51:02 +00:00
|
|
|
|
|
|
|
delete(sp.loops, fp)
|
2018-09-26 09:20:56 +00:00
|
|
|
delete(sp.activeTargets, fp)
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
2019-04-10 12:20:00 +00:00
|
|
|
sp.client.CloseIdleConnections()
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 13:37:25 +00: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 15:59:02 +00:00
|
|
|
// This method returns after all scrape loops that were stopped have stopped scraping.
|
2019-02-13 13:24:22 +00:00
|
|
|
func (sp *scrapePool) reload(cfg *config.ScrapeConfig) error {
|
|
|
|
targetScrapePoolReloads.Inc()
|
2016-03-09 15:33:10 +00:00
|
|
|
start := time.Now()
|
2016-11-22 11:48:30 +00:00
|
|
|
|
2016-02-23 12:34:24 +00:00
|
|
|
sp.mtx.Lock()
|
|
|
|
defer sp.mtx.Unlock()
|
|
|
|
|
2019-08-14 09:00:39 +00:00
|
|
|
client, err := config_util.NewClientFromConfig(cfg.HTTPClientConfig, cfg.JobName, false)
|
2016-02-28 18:21:50 +00:00
|
|
|
if err != nil {
|
2019-02-13 13:24:22 +00:00
|
|
|
targetScrapePoolReloadsFailed.Inc()
|
|
|
|
return errors.Wrap(err, "error creating HTTP client")
|
2016-02-28 18:21:50 +00:00
|
|
|
}
|
2016-02-23 12:34:24 +00:00
|
|
|
sp.config = cfg
|
2019-04-10 12:20:00 +00:00
|
|
|
oldClient := sp.client
|
2016-02-28 18:21:50 +00:00
|
|
|
sp.client = client
|
2016-02-23 12:34:24 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
var (
|
2019-03-15 10:04:15 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
interval = time.Duration(sp.config.ScrapeInterval)
|
|
|
|
timeout = time.Duration(sp.config.ScrapeTimeout)
|
|
|
|
limit = int(sp.config.SampleLimit)
|
|
|
|
honorLabels = sp.config.HonorLabels
|
|
|
|
honorTimestamps = sp.config.HonorTimestamps
|
|
|
|
mrc = sp.config.MetricRelabelConfigs
|
2016-02-23 13:37:25 +00:00
|
|
|
)
|
2016-02-23 12:34:24 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
for fp, oldLoop := range sp.loops {
|
|
|
|
var (
|
2018-09-26 09:20:56 +00:00
|
|
|
t = sp.activeTargets[fp]
|
2017-04-27 08:19:55 +00:00
|
|
|
s = &targetScraper{Target: t, client: sp.client, timeout: timeout}
|
2019-03-12 10:26:18 +00:00
|
|
|
newLoop = sp.newLoop(scrapeLoopOptions{
|
2019-03-15 10:04:15 +00:00
|
|
|
target: t,
|
|
|
|
scraper: s,
|
|
|
|
limit: limit,
|
|
|
|
honorLabels: honorLabels,
|
|
|
|
honorTimestamps: honorTimestamps,
|
|
|
|
mrc: mrc,
|
2019-03-12 10:26:18 +00:00
|
|
|
})
|
2016-02-23 13:37:25 +00:00
|
|
|
)
|
|
|
|
wg.Add(1)
|
2016-02-23 12:34:24 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
go func(oldLoop, newLoop loop) {
|
|
|
|
oldLoop.stop()
|
|
|
|
wg.Done()
|
2016-02-23 12:34:24 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
go newLoop.run(interval, timeout, nil)
|
|
|
|
}(oldLoop, newLoop)
|
|
|
|
|
|
|
|
sp.loops[fp] = newLoop
|
2016-02-23 12:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2019-04-10 12:20:00 +00:00
|
|
|
oldClient.CloseIdleConnections()
|
2016-03-09 15:33:10 +00:00
|
|
|
targetReloadIntervalLength.WithLabelValues(interval.String()).Observe(
|
2016-07-07 13:24:35 +00:00
|
|
|
time.Since(start).Seconds(),
|
2016-03-09 15:33:10 +00:00
|
|
|
)
|
2019-02-13 13:24:22 +00:00
|
|
|
return nil
|
2016-02-23 12:34:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 11:48:30 +00:00
|
|
|
// Sync converts target groups into actual scrape targets and synchronizes
|
2018-04-09 14:18:25 +00:00
|
|
|
// the currently running scraper with the resulting set and returns all scraped and dropped targets.
|
2018-09-26 09:20:56 +00:00
|
|
|
func (sp *scrapePool) Sync(tgs []*targetgroup.Group) {
|
2016-11-22 11:48:30 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var all []*Target
|
2018-01-04 14:13:31 +00:00
|
|
|
sp.mtx.Lock()
|
|
|
|
sp.droppedTargets = []*Target{}
|
2016-11-22 11:48:30 +00:00
|
|
|
for _, tg := range tgs {
|
|
|
|
targets, err := targetsFromGroup(tg, sp.config)
|
|
|
|
if err != nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Error(sp.logger).Log("msg", "creating targets failed", "err", err)
|
2016-11-22 11:48:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-12-04 15:12:28 +00:00
|
|
|
for _, t := range targets {
|
|
|
|
if t.Labels().Len() > 0 {
|
|
|
|
all = append(all, t)
|
|
|
|
} else if t.DiscoveredLabels().Len() > 0 {
|
|
|
|
sp.droppedTargets = append(sp.droppedTargets, t)
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 11:48:30 +00:00
|
|
|
}
|
2018-01-04 14:13:31 +00:00
|
|
|
sp.mtx.Unlock()
|
2016-11-22 11:48:30 +00:00
|
|
|
sp.sync(all)
|
|
|
|
|
|
|
|
targetSyncIntervalLength.WithLabelValues(sp.config.JobName).Observe(
|
|
|
|
time.Since(start).Seconds(),
|
|
|
|
)
|
|
|
|
targetScrapePoolSyncsCounter.WithLabelValues(sp.config.JobName).Inc()
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:37:25 +00: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 15:46:55 +00:00
|
|
|
sp.mtx.Lock()
|
2016-02-23 12:34:24 +00:00
|
|
|
defer sp.mtx.Unlock()
|
2016-02-22 15:46:55 +00:00
|
|
|
|
|
|
|
var (
|
2019-03-15 10:04:15 +00:00
|
|
|
uniqueTargets = map[uint64]struct{}{}
|
|
|
|
interval = time.Duration(sp.config.ScrapeInterval)
|
|
|
|
timeout = time.Duration(sp.config.ScrapeTimeout)
|
|
|
|
limit = int(sp.config.SampleLimit)
|
|
|
|
honorLabels = sp.config.HonorLabels
|
|
|
|
honorTimestamps = sp.config.HonorTimestamps
|
|
|
|
mrc = sp.config.MetricRelabelConfigs
|
2016-02-22 15:46:55 +00:00
|
|
|
)
|
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
for _, t := range targets {
|
2017-01-13 13:48:01 +00:00
|
|
|
t := t
|
2016-02-28 18:56:18 +00:00
|
|
|
hash := t.hash()
|
|
|
|
uniqueTargets[hash] = struct{}{}
|
2016-02-23 13:37:25 +00:00
|
|
|
|
2018-09-26 09:20:56 +00:00
|
|
|
if _, ok := sp.activeTargets[hash]; !ok {
|
2017-04-27 08:19:55 +00:00
|
|
|
s := &targetScraper{Target: t, client: sp.client, timeout: timeout}
|
2019-03-12 10:26:18 +00:00
|
|
|
l := sp.newLoop(scrapeLoopOptions{
|
2019-03-15 10:04:15 +00:00
|
|
|
target: t,
|
|
|
|
scraper: s,
|
|
|
|
limit: limit,
|
|
|
|
honorLabels: honorLabels,
|
|
|
|
honorTimestamps: honorTimestamps,
|
|
|
|
mrc: mrc,
|
2019-03-12 10:26:18 +00:00
|
|
|
})
|
2016-02-23 13:37:25 +00:00
|
|
|
|
2018-09-26 09:20:56 +00:00
|
|
|
sp.activeTargets[hash] = t
|
2016-02-28 18:56:18 +00:00
|
|
|
sp.loops[hash] = l
|
2016-02-23 13:37:25 +00:00
|
|
|
|
|
|
|
go l.run(interval, timeout, nil)
|
2018-02-07 10:29:27 +00:00
|
|
|
} else {
|
|
|
|
// Need to keep the most updated labels information
|
|
|
|
// for displaying it in the Service Discovery web page.
|
2018-09-26 09:20:56 +00:00
|
|
|
sp.activeTargets[hash].SetDiscoveredLabels(t.DiscoveredLabels())
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
var wg sync.WaitGroup
|
2016-02-22 15:46:55 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
// Stop and remove old targets and scraper loops.
|
2018-09-26 09:20:56 +00:00
|
|
|
for hash := range sp.activeTargets {
|
2016-02-28 18:56:18 +00:00
|
|
|
if _, ok := uniqueTargets[hash]; !ok {
|
2016-02-23 13:37:25 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(l loop) {
|
2018-09-26 09:20:56 +00:00
|
|
|
|
2016-02-23 13:37:25 +00:00
|
|
|
l.stop()
|
2018-09-26 09:20:56 +00:00
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
wg.Done()
|
2016-02-28 18:56:18 +00:00
|
|
|
}(sp.loops[hash])
|
2016-02-23 13:37:25 +00:00
|
|
|
|
2016-02-28 18:56:18 +00:00
|
|
|
delete(sp.loops, hash)
|
2018-09-26 09:20:56 +00:00
|
|
|
delete(sp.activeTargets, hash)
|
2016-02-22 15:46:55 +00: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()
|
|
|
|
}
|
|
|
|
|
2018-12-18 11:26:36 +00:00
|
|
|
func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*relabel.Config) labels.Labels {
|
2017-09-08 12:34:45 +00:00
|
|
|
lb := labels.NewBuilder(lset)
|
2017-01-30 16:30:28 +00:00
|
|
|
|
2018-04-12 14:54:53 +00:00
|
|
|
if honor {
|
2017-09-08 12:34:45 +00:00
|
|
|
for _, l := range target.Labels() {
|
2018-02-14 17:03:58 +00:00
|
|
|
if !lset.Has(l.Name) {
|
2017-09-08 12:34:45 +00:00
|
|
|
lb.Set(l.Name, l.Value)
|
|
|
|
}
|
2017-07-04 12:55:33 +00:00
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
} else {
|
|
|
|
for _, l := range target.Labels() {
|
2019-08-13 10:19:17 +00:00
|
|
|
// existingValue will be empty if l.Name doesn't exist.
|
|
|
|
existingValue := lset.Get(l.Name)
|
|
|
|
// Because setting a label with an empty value is a no-op,
|
|
|
|
// this will only create the prefixed label if necessary.
|
|
|
|
lb.Set(model.ExportedLabelPrefix+l.Name, existingValue)
|
|
|
|
// It is now safe to set the target label.
|
2017-09-08 12:34:45 +00:00
|
|
|
lb.Set(l.Name, l.Value)
|
2017-01-30 16:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
res := lb.Labels()
|
|
|
|
|
2018-04-12 14:54:53 +00:00
|
|
|
if len(rc) > 0 {
|
|
|
|
res = relabel.Process(res, rc...)
|
2016-02-28 08:51:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-04-12 14:54:53 +00:00
|
|
|
func mutateReportSampleLabels(lset labels.Labels, target *Target) labels.Labels {
|
2017-09-08 12:34:45 +00:00
|
|
|
lb := labels.NewBuilder(lset)
|
|
|
|
|
|
|
|
for _, l := range target.Labels() {
|
2019-08-13 10:19:17 +00:00
|
|
|
lb.Set(model.ExportedLabelPrefix+l.Name, lset.Get(l.Name))
|
2017-09-08 12:34:45 +00:00
|
|
|
lb.Set(l.Name, l.Value)
|
2016-02-28 08:51:02 +00:00
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
|
|
|
|
return lb.Labels()
|
2016-02-28 08:51:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
// appender returns an appender for ingested samples from the target.
|
2018-04-12 14:54:53 +00:00
|
|
|
func appender(app storage.Appender, limit int) storage.Appender {
|
2017-09-08 12:34:45 +00:00
|
|
|
app = &timeLimitAppender{
|
2016-12-30 20:35:35 +00:00
|
|
|
Appender: app,
|
2017-09-08 12:34:45 +00:00
|
|
|
maxTime: timestamp.FromTime(time.Now().Add(maxAheadTime)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// The limit is applied after metrics are potentially dropped via relabeling.
|
2018-04-12 14:54:53 +00:00
|
|
|
if limit > 0 {
|
2017-09-08 12:34:45 +00:00
|
|
|
app = &limitAppender{
|
|
|
|
Appender: app,
|
2018-04-12 14:54:53 +00:00
|
|
|
limit: limit,
|
2017-09-08 12:34:45 +00:00
|
|
|
}
|
2016-02-28 08:51:02 +00:00
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
return app
|
2016-02-28 08:51:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 12:58:46 +00:00
|
|
|
// A scraper retrieves samples and accepts a status report at the end.
|
|
|
|
type scraper interface {
|
2018-10-04 13:52:03 +00:00
|
|
|
scrape(ctx context.Context, w io.Writer) (string, error)
|
2016-02-25 12:58:46 +00:00
|
|
|
report(start time.Time, dur time.Duration, err error)
|
2019-03-12 10:46:15 +00:00
|
|
|
offset(interval time.Duration, jitterSeed uint64) time.Duration
|
2016-02-25 12:58:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 18:21:50 +00:00
|
|
|
// targetScraper implements the scraper interface for a target.
|
|
|
|
type targetScraper struct {
|
|
|
|
*Target
|
|
|
|
|
2017-04-04 17:26:28 +00:00
|
|
|
client *http.Client
|
2017-04-27 08:19:55 +00:00
|
|
|
req *http.Request
|
2017-04-04 17:26:28 +00:00
|
|
|
timeout time.Duration
|
2017-01-15 16:33:07 +00:00
|
|
|
|
2017-02-22 12:00:51 +00:00
|
|
|
gzipr *gzip.Reader
|
|
|
|
buf *bufio.Reader
|
2017-01-15 16:33:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:11:16 +00:00
|
|
|
const acceptHeader = `application/openmetrics-text; version=0.0.1,text/plain;version=0.0.4;q=0.5,*/*;q=0.1`
|
2017-01-15 16:33:07 +00:00
|
|
|
|
2017-02-28 13:59:33 +00:00
|
|
|
var userAgentHeader = fmt.Sprintf("Prometheus/%s", version.Version)
|
|
|
|
|
2018-10-04 13:52:03 +00:00
|
|
|
func (s *targetScraper) scrape(ctx context.Context, w io.Writer) (string, error) {
|
2017-02-22 12:00:51 +00:00
|
|
|
if s.req == nil {
|
|
|
|
req, err := http.NewRequest("GET", s.URL().String(), nil)
|
|
|
|
if err != nil {
|
2018-10-04 13:52:03 +00:00
|
|
|
return "", err
|
2017-02-22 12:00:51 +00:00
|
|
|
}
|
2017-09-22 16:06:43 +00:00
|
|
|
req.Header.Add("Accept", acceptHeader)
|
2017-02-22 12:00:51 +00:00
|
|
|
req.Header.Add("Accept-Encoding", "gzip")
|
2017-03-17 15:27:07 +00:00
|
|
|
req.Header.Set("User-Agent", userAgentHeader)
|
2017-04-27 08:19:55 +00:00
|
|
|
req.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", fmt.Sprintf("%f", s.timeout.Seconds()))
|
2017-02-22 12:00:51 +00:00
|
|
|
|
|
|
|
s.req = req
|
2016-02-28 18:21:50 +00:00
|
|
|
}
|
2017-08-09 14:30:49 +00:00
|
|
|
|
2018-11-19 11:31:16 +00:00
|
|
|
resp, err := s.client.Do(s.req.WithContext(ctx))
|
2016-02-28 18:21:50 +00:00
|
|
|
if err != nil {
|
2018-10-04 13:52:03 +00:00
|
|
|
return "", err
|
2016-02-28 18:21:50 +00:00
|
|
|
}
|
2019-04-18 08:50:37 +00:00
|
|
|
defer func() {
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
}()
|
2016-02-28 18:21:50 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2019-03-25 23:01:12 +00:00
|
|
|
return "", errors.Errorf("server returned HTTP status %s", resp.Status)
|
2016-02-28 18:21:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 12:00:51 +00:00
|
|
|
if resp.Header.Get("Content-Encoding") != "gzip" {
|
|
|
|
_, err = io.Copy(w, resp.Body)
|
2018-11-26 13:05:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.Header.Get("Content-Type"), nil
|
2017-02-22 12:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.gzipr == nil {
|
|
|
|
s.buf = bufio.NewReader(resp.Body)
|
|
|
|
s.gzipr, err = gzip.NewReader(s.buf)
|
|
|
|
if err != nil {
|
2018-10-04 13:52:03 +00:00
|
|
|
return "", err
|
2017-02-22 12:00:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.buf.Reset(resp.Body)
|
2018-08-17 15:24:35 +00:00
|
|
|
if err = s.gzipr.Reset(s.buf); err != nil {
|
2018-10-04 13:52:03 +00:00
|
|
|
return "", err
|
2018-08-17 15:24:35 +00:00
|
|
|
}
|
2017-02-22 12:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, s.gzipr)
|
|
|
|
s.gzipr.Close()
|
2018-10-04 13:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.Header.Get("Content-Type"), nil
|
2016-02-28 18:21:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:51:02 +00:00
|
|
|
// A loop can run and be stopped again. It must not be reused after it was stopped.
|
2016-02-22 15:46:55 +00:00
|
|
|
type loop interface {
|
2016-02-22 17:49:26 +00:00
|
|
|
run(interval, timeout time.Duration, errc chan<- error)
|
2016-02-22 15:46:55 +00:00
|
|
|
stop()
|
|
|
|
}
|
|
|
|
|
2017-09-15 09:08:51 +00:00
|
|
|
type cacheEntry struct {
|
2017-09-07 12:14:41 +00:00
|
|
|
ref uint64
|
2017-05-26 06:44:24 +00:00
|
|
|
lastIter uint64
|
2017-09-15 09:08:51 +00:00
|
|
|
hash uint64
|
|
|
|
lset labels.Labels
|
2017-04-13 17:07:23 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
type scrapeLoop struct {
|
2019-03-15 10:04:15 +00:00
|
|
|
scraper scraper
|
|
|
|
l log.Logger
|
|
|
|
cache *scrapeCache
|
|
|
|
lastScrapeSize int
|
|
|
|
buffers *pool.Pool
|
|
|
|
jitterSeed uint64
|
|
|
|
honorTimestamps bool
|
2017-05-26 06:44:24 +00:00
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
appender func() storage.Appender
|
|
|
|
sampleMutator labelsMutator
|
|
|
|
reportSampleMutator labelsMutator
|
2016-02-22 15:46:55 +00:00
|
|
|
|
2019-08-28 13:55:09 +00:00
|
|
|
parentCtx context.Context
|
2017-05-26 08:44:48 +00:00
|
|
|
ctx 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.
|
|
|
|
|
2019-03-28 17:52:46 +00:00
|
|
|
// How many series and metadata entries there were at the last success.
|
|
|
|
successfulCount int
|
|
|
|
|
2017-09-15 09:08:51 +00:00
|
|
|
// Parsed string to an entry with information about the actual label set
|
|
|
|
// and its storage reference.
|
2018-05-18 07:32:11 +00:00
|
|
|
series map[string]*cacheEntry
|
2017-05-24 14:23:48 +00:00
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
// Cache of dropped metric strings and their iteration. The iteration must
|
|
|
|
// be a pointer so we can update it without setting a new entry with an unsafe
|
|
|
|
// string in addDropped().
|
2018-05-18 07:32:11 +00:00
|
|
|
droppedSeries map[string]*uint64
|
2017-09-08 12:34:45 +00:00
|
|
|
|
2017-05-24 14:23:48 +00:00
|
|
|
// seriesCur and seriesPrev store the labels of series that were seen
|
|
|
|
// in the current and previous scrape.
|
2017-05-24 15:05:42 +00:00
|
|
|
// We hold two maps and swap them out to save allocations.
|
2017-05-26 06:44:24 +00:00
|
|
|
seriesCur map[uint64]labels.Labels
|
|
|
|
seriesPrev map[uint64]labels.Labels
|
2018-05-18 07:32:11 +00:00
|
|
|
|
|
|
|
metaMtx sync.Mutex
|
|
|
|
metadata map[string]*metaEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// metaEntry holds meta information about a metric.
|
|
|
|
type metaEntry struct {
|
2018-06-05 10:30:19 +00:00
|
|
|
lastIter uint64 // Last scrape iteration the entry was observed at.
|
2018-05-18 07:32:11 +00:00
|
|
|
typ textparse.MetricType
|
|
|
|
help string
|
2018-10-05 16:11:16 +00:00
|
|
|
unit string
|
2017-05-26 08:44:48 +00:00
|
|
|
}
|
2017-01-15 16:33:07 +00:00
|
|
|
|
2017-05-26 08:44:48 +00:00
|
|
|
func newScrapeCache() *scrapeCache {
|
|
|
|
return &scrapeCache{
|
2018-05-18 07:32:11 +00:00
|
|
|
series: map[string]*cacheEntry{},
|
|
|
|
droppedSeries: map[string]*uint64{},
|
|
|
|
seriesCur: map[uint64]labels.Labels{},
|
|
|
|
seriesPrev: map[uint64]labels.Labels{},
|
|
|
|
metadata: map[string]*metaEntry{},
|
2017-05-26 08:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:52:46 +00:00
|
|
|
func (c *scrapeCache) iterDone(flushCache bool) {
|
|
|
|
c.metaMtx.Lock()
|
|
|
|
count := len(c.series) + len(c.droppedSeries) + len(c.metadata)
|
|
|
|
c.metaMtx.Unlock()
|
|
|
|
|
|
|
|
if flushCache {
|
|
|
|
c.successfulCount = count
|
|
|
|
} else if count > c.successfulCount*2+1000 {
|
|
|
|
// If a target had varying labels in scrapes that ultimately failed,
|
|
|
|
// the caches would grow indefinitely. Force a flush when this happens.
|
|
|
|
// We use the heuristic that this is a doubling of the cache size
|
|
|
|
// since the last scrape, and allow an additional 1000 in case
|
|
|
|
// initial scrapes all fail.
|
|
|
|
flushCache = true
|
|
|
|
targetScrapeCacheFlushForced.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
if flushCache {
|
2019-03-28 17:07:14 +00:00
|
|
|
// All caches 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.series {
|
|
|
|
if c.iter != e.lastIter {
|
|
|
|
delete(c.series, s)
|
|
|
|
}
|
2017-05-26 08:44:48 +00:00
|
|
|
}
|
2019-03-28 17:07:14 +00:00
|
|
|
for s, iter := range c.droppedSeries {
|
|
|
|
if c.iter != *iter {
|
|
|
|
delete(c.droppedSeries, s)
|
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
}
|
2019-03-28 17:07:14 +00:00
|
|
|
c.metaMtx.Lock()
|
|
|
|
for m, e := range c.metadata {
|
|
|
|
// Keep metadata around for 10 scrapes after its metric disappeared.
|
|
|
|
if c.iter-e.lastIter > 10 {
|
|
|
|
delete(c.metadata, m)
|
|
|
|
}
|
2018-05-18 07:32:11 +00:00
|
|
|
}
|
2019-03-28 17:07:14 +00:00
|
|
|
c.metaMtx.Unlock()
|
|
|
|
|
|
|
|
c.iter++
|
2018-05-18 07:32:11 +00:00
|
|
|
}
|
2017-05-26 08:44:48 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 09:08:51 +00:00
|
|
|
func (c *scrapeCache) get(met string) (*cacheEntry, bool) {
|
2018-05-18 07:32:11 +00:00
|
|
|
e, ok := c.series[met]
|
2017-05-26 08:44:48 +00:00
|
|
|
if !ok {
|
2017-09-15 09:08:51 +00:00
|
|
|
return nil, false
|
2017-05-26 08:44:48 +00:00
|
|
|
}
|
|
|
|
e.lastIter = c.iter
|
2017-09-15 09:08:51 +00:00
|
|
|
return e, true
|
2017-05-26 08:44:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:14:41 +00:00
|
|
|
func (c *scrapeCache) addRef(met string, ref uint64, lset labels.Labels, hash uint64) {
|
|
|
|
if ref == 0 {
|
2017-06-26 06:56:40 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-18 07:32:11 +00:00
|
|
|
c.series[met] = &cacheEntry{ref: ref, lastIter: c.iter, lset: lset, hash: hash}
|
2017-05-26 08:44:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
func (c *scrapeCache) addDropped(met string) {
|
|
|
|
iter := c.iter
|
2018-05-18 07:32:11 +00:00
|
|
|
c.droppedSeries[met] = &iter
|
2017-09-08 12:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scrapeCache) getDropped(met string) bool {
|
2018-05-18 07:32:11 +00:00
|
|
|
iterp, ok := c.droppedSeries[met]
|
2017-09-08 12:34:45 +00:00
|
|
|
if ok {
|
|
|
|
*iterp = c.iter
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-06-26 06:56:40 +00:00
|
|
|
func (c *scrapeCache) trackStaleness(hash uint64, lset labels.Labels) {
|
|
|
|
c.seriesCur[hash] = lset
|
2017-05-26 08:44:48 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 07:32:11 +00:00
|
|
|
func (c *scrapeCache) setType(metric []byte, t textparse.MetricType) {
|
|
|
|
c.metaMtx.Lock()
|
|
|
|
|
|
|
|
e, ok := c.metadata[yoloString(metric)]
|
|
|
|
if !ok {
|
2018-10-05 16:11:16 +00:00
|
|
|
e = &metaEntry{typ: textparse.MetricTypeUnknown}
|
2018-05-18 07:32:11 +00:00
|
|
|
c.metadata[string(metric)] = e
|
|
|
|
}
|
|
|
|
e.typ = t
|
|
|
|
e.lastIter = c.iter
|
|
|
|
|
|
|
|
c.metaMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *scrapeCache) setHelp(metric, help []byte) {
|
|
|
|
c.metaMtx.Lock()
|
|
|
|
|
|
|
|
e, ok := c.metadata[yoloString(metric)]
|
|
|
|
if !ok {
|
2018-10-05 16:11:16 +00:00
|
|
|
e = &metaEntry{typ: textparse.MetricTypeUnknown}
|
2018-05-18 07:32:11 +00:00
|
|
|
c.metadata[string(metric)] = e
|
|
|
|
}
|
|
|
|
if e.help != yoloString(help) {
|
|
|
|
e.help = string(help)
|
|
|
|
}
|
|
|
|
e.lastIter = c.iter
|
|
|
|
|
|
|
|
c.metaMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:11:16 +00:00
|
|
|
func (c *scrapeCache) setUnit(metric, unit []byte) {
|
|
|
|
c.metaMtx.Lock()
|
|
|
|
|
|
|
|
e, ok := c.metadata[yoloString(metric)]
|
|
|
|
if !ok {
|
|
|
|
e = &metaEntry{typ: textparse.MetricTypeUnknown}
|
|
|
|
c.metadata[string(metric)] = e
|
|
|
|
}
|
|
|
|
if e.unit != yoloString(unit) {
|
|
|
|
e.unit = string(unit)
|
|
|
|
}
|
|
|
|
e.lastIter = c.iter
|
|
|
|
|
|
|
|
c.metaMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-05-18 07:32:11 +00:00
|
|
|
func (c *scrapeCache) getMetadata(metric string) (MetricMetadata, bool) {
|
|
|
|
c.metaMtx.Lock()
|
|
|
|
defer c.metaMtx.Unlock()
|
|
|
|
|
|
|
|
m, ok := c.metadata[metric]
|
|
|
|
if !ok {
|
|
|
|
return MetricMetadata{}, false
|
|
|
|
}
|
|
|
|
return MetricMetadata{
|
|
|
|
Metric: metric,
|
|
|
|
Type: m.typ,
|
|
|
|
Help: m.help,
|
2018-10-05 16:11:16 +00:00
|
|
|
Unit: m.unit,
|
2018-05-18 07:32:11 +00:00
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
2018-06-05 10:30:19 +00:00
|
|
|
func (c *scrapeCache) listMetadata() []MetricMetadata {
|
2018-05-18 07:32:11 +00:00
|
|
|
c.metaMtx.Lock()
|
|
|
|
defer c.metaMtx.Unlock()
|
|
|
|
|
2018-06-05 10:30:19 +00:00
|
|
|
res := make([]MetricMetadata, 0, len(c.metadata))
|
|
|
|
|
2018-05-18 07:32:11 +00:00
|
|
|
for m, e := range c.metadata {
|
|
|
|
res = append(res, MetricMetadata{
|
|
|
|
Metric: m,
|
|
|
|
Type: e.typ,
|
|
|
|
Help: e.help,
|
2018-10-05 16:11:16 +00:00
|
|
|
Unit: e.unit,
|
2018-05-18 07:32:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-11-26 15:15:15 +00:00
|
|
|
func newScrapeLoop(ctx context.Context,
|
2017-05-26 08:44:48 +00:00
|
|
|
sc scraper,
|
|
|
|
l log.Logger,
|
2018-02-13 20:44:51 +00:00
|
|
|
buffers *pool.Pool,
|
2017-09-08 12:34:45 +00:00
|
|
|
sampleMutator labelsMutator,
|
|
|
|
reportSampleMutator labelsMutator,
|
|
|
|
appender func() storage.Appender,
|
2018-05-18 07:32:11 +00:00
|
|
|
cache *scrapeCache,
|
2019-03-12 10:46:15 +00:00
|
|
|
jitterSeed uint64,
|
2019-03-15 10:04:15 +00:00
|
|
|
honorTimestamps bool,
|
2017-05-26 08:44:48 +00:00
|
|
|
) *scrapeLoop {
|
2017-05-16 13:04:37 +00:00
|
|
|
if l == nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
l = log.NewNopLogger()
|
2017-05-16 13:04:37 +00:00
|
|
|
}
|
2017-09-07 12:43:21 +00:00
|
|
|
if buffers == nil {
|
2018-02-13 20:44:51 +00:00
|
|
|
buffers = pool.New(1e3, 1e6, 3, func(sz int) interface{} { return make([]byte, 0, sz) })
|
2017-09-07 12:43:21 +00:00
|
|
|
}
|
2018-05-18 07:32:11 +00:00
|
|
|
if cache == nil {
|
|
|
|
cache = newScrapeCache()
|
|
|
|
}
|
2016-02-22 15:46:55 +00:00
|
|
|
sl := &scrapeLoop{
|
2017-09-08 12:34:45 +00:00
|
|
|
scraper: sc,
|
|
|
|
buffers: buffers,
|
2018-05-18 07:32:11 +00:00
|
|
|
cache: cache,
|
2017-09-08 12:34:45 +00:00
|
|
|
appender: appender,
|
|
|
|
sampleMutator: sampleMutator,
|
|
|
|
reportSampleMutator: reportSampleMutator,
|
|
|
|
stopped: make(chan struct{}),
|
2019-03-12 10:46:15 +00:00
|
|
|
jitterSeed: jitterSeed,
|
2017-09-08 12:34:45 +00:00
|
|
|
l: l,
|
2019-08-28 13:55:09 +00:00
|
|
|
parentCtx: ctx,
|
2019-03-15 10:04:15 +00:00
|
|
|
honorTimestamps: honorTimestamps,
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2019-08-28 13:55:09 +00:00
|
|
|
sl.ctx, sl.cancel = context.WithCancel(ctx)
|
2016-02-22 15:46:55 +00:00
|
|
|
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:49:26 +00:00
|
|
|
func (sl *scrapeLoop) run(interval, timeout time.Duration, errc chan<- error) {
|
2016-02-22 15:46:55 +00:00
|
|
|
select {
|
2019-03-12 10:46:15 +00:00
|
|
|
case <-time.After(sl.scraper.offset(interval, sl.jitterSeed)):
|
2016-02-22 15:46:55 +00:00
|
|
|
// Continue after a scraping offset.
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.ctx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
close(sl.stopped)
|
2016-02-22 15:46:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var last time.Time
|
|
|
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2017-05-10 15:59:02 +00:00
|
|
|
mainLoop:
|
2016-02-22 15:46:55 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.parentCtx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
close(sl.stopped)
|
2016-02-22 15:46:55 +00:00
|
|
|
return
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.ctx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
break mainLoop
|
2016-02-22 15:46:55 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-12-29 08:27:30 +00:00
|
|
|
var (
|
2017-05-11 15:20:03 +00:00
|
|
|
start = time.Now()
|
|
|
|
scrapeCtx, cancel = context.WithTimeout(sl.ctx, timeout)
|
2016-12-29 08:27:30 +00:00
|
|
|
)
|
2016-02-22 17:49:26 +00:00
|
|
|
|
2016-12-29 08:27:30 +00:00
|
|
|
// Only record after the first scrape.
|
|
|
|
if !last.IsZero() {
|
|
|
|
targetIntervalLength.WithLabelValues(interval.String()).Observe(
|
|
|
|
time.Since(last).Seconds(),
|
|
|
|
)
|
|
|
|
}
|
2018-02-20 11:32:23 +00:00
|
|
|
|
|
|
|
b := sl.buffers.Get(sl.lastScrapeSize).([]byte)
|
2017-09-07 12:43:21 +00:00
|
|
|
buf := bytes.NewBuffer(b)
|
2016-02-22 15:46:55 +00:00
|
|
|
|
2018-10-04 13:52:03 +00:00
|
|
|
contentType, scrapeErr := sl.scraper.scrape(scrapeCtx, buf)
|
2017-05-11 15:20:03 +00:00
|
|
|
cancel()
|
2017-09-07 12:43:21 +00:00
|
|
|
|
2017-06-15 02:08:03 +00:00
|
|
|
if scrapeErr == nil {
|
2017-05-03 13:55:35 +00:00
|
|
|
b = buf.Bytes()
|
2017-09-07 12:43:21 +00:00
|
|
|
// NOTE: There were issues with misbehaving clients in the past
|
|
|
|
// that occasionally returned empty results. We don't want those
|
|
|
|
// to falsely reset our buffer size.
|
|
|
|
if len(b) > 0 {
|
|
|
|
sl.lastScrapeSize = len(b)
|
|
|
|
}
|
2017-09-05 10:55:14 +00:00
|
|
|
} else {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Scrape failed", "err", scrapeErr.Error())
|
2017-09-05 10:55:14 +00:00
|
|
|
if errc != nil {
|
|
|
|
errc <- scrapeErr
|
|
|
|
}
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2017-06-15 02:08:03 +00:00
|
|
|
|
2017-05-03 13:55:35 +00:00
|
|
|
// A failed scrape is the same as an empty scrape,
|
|
|
|
// we still call sl.append to trigger stale markers.
|
2019-05-08 21:24:00 +00:00
|
|
|
total, added, seriesAdded, appErr := sl.append(b, contentType, start)
|
2017-06-15 02:08:03 +00:00
|
|
|
if appErr != nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(sl.l).Log("msg", "append failed", "err", appErr)
|
2017-05-29 13:08:55 +00:00
|
|
|
// The append failed, probably due to a parse error or sample limit.
|
2017-05-03 15:51:45 +00:00
|
|
|
// Call sl.append again with an empty scrape to trigger stale markers.
|
2019-05-08 21:24:00 +00:00
|
|
|
if _, _, _, err := sl.append([]byte{}, "", start); err != nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(sl.l).Log("msg", "append failed", "err", err)
|
2017-05-03 15:51:45 +00:00
|
|
|
}
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:43:21 +00:00
|
|
|
sl.buffers.Put(b)
|
|
|
|
|
2017-06-15 02:08:03 +00:00
|
|
|
if scrapeErr == nil {
|
|
|
|
scrapeErr = appErr
|
|
|
|
}
|
|
|
|
|
2019-05-08 21:24:00 +00:00
|
|
|
if err := sl.report(start, time.Since(start), total, added, seriesAdded, scrapeErr); err != nil {
|
2018-08-17 15:24:35 +00:00
|
|
|
level.Warn(sl.l).Log("msg", "appending scrape report failed", "err", err)
|
|
|
|
}
|
2016-12-29 08:27:30 +00:00
|
|
|
last = start
|
|
|
|
|
2016-02-22 15:46:55 +00:00
|
|
|
select {
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.parentCtx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
close(sl.stopped)
|
2016-02-22 15:46:55 +00:00
|
|
|
return
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.ctx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
break mainLoop
|
2016-02-22 15:46:55 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 15:59:02 +00:00
|
|
|
|
|
|
|
close(sl.stopped)
|
|
|
|
|
2017-05-16 12:12:21 +00:00
|
|
|
sl.endOfRunStaleness(last, ticker, interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sl *scrapeLoop) endOfRunStaleness(last time.Time, ticker *time.Ticker, interval time.Duration) {
|
2017-05-10 15:59:02 +00: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.
|
2018-11-27 16:44:29 +00:00
|
|
|
// If the context is canceled, we presume the server is shutting down
|
2017-05-10 15:59:02 +00:00
|
|
|
// 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 {
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.parentCtx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
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 {
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.parentCtx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for an extra 10% of the interval, just to be safe.
|
|
|
|
select {
|
2019-08-28 13:55:09 +00:00
|
|
|
case <-sl.parentCtx.Done():
|
2017-05-10 15:59:02 +00:00
|
|
|
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.
|
2019-05-08 21:24:00 +00:00
|
|
|
if _, _, _, err := sl.append([]byte{}, "", staleTime); err != nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Error(sl.l).Log("msg", "stale append failed", "err", err)
|
2017-05-10 15:59:02 +00:00
|
|
|
}
|
2017-05-11 13:43:43 +00:00
|
|
|
if err := sl.reportStale(staleTime); err != nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Error(sl.l).Log("msg", "stale report failed", "err", err)
|
2017-05-11 13:43:43 +00:00
|
|
|
}
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 15:59:02 +00: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 15:46:55 +00:00
|
|
|
func (sl *scrapeLoop) stop() {
|
|
|
|
sl.cancel()
|
2017-05-10 15:59:02 +00:00
|
|
|
<-sl.stopped
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 21:24:00 +00:00
|
|
|
func (sl *scrapeLoop) append(b []byte, contentType string, ts time.Time) (total, added, seriesAdded int, err error) {
|
2016-04-25 14:43:52 +00:00
|
|
|
var (
|
2017-07-04 09:24:13 +00:00
|
|
|
app = sl.appender()
|
2018-10-04 13:52:03 +00:00
|
|
|
p = textparse.New(b, contentType)
|
2017-07-04 09:24:13 +00:00
|
|
|
defTime = timestamp.FromTime(ts)
|
|
|
|
numOutOfOrder = 0
|
|
|
|
numDuplicates = 0
|
|
|
|
numOutOfBounds = 0
|
2016-04-25 14:43:52 +00:00
|
|
|
)
|
2017-05-29 13:08:55 +00:00
|
|
|
var sampleLimitErr error
|
2016-02-22 15:46:55 +00:00
|
|
|
|
2017-02-01 14:59:37 +00:00
|
|
|
loop:
|
2018-05-14 20:19:53 +00:00
|
|
|
for {
|
|
|
|
var et textparse.Entry
|
|
|
|
if et, err = p.Next(); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-05-18 07:32:11 +00:00
|
|
|
switch et {
|
|
|
|
case textparse.EntryType:
|
|
|
|
sl.cache.setType(p.Type())
|
|
|
|
continue
|
|
|
|
case textparse.EntryHelp:
|
|
|
|
sl.cache.setHelp(p.Help())
|
2018-05-14 20:19:53 +00:00
|
|
|
continue
|
2018-10-05 16:11:16 +00:00
|
|
|
case textparse.EntryUnit:
|
|
|
|
sl.cache.setUnit(p.Unit())
|
|
|
|
continue
|
2018-05-18 07:32:11 +00:00
|
|
|
case textparse.EntryComment:
|
|
|
|
continue
|
|
|
|
default:
|
2018-05-14 20:19:53 +00:00
|
|
|
}
|
2017-01-30 16:30:28 +00:00
|
|
|
total++
|
|
|
|
|
2017-01-15 16:33:07 +00:00
|
|
|
t := defTime
|
2018-05-14 20:19:53 +00:00
|
|
|
met, tp, v := p.Series()
|
2019-03-15 10:04:15 +00:00
|
|
|
if !sl.honorTimestamps {
|
|
|
|
tp = nil
|
|
|
|
}
|
2017-01-15 16:33:07 +00:00
|
|
|
if tp != nil {
|
|
|
|
t = *tp
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:34:45 +00:00
|
|
|
if sl.cache.getDropped(yoloString(met)) {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-15 09:08:51 +00:00
|
|
|
ce, ok := sl.cache.get(yoloString(met))
|
2017-01-15 16:33:07 +00:00
|
|
|
if ok {
|
2017-09-15 09:08:51 +00:00
|
|
|
switch err = app.AddFast(ce.lset, ce.ref, t, v); err {
|
2017-02-01 14:59:37 +00:00
|
|
|
case nil:
|
2017-05-24 14:23:48 +00:00
|
|
|
if tp == nil {
|
2017-09-15 09:08:51 +00:00
|
|
|
sl.cache.trackStaleness(ce.hash, ce.lset)
|
2017-05-24 14:23:48 +00:00
|
|
|
}
|
2017-02-01 14:59:37 +00:00
|
|
|
case storage.ErrNotFound:
|
|
|
|
ok = false
|
2017-05-10 13:44:13 +00:00
|
|
|
case storage.ErrOutOfOrderSample:
|
2017-05-26 06:44:24 +00:00
|
|
|
numOutOfOrder++
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Out of order sample", "series", string(met))
|
2017-08-02 12:10:18 +00:00
|
|
|
targetScrapeSampleOutOfOrder.Inc()
|
2017-05-10 13:44:13 +00:00
|
|
|
continue
|
|
|
|
case storage.ErrDuplicateSampleForTimestamp:
|
2017-05-26 06:44:24 +00:00
|
|
|
numDuplicates++
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Duplicate sample for timestamp", "series", string(met))
|
2017-08-02 12:10:18 +00:00
|
|
|
targetScrapeSampleDuplicate.Inc()
|
2017-01-17 07:39:18 +00:00
|
|
|
continue
|
2017-07-04 09:24:13 +00:00
|
|
|
case storage.ErrOutOfBounds:
|
|
|
|
numOutOfBounds++
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Out of bounds metric", "series", string(met))
|
2017-08-02 12:10:18 +00:00
|
|
|
targetScrapeSampleOutOfBounds.Inc()
|
2017-07-04 09:24:13 +00:00
|
|
|
continue
|
2017-05-29 13:08:55 +00: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 14:59:37 +00:00
|
|
|
default:
|
|
|
|
break loop
|
2017-01-15 16:33:07 +00:00
|
|
|
}
|
2017-01-13 13:48:01 +00:00
|
|
|
}
|
2017-01-15 16:33:07 +00:00
|
|
|
if !ok {
|
2017-09-15 09:08:51 +00:00
|
|
|
var lset labels.Labels
|
|
|
|
|
|
|
|
mets := p.Metric(&lset)
|
|
|
|
hash := lset.Hash()
|
|
|
|
|
|
|
|
// Hash label set as it is seen local to the target. Then add target labels
|
|
|
|
// and relabeling and store the final label set.
|
|
|
|
lset = sl.sampleMutator(lset)
|
|
|
|
|
|
|
|
// The label set may be set to nil to indicate dropping.
|
|
|
|
if lset == nil {
|
|
|
|
sl.cache.addDropped(mets)
|
|
|
|
continue
|
2017-06-26 06:56:40 +00:00
|
|
|
}
|
2017-01-30 16:30:28 +00:00
|
|
|
|
2017-09-07 12:14:41 +00:00
|
|
|
var ref uint64
|
2017-02-01 14:59:37 +00:00
|
|
|
ref, err = app.Add(lset, t, v)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-05-10 13:44:13 +00:00
|
|
|
case storage.ErrOutOfOrderSample:
|
|
|
|
err = nil
|
2017-05-26 06:44:24 +00:00
|
|
|
numOutOfOrder++
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Out of order sample", "series", string(met))
|
2017-08-02 12:10:18 +00:00
|
|
|
targetScrapeSampleOutOfOrder.Inc()
|
2017-05-10 13:44:13 +00:00
|
|
|
continue
|
|
|
|
case storage.ErrDuplicateSampleForTimestamp:
|
|
|
|
err = nil
|
2017-05-26 06:44:24 +00:00
|
|
|
numDuplicates++
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Duplicate sample for timestamp", "series", string(met))
|
2017-08-02 12:10:18 +00:00
|
|
|
targetScrapeSampleDuplicate.Inc()
|
2017-01-30 16:30:28 +00:00
|
|
|
continue
|
2017-07-04 09:24:13 +00:00
|
|
|
case storage.ErrOutOfBounds:
|
2017-07-04 12:55:33 +00:00
|
|
|
err = nil
|
2017-07-04 09:24:13 +00:00
|
|
|
numOutOfBounds++
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "Out of bounds metric", "series", string(met))
|
2017-08-02 12:10:18 +00:00
|
|
|
targetScrapeSampleOutOfBounds.Inc()
|
2017-07-04 09:24:13 +00:00
|
|
|
continue
|
2017-05-29 13:08:55 +00:00
|
|
|
case errSampleLimit:
|
|
|
|
sampleLimitErr = err
|
|
|
|
added++
|
|
|
|
continue
|
2017-02-01 14:59:37 +00:00
|
|
|
default:
|
2017-09-18 10:32:17 +00:00
|
|
|
level.Debug(sl.l).Log("msg", "unexpected error", "series", string(met), "err", err)
|
2017-02-01 14:59:37 +00:00
|
|
|
break loop
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2017-04-28 15:36:36 +00:00
|
|
|
if tp == nil {
|
|
|
|
// Bypass staleness logic if there is an explicit timestamp.
|
2017-06-26 06:56:40 +00:00
|
|
|
sl.cache.trackStaleness(hash, lset)
|
2017-04-28 15:36:36 +00:00
|
|
|
}
|
2017-06-26 06:56:40 +00:00
|
|
|
sl.cache.addRef(mets, ref, lset, hash)
|
2019-05-08 21:24:00 +00:00
|
|
|
seriesAdded++
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2017-02-01 14:59:37 +00:00
|
|
|
added++
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
2018-01-09 15:43:28 +00:00
|
|
|
if sampleLimitErr != nil {
|
2018-05-14 20:19:53 +00:00
|
|
|
if err == nil {
|
|
|
|
err = sampleLimitErr
|
|
|
|
}
|
2018-01-09 15:43:28 +00:00
|
|
|
// We only want to increment this once per scrape, so this is Inc'd outside the loop.
|
2017-05-29 13:08:55 +00:00
|
|
|
targetScrapeSampleLimit.Inc()
|
|
|
|
}
|
2017-05-16 12:30:40 +00:00
|
|
|
if numOutOfOrder > 0 {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(sl.l).Log("msg", "Error on ingesting out-of-order samples", "num_dropped", numOutOfOrder)
|
2017-05-16 12:30:40 +00:00
|
|
|
}
|
|
|
|
if numDuplicates > 0 {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(sl.l).Log("msg", "Error on ingesting samples with different value but same timestamp", "num_dropped", numDuplicates)
|
2017-05-16 12:30:40 +00:00
|
|
|
}
|
2017-07-04 09:24:13 +00:00
|
|
|
if numOutOfBounds > 0 {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(sl.l).Log("msg", "Error on ingesting samples that are too old or are too far into the future", "num_dropped", numOutOfBounds)
|
2017-07-04 09:24:13 +00:00
|
|
|
}
|
2017-04-13 17:07:23 +00:00
|
|
|
if err == nil {
|
2017-05-26 08:44:48 +00: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 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 17:07:23 +00:00
|
|
|
}
|
2017-05-26 08:44:48 +00:00
|
|
|
return err == nil
|
|
|
|
})
|
2017-04-13 17:07:23 +00:00
|
|
|
}
|
2017-01-15 16:33:07 +00:00
|
|
|
if err != nil {
|
|
|
|
app.Rollback()
|
2019-05-08 21:24:00 +00:00
|
|
|
return total, added, seriesAdded, err
|
2016-04-25 14:43:52 +00:00
|
|
|
}
|
2017-01-13 13:48:01 +00:00
|
|
|
if err := app.Commit(); err != nil {
|
2019-05-08 21:24:00 +00:00
|
|
|
return total, added, seriesAdded, err
|
2016-12-29 08:27:30 +00:00
|
|
|
}
|
2017-05-24 14:23:48 +00:00
|
|
|
|
2019-03-28 17:52:46 +00:00
|
|
|
// Only perform cache cleaning if the scrape was not empty.
|
|
|
|
// An empty scrape (usually) is used to indicate a failed scrape.
|
2019-03-28 17:07:14 +00:00
|
|
|
sl.cache.iterDone(len(b) > 0)
|
2017-05-24 14:23:48 +00:00
|
|
|
|
2019-05-08 21:24:00 +00:00
|
|
|
return total, added, seriesAdded, nil
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 12:00:51 +00:00
|
|
|
func yoloString(b []byte) string {
|
2017-03-07 10:41:11 +00:00
|
|
|
return *((*string)(unsafe.Pointer(&b)))
|
2017-02-22 12:00:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 09:08:51 +00:00
|
|
|
// The constants are suffixed with the invalid \xff unicode rune to avoid collisions
|
|
|
|
// with scraped metrics in the cache.
|
|
|
|
const (
|
|
|
|
scrapeHealthMetricName = "up" + "\xff"
|
|
|
|
scrapeDurationMetricName = "scrape_duration_seconds" + "\xff"
|
|
|
|
scrapeSamplesMetricName = "scrape_samples_scraped" + "\xff"
|
|
|
|
samplesPostRelabelMetricName = "scrape_samples_post_metric_relabeling" + "\xff"
|
2019-05-08 21:24:00 +00:00
|
|
|
scrapeSeriesAddedMetricName = "scrape_series_added" + "\xff"
|
2017-09-15 09:08:51 +00:00
|
|
|
)
|
|
|
|
|
2019-05-08 21:24:00 +00:00
|
|
|
func (sl *scrapeLoop) report(start time.Time, duration time.Duration, scraped, appended, seriesAdded int, err error) error {
|
2016-02-22 15:46:55 +00:00
|
|
|
sl.scraper.report(start, duration, err)
|
|
|
|
|
2017-01-15 16:33:07 +00:00
|
|
|
ts := timestamp.FromTime(start)
|
2016-02-22 15:46:55 +00:00
|
|
|
|
2016-12-29 08:27:30 +00:00
|
|
|
var health float64
|
2016-02-22 15:46:55 +00:00
|
|
|
if err == nil {
|
|
|
|
health = 1
|
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
app := sl.appender()
|
2017-01-13 13:48:01 +00:00
|
|
|
|
2017-01-15 16:33:07 +00:00
|
|
|
if err := sl.addReportSample(app, scrapeHealthMetricName, ts, health); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
2017-01-13 13:48:01 +00:00
|
|
|
}
|
2017-01-15 16:33:07 +00:00
|
|
|
if err := sl.addReportSample(app, scrapeDurationMetricName, ts, duration.Seconds()); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
2016-05-19 14:22:49 +00:00
|
|
|
}
|
2017-01-30 16:30:28 +00:00
|
|
|
if err := sl.addReportSample(app, scrapeSamplesMetricName, ts, float64(scraped)); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
2016-10-26 16:43:01 +00:00
|
|
|
}
|
2017-01-30 16:30:28 +00:00
|
|
|
if err := sl.addReportSample(app, samplesPostRelabelMetricName, ts, float64(appended)); err != nil {
|
2017-01-15 16:33:07 +00:00
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2019-05-08 21:24:00 +00:00
|
|
|
if err := sl.addReportSample(app, scrapeSeriesAddedMetricName, ts, float64(seriesAdded)); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2017-01-15 16:33:07 +00:00
|
|
|
return app.Commit()
|
|
|
|
}
|
|
|
|
|
2017-05-11 13:43:43 +00:00
|
|
|
func (sl *scrapeLoop) reportStale(start time.Time) error {
|
|
|
|
ts := timestamp.FromTime(start)
|
2017-09-08 12:34:45 +00:00
|
|
|
app := sl.appender()
|
|
|
|
|
2017-05-11 13:43:43 +00:00
|
|
|
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
|
|
|
|
}
|
2019-05-08 21:24:00 +00:00
|
|
|
if err := sl.addReportSample(app, scrapeSeriesAddedMetricName, ts, stale); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2017-05-11 13:43:43 +00:00
|
|
|
return app.Commit()
|
|
|
|
}
|
|
|
|
|
2017-01-15 16:33:07 +00:00
|
|
|
func (sl *scrapeLoop) addReportSample(app storage.Appender, s string, t int64, v float64) error {
|
2017-09-15 09:08:51 +00:00
|
|
|
ce, ok := sl.cache.get(s)
|
2017-01-15 16:33:07 +00:00
|
|
|
if ok {
|
2017-09-15 09:08:51 +00:00
|
|
|
err := app.AddFast(ce.lset, ce.ref, t, v)
|
2017-05-11 13:43:43 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-02-01 14:59:37 +00:00
|
|
|
return nil
|
2017-05-11 13:43:43 +00: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 14:59:37 +00:00
|
|
|
return nil
|
2017-05-11 13:43:43 +00:00
|
|
|
default:
|
2017-01-15 16:33:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-01-13 13:48:01 +00:00
|
|
|
}
|
2017-07-18 10:42:29 +00:00
|
|
|
lset := labels.Labels{
|
2017-09-15 09:08:51 +00:00
|
|
|
// The constants are suffixed with the invalid \xff unicode rune to avoid collisions
|
|
|
|
// with scraped metrics in the cache.
|
|
|
|
// We have to drop it when building the actual metric.
|
|
|
|
labels.Label{Name: labels.MetricName, Value: s[:len(s)-1]},
|
2016-05-19 14:22:49 +00:00
|
|
|
}
|
2017-09-08 12:34:45 +00:00
|
|
|
|
|
|
|
hash := lset.Hash()
|
|
|
|
lset = sl.reportSampleMutator(lset)
|
|
|
|
|
2017-07-18 10:42:29 +00:00
|
|
|
ref, err := app.Add(lset, t, v)
|
2017-05-11 13:43:43 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2017-09-15 09:08:51 +00:00
|
|
|
sl.cache.addRef(s, ref, lset, hash)
|
2017-05-11 13:43:43 +00:00
|
|
|
return nil
|
|
|
|
case storage.ErrOutOfOrderSample, storage.ErrDuplicateSampleForTimestamp:
|
|
|
|
return nil
|
|
|
|
default:
|
2017-01-15 16:33:07 +00:00
|
|
|
return err
|
2017-01-13 13:48:01 +00:00
|
|
|
}
|
2016-02-22 15:46:55 +00:00
|
|
|
}
|