2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-04 03:17:31 -08:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2013-07-14 14:03:56 -07:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
2015-03-14 19:36:15 -07:00
|
|
|
"errors"
|
2013-01-04 05:41:47 -08:00
|
|
|
"fmt"
|
2015-08-20 10:02:29 -07:00
|
|
|
"io"
|
2015-07-22 08:48:22 -07:00
|
|
|
"io/ioutil"
|
2013-01-04 05:41:47 -08:00
|
|
|
"net/http"
|
2015-02-19 09:58:47 -08:00
|
|
|
"net/url"
|
2013-04-10 05:26:07 -07:00
|
|
|
"strings"
|
2014-12-03 09:07:23 -08:00
|
|
|
"sync"
|
2012-12-25 04:50:36 -08:00
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
2014-06-18 10:43:15 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 10:02:29 -07:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-02-16 02:44:42 -08:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/net/context/ctxhttp"
|
2013-07-30 08:18:07 -07:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-03-14 19:36:15 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2016-02-02 05:01:44 -08:00
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2015-05-29 04:30:30 -07:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2012-12-25 04:50:36 -08:00
|
|
|
)
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
const (
|
2015-08-21 14:01:08 -07:00
|
|
|
scrapeHealthMetricName = "up"
|
|
|
|
scrapeDurationMetricName = "scrape_duration_seconds"
|
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
// Capacity of the channel to buffer samples during ingestion.
|
|
|
|
ingestedSamplesCap = 256
|
2014-06-18 10:43:15 -07:00
|
|
|
|
|
|
|
// Constants for instrumentation.
|
2014-07-23 10:55:33 -07:00
|
|
|
namespace = "prometheus"
|
2014-07-29 11:31:11 -07:00
|
|
|
interval = "interval"
|
2014-06-18 10:43:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-01-27 10:07:46 -08:00
|
|
|
errSkippedScrape = errors.New("scrape skipped due to throttled ingestion")
|
2015-03-14 19:36:15 -07:00
|
|
|
|
2014-07-29 11:31:11 -07:00
|
|
|
targetIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "target_interval_length_seconds",
|
|
|
|
Help: "Actual intervals between scrapes.",
|
2015-01-21 06:42:25 -08:00
|
|
|
Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
|
2014-07-29 11:31:11 -07:00
|
|
|
},
|
|
|
|
[]string{interval},
|
|
|
|
)
|
2016-01-27 10:07:46 -08:00
|
|
|
targetSkippedScrapes = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "target_skipped_scrapes_total",
|
|
|
|
Help: "Total number of scrapes that were skipped because the metric storage was throttled.",
|
|
|
|
},
|
|
|
|
[]string{interval},
|
|
|
|
)
|
2013-04-10 05:26:07 -07:00
|
|
|
)
|
|
|
|
|
2014-06-18 10:43:15 -07:00
|
|
|
func init() {
|
2014-07-29 11:31:11 -07:00
|
|
|
prometheus.MustRegister(targetIntervalLength)
|
2016-01-27 10:07:46 -08:00
|
|
|
prometheus.MustRegister(targetSkippedScrapes)
|
2014-06-18 10:43:15 -07:00
|
|
|
}
|
2013-06-25 05:02:27 -07:00
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// TargetHealth describes the health state of a target.
|
|
|
|
type TargetHealth int
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t TargetHealth) String() string {
|
2013-03-21 03:52:42 -07:00
|
|
|
switch t {
|
2015-05-18 04:14:41 -07:00
|
|
|
case HealthUnknown:
|
2015-06-23 08:46:57 -07:00
|
|
|
return "unknown"
|
2015-05-18 04:14:41 -07:00
|
|
|
case HealthGood:
|
2016-01-12 04:09:20 -08:00
|
|
|
return "up"
|
2015-05-18 04:14:41 -07:00
|
|
|
case HealthBad:
|
2016-01-12 04:09:20 -08:00
|
|
|
return "down"
|
2013-03-21 03:52:42 -07:00
|
|
|
}
|
|
|
|
panic("unknown state")
|
|
|
|
}
|
|
|
|
|
2015-08-21 14:01:08 -07:00
|
|
|
func (t TargetHealth) value() model.SampleValue {
|
|
|
|
if t == HealthGood {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
const (
|
2015-08-24 06:07:27 -07:00
|
|
|
// HealthUnknown is the state of a Target before it is first scraped.
|
2015-05-18 04:14:41 -07:00
|
|
|
HealthUnknown TargetHealth = iota
|
2015-08-24 06:07:27 -07:00
|
|
|
// HealthGood is the state of a Target that has been successfully scraped.
|
2015-05-18 04:14:41 -07:00
|
|
|
HealthGood
|
2015-08-24 06:07:27 -07:00
|
|
|
// HealthBad is the state of a Target that was scraped unsuccessfully.
|
2015-05-18 04:14:41 -07:00
|
|
|
HealthBad
|
2012-12-25 04:50:36 -08:00
|
|
|
)
|
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
// TargetStatus contains information about the current status of a scrape target.
|
|
|
|
type TargetStatus struct {
|
|
|
|
lastError error
|
|
|
|
lastScrape time.Time
|
2015-05-18 04:14:41 -07:00
|
|
|
health TargetHealth
|
2015-05-18 02:13:13 -07:00
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastError returns the error encountered during the last scrape.
|
|
|
|
func (ts *TargetStatus) LastError() error {
|
|
|
|
ts.mu.RLock()
|
|
|
|
defer ts.mu.RUnlock()
|
2016-02-12 06:43:27 -08:00
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
return ts.lastError
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastScrape returns the time of the last scrape.
|
|
|
|
func (ts *TargetStatus) LastScrape() time.Time {
|
|
|
|
ts.mu.RLock()
|
|
|
|
defer ts.mu.RUnlock()
|
2016-02-12 06:43:27 -08:00
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
return ts.lastScrape
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// Health returns the last known health state of the target.
|
|
|
|
func (ts *TargetStatus) Health() TargetHealth {
|
2015-05-18 02:13:13 -07:00
|
|
|
ts.mu.RLock()
|
|
|
|
defer ts.mu.RUnlock()
|
2016-02-12 06:43:27 -08:00
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
return ts.health
|
2015-05-18 02:13:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TargetStatus) setLastScrape(t time.Time) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
2016-02-12 06:43:27 -08:00
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
ts.lastScrape = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TargetStatus) setLastError(err error) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
2016-02-12 06:43:27 -08:00
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
if err == nil {
|
2015-05-18 04:14:41 -07:00
|
|
|
ts.health = HealthGood
|
2015-05-18 02:13:13 -07:00
|
|
|
} else {
|
2015-05-18 04:14:41 -07:00
|
|
|
ts.health = HealthBad
|
2015-05-18 02:13:13 -07:00
|
|
|
}
|
|
|
|
ts.lastError = err
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// Target refers to a singular HTTP or HTTPS endpoint.
|
|
|
|
type Target struct {
|
|
|
|
// The status object for the target. It is only set once on initialization.
|
|
|
|
status *TargetStatus
|
2014-12-03 09:07:23 -08:00
|
|
|
// Closing scraperStopping signals that scraping should stop.
|
|
|
|
scraperStopping chan struct{}
|
|
|
|
// Closing scraperStopped signals that scraping has been stopped.
|
|
|
|
scraperStopped chan struct{}
|
2013-01-04 03:17:31 -08:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// Mutex protects the members below.
|
2015-04-18 23:39:33 -07:00
|
|
|
sync.RWMutex
|
2016-02-12 06:43:27 -08:00
|
|
|
|
|
|
|
scrapeConfig *config.ScrapeConfig
|
|
|
|
|
2015-06-05 13:42:39 -07:00
|
|
|
// Labels before any processing.
|
2015-08-20 08:18:46 -07:00
|
|
|
metaLabels model.LabelSet
|
2016-02-15 01:31:38 -08:00
|
|
|
// Any labels that are added to this target and its metrics.
|
2016-02-12 06:43:27 -08:00
|
|
|
labels model.LabelSet
|
|
|
|
|
|
|
|
// The HTTP client used to scrape the target's endpoint.
|
|
|
|
httpClient *http.Client
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewTarget creates a reasonably configured target for querying.
|
2016-02-12 06:43:27 -08:00
|
|
|
func NewTarget(cfg *config.ScrapeConfig, labels, metaLabels model.LabelSet) (*Target, error) {
|
2015-05-18 04:14:41 -07:00
|
|
|
t := &Target{
|
2015-05-18 02:13:13 -07:00
|
|
|
status: &TargetStatus{},
|
2014-12-03 09:07:23 -08:00
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2016-02-12 06:43:27 -08:00
|
|
|
err := t.Update(cfg, labels, metaLabels)
|
2016-02-08 12:26:00 -08:00
|
|
|
return t, err
|
2015-03-14 19:36:15 -07:00
|
|
|
}
|
2013-04-16 08:22:10 -07:00
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// Status returns the status of the target.
|
|
|
|
func (t *Target) Status() *TargetStatus {
|
2015-05-18 02:13:13 -07:00
|
|
|
return t.status
|
|
|
|
}
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
// Update overwrites settings in the target that are derived from the job config
|
|
|
|
// it belongs to.
|
2016-02-15 01:31:38 -08:00
|
|
|
func (t *Target) Update(cfg *config.ScrapeConfig, labels, metaLabels model.LabelSet) error {
|
2015-04-20 03:24:25 -07:00
|
|
|
t.Lock()
|
2015-07-22 08:48:22 -07:00
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
t.scrapeConfig = cfg
|
2016-02-15 01:31:38 -08:00
|
|
|
t.labels = labels
|
2016-02-12 06:43:27 -08:00
|
|
|
t.metaLabels = metaLabels
|
2015-09-25 13:51:49 -07:00
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
t.Unlock()
|
2015-08-21 14:34:51 -07:00
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
httpClient, err := t.client()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create HTTP client: %s", err)
|
2015-07-05 12:25:45 -07:00
|
|
|
}
|
2016-02-12 06:43:27 -08:00
|
|
|
t.Lock()
|
|
|
|
t.httpClient = httpClient
|
|
|
|
t.Unlock()
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2016-02-08 12:26:00 -08:00
|
|
|
return nil
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
|
|
|
|
2015-07-22 08:48:22 -07:00
|
|
|
func newHTTPClient(cfg *config.ScrapeConfig) (*http.Client, error) {
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsOpts := httputil.TLSOptions{
|
|
|
|
InsecureSkipVerify: cfg.TLSConfig.InsecureSkipVerify,
|
|
|
|
CAFile: cfg.TLSConfig.CAFile,
|
|
|
|
}
|
|
|
|
if len(cfg.TLSConfig.CertFile) > 0 && len(cfg.TLSConfig.KeyFile) > 0 {
|
|
|
|
tlsOpts.CertFile = cfg.TLSConfig.CertFile
|
|
|
|
tlsOpts.KeyFile = cfg.TLSConfig.KeyFile
|
|
|
|
}
|
|
|
|
tlsConfig, err := httputil.NewTLSConfig(tlsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
2016-02-16 02:44:42 -08:00
|
|
|
// The only timeout we care about is the configured scrape timeout.
|
|
|
|
// It is applied on request. So we leave out any timings here.
|
|
|
|
var rt http.RoundTripper = &http.Transport{
|
|
|
|
Proxy: http.ProxyURL(cfg.ProxyURL.URL),
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
2015-07-22 08:48:22 -07:00
|
|
|
|
|
|
|
// If a bearer token is provided, create a round tripper that will set the
|
|
|
|
// Authorization header correctly on each request.
|
|
|
|
bearerToken := cfg.BearerToken
|
|
|
|
if len(bearerToken) == 0 && len(cfg.BearerTokenFile) > 0 {
|
2015-08-24 06:07:27 -07:00
|
|
|
b, err := ioutil.ReadFile(cfg.BearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read bearer token file %s: %s", cfg.BearerTokenFile, err)
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
2015-08-24 06:07:27 -07:00
|
|
|
bearerToken = string(b)
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
2015-08-24 06:07:27 -07:00
|
|
|
|
2015-07-22 08:48:22 -07:00
|
|
|
if len(bearerToken) > 0 {
|
|
|
|
rt = httputil.NewBearerAuthRoundTripper(bearerToken, rt)
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
if cfg.BasicAuth != nil {
|
|
|
|
rt = httputil.NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, rt)
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:48:22 -07:00
|
|
|
// Return a new client with the configured round tripper.
|
|
|
|
return httputil.NewClient(rt), nil
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) String() string {
|
2016-02-12 06:43:27 -08:00
|
|
|
return t.host()
|
|
|
|
}
|
|
|
|
|
2016-02-15 06:22:57 -08:00
|
|
|
// fingerprint returns an identifying hash for the target.
|
|
|
|
func (t *Target) fingerprint() model.Fingerprint {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return t.labels.Fingerprint()
|
|
|
|
}
|
|
|
|
|
|
|
|
// offset returns the time until the next scrape cycle for the target.
|
|
|
|
func (t *Target) offset(interval time.Duration) time.Duration {
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
|
|
|
|
var (
|
|
|
|
base = now % int64(interval)
|
|
|
|
offset = uint64(t.fingerprint()) % uint64(interval)
|
|
|
|
next = base + int64(offset)
|
|
|
|
)
|
|
|
|
|
|
|
|
if next > int64(interval) {
|
|
|
|
next -= int64(interval)
|
|
|
|
}
|
|
|
|
return time.Duration(next)
|
|
|
|
}
|
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
func (t *Target) client() (*http.Client, error) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return newHTTPClient(t.scrapeConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) interval() time.Duration {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return time.Duration(t.scrapeConfig.ScrapeInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) timeout() time.Duration {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return time.Duration(t.scrapeConfig.ScrapeTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) scheme() string {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return string(t.labels[model.SchemeLabel])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) host() string {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return string(t.labels[model.AddressLabel])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) path() string {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
return string(t.labels[model.MetricsPathLabel])
|
|
|
|
}
|
|
|
|
|
2016-02-15 07:10:29 -08:00
|
|
|
// wrapAppender wraps a SampleAppender for samples ingested from the target.
|
2016-02-29 07:33:22 -08:00
|
|
|
// RLock must be acquired by the caller.
|
2016-02-15 07:10:29 -08:00
|
|
|
func (t *Target) wrapAppender(app storage.SampleAppender) storage.SampleAppender {
|
|
|
|
// The relabelAppender has to be inside the label-modifying appenders
|
|
|
|
// so the relabeling rules are applied to the correct label set.
|
|
|
|
if mrc := t.scrapeConfig.MetricRelabelConfigs; len(mrc) > 0 {
|
|
|
|
app = relabelAppender{
|
|
|
|
SampleAppender: app,
|
|
|
|
relabelings: mrc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.scrapeConfig.HonorLabels {
|
|
|
|
app = honorLabelsAppender{
|
|
|
|
SampleAppender: app,
|
2016-02-29 07:33:22 -08:00
|
|
|
labels: t.unlockedLabels(),
|
2016-02-15 07:10:29 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app = ruleLabelsAppender{
|
|
|
|
SampleAppender: app,
|
2016-02-29 07:33:22 -08:00
|
|
|
labels: t.unlockedLabels(),
|
2016-02-15 07:10:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2016-02-15 07:21:03 -08:00
|
|
|
// wrapReportingAppender wraps an appender for target status report samples.
|
|
|
|
// It ignores any relabeling rules set for the target.
|
2016-02-29 07:33:22 -08:00
|
|
|
// RLock must not be acquired by the caller.
|
2016-02-15 07:21:03 -08:00
|
|
|
func (t *Target) wrapReportingAppender(app storage.SampleAppender) storage.SampleAppender {
|
|
|
|
return ruleLabelsAppender{
|
|
|
|
SampleAppender: app,
|
|
|
|
labels: t.Labels(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
// URL returns a copy of the target's URL.
|
|
|
|
func (t *Target) URL() *url.URL {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
|
|
|
|
for k, v := range t.scrapeConfig.Params {
|
|
|
|
params[k] = make([]string, len(v))
|
|
|
|
copy(params[k], v)
|
|
|
|
}
|
|
|
|
for k, v := range t.labels {
|
|
|
|
if !strings.HasPrefix(string(k), model.ParamLabelPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ks := string(k[len(model.ParamLabelPrefix):])
|
|
|
|
|
|
|
|
if len(params[ks]) > 0 {
|
|
|
|
params[ks][0] = string(v)
|
|
|
|
} else {
|
|
|
|
params[ks] = []string{string(v)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: string(t.labels[model.SchemeLabel]),
|
|
|
|
Host: string(t.labels[model.AddressLabel]),
|
|
|
|
Path: string(t.labels[model.MetricsPathLabel]),
|
|
|
|
RawQuery: params.Encode(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstanceIdentifier returns the identifier for the target.
|
|
|
|
func (t *Target) InstanceIdentifier() string {
|
|
|
|
return t.host()
|
|
|
|
}
|
|
|
|
|
2014-10-09 06:59:47 -07:00
|
|
|
// RunScraper implements Target.
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) RunScraper(sampleAppender storage.SampleAppender) {
|
2015-04-18 23:39:33 -07:00
|
|
|
defer close(t.scraperStopped)
|
2014-11-11 07:41:20 -08:00
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
lastScrapeInterval := t.interval()
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Debugf("Starting scraper for target %v...", t)
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2014-07-29 11:31:11 -07:00
|
|
|
select {
|
2016-02-15 06:22:57 -08:00
|
|
|
case <-time.After(t.offset(lastScrapeInterval)):
|
|
|
|
// Continue after scraping offset.
|
2014-12-03 09:07:23 -08:00
|
|
|
case <-t.scraperStopping:
|
2014-07-29 11:31:11 -07:00
|
|
|
return
|
2013-04-26 08:26:52 -07:00
|
|
|
}
|
2014-07-29 11:31:11 -07:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
ticker := time.NewTicker(lastScrapeInterval)
|
2014-07-29 11:31:11 -07:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
t.scrape(sampleAppender)
|
2014-11-11 15:38:28 -08:00
|
|
|
|
|
|
|
// Explanation of the contraption below:
|
|
|
|
//
|
2015-04-18 23:39:33 -07:00
|
|
|
// In case t.scraperStopping has something to receive, we want to read
|
|
|
|
// from that channel rather than starting a new scrape (which might take very
|
|
|
|
// long). That's why the outer select has no ticker.C. Should t.scraperStopping
|
|
|
|
// not have anything to receive, we go into the inner select, where ticker.C
|
|
|
|
// is in the mix.
|
2014-07-29 11:31:11 -07:00
|
|
|
for {
|
|
|
|
select {
|
2014-12-03 09:07:23 -08:00
|
|
|
case <-t.scraperStopping:
|
2014-07-29 11:31:11 -07:00
|
|
|
return
|
2014-11-11 15:38:28 -08:00
|
|
|
default:
|
|
|
|
select {
|
2014-12-03 09:07:23 -08:00
|
|
|
case <-t.scraperStopping:
|
2014-11-11 15:38:28 -08:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2015-05-18 02:13:13 -07:00
|
|
|
took := time.Since(t.status.LastScrape())
|
2015-04-20 03:24:25 -07:00
|
|
|
|
|
|
|
intervalStr := lastScrapeInterval.String()
|
|
|
|
|
|
|
|
// On changed scrape interval the new interval becomes effective
|
|
|
|
// after the next scrape.
|
2016-02-12 06:43:27 -08:00
|
|
|
if iv := t.interval(); iv != lastScrapeInterval {
|
2015-05-12 07:52:56 -07:00
|
|
|
ticker.Stop()
|
2016-02-12 06:43:27 -08:00
|
|
|
ticker = time.NewTicker(iv)
|
|
|
|
lastScrapeInterval = iv
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
targetIntervalLength.WithLabelValues(intervalStr).Observe(
|
2015-02-06 07:44:56 -08:00
|
|
|
float64(took) / float64(time.Second), // Sub-second precision.
|
|
|
|
)
|
2016-01-27 10:07:46 -08:00
|
|
|
if sampleAppender.NeedsThrottling() {
|
|
|
|
targetSkippedScrapes.WithLabelValues(intervalStr).Inc()
|
|
|
|
t.status.setLastError(errSkippedScrape)
|
|
|
|
continue
|
|
|
|
}
|
2015-03-14 19:36:15 -07:00
|
|
|
t.scrape(sampleAppender)
|
2014-11-11 15:38:28 -08:00
|
|
|
}
|
2014-07-29 11:31:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 06:59:47 -07:00
|
|
|
// StopScraper implements Target.
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) StopScraper() {
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Debugf("Stopping scraper for target %v...", t)
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2014-12-03 09:07:23 -08:00
|
|
|
close(t.scraperStopping)
|
|
|
|
<-t.scraperStopped
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Debugf("Scraper for target %v stopped.", t)
|
2013-04-26 08:26:52 -07:00
|
|
|
}
|
2013-01-22 09:37:01 -08:00
|
|
|
|
2014-09-03 07:46:29 -07:00
|
|
|
const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,text/plain;version=0.0.4;q=0.3,application/json;schema="prometheus/telemetry";version=0.0.2;q=0.2,*/*;q=0.1`
|
2013-06-27 09:32:05 -07:00
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
func (t *Target) scrape(appender storage.SampleAppender) error {
|
|
|
|
var (
|
2016-02-15 07:21:03 -08:00
|
|
|
err error
|
|
|
|
start = time.Now()
|
2016-02-12 06:43:27 -08:00
|
|
|
)
|
2015-08-21 14:34:51 -07:00
|
|
|
defer func(appender storage.SampleAppender) {
|
2016-02-15 07:21:03 -08:00
|
|
|
t.report(appender, start, time.Since(start), err)
|
2015-08-21 14:34:51 -07:00
|
|
|
}(appender)
|
|
|
|
|
2015-07-08 12:27:52 -07:00
|
|
|
t.RLock()
|
2015-08-21 14:34:51 -07:00
|
|
|
|
2016-02-15 07:10:29 -08:00
|
|
|
appender = t.wrapAppender(appender)
|
2015-08-21 14:34:51 -07:00
|
|
|
|
2016-02-16 02:44:42 -08:00
|
|
|
client := t.httpClient
|
2015-08-21 14:34:51 -07:00
|
|
|
t.RUnlock()
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-06-22 13:57:32 -07:00
|
|
|
req, err := http.NewRequest("GET", t.URL().String(), nil)
|
2013-06-27 09:32:05 -07:00
|
|
|
if err != nil {
|
2015-09-10 03:10:12 -07:00
|
|
|
return err
|
2013-06-27 09:32:05 -07:00
|
|
|
}
|
|
|
|
req.Header.Add("Accept", acceptHeader)
|
|
|
|
|
2016-02-16 02:44:42 -08:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), t.timeout())
|
|
|
|
resp, err := ctxhttp.Do(ctx, client, req)
|
2013-04-26 08:26:52 -07:00
|
|
|
if err != nil {
|
2013-05-15 22:38:31 -07:00
|
|
|
return err
|
2013-04-26 08:26:52 -07:00
|
|
|
}
|
2015-09-10 13:32:40 -07:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2014-06-16 08:04:08 -07:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("server returned HTTP status %s", resp.Status)
|
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-09-19 02:49:40 -07:00
|
|
|
dec := expfmt.NewDecoder(resp.Body, expfmt.ResponseFormat(resp.Header))
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-08-20 10:02:29 -07:00
|
|
|
sdec := expfmt.SampleDecoder{
|
|
|
|
Dec: dec,
|
|
|
|
Opts: &expfmt.DecodeOptions{
|
|
|
|
Timestamp: model.TimeFromUnixNano(start.UnixNano()),
|
|
|
|
},
|
2013-08-12 06:15:41 -07:00
|
|
|
}
|
2015-03-14 19:36:15 -07:00
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
var (
|
|
|
|
samples model.Vector
|
|
|
|
numOutOfOrder int
|
2016-02-03 01:39:34 -08:00
|
|
|
logger = log.With("target", t.InstanceIdentifier())
|
2016-02-02 05:01:44 -08:00
|
|
|
)
|
2016-01-27 10:07:46 -08:00
|
|
|
for {
|
|
|
|
if err = sdec.Decode(&samples); err != nil {
|
|
|
|
break
|
2015-08-20 10:02:29 -07:00
|
|
|
}
|
2015-03-14 19:36:15 -07:00
|
|
|
for _, s := range samples {
|
2016-02-02 05:01:44 -08:00
|
|
|
err := appender.Append(s)
|
|
|
|
if err != nil {
|
|
|
|
if err == local.ErrOutOfOrderSample {
|
|
|
|
numOutOfOrder++
|
|
|
|
} else {
|
2016-02-03 01:39:34 -08:00
|
|
|
logger.With("sample", s).Warnf("Error inserting sample: %s", err)
|
2016-02-02 05:01:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 05:01:44 -08:00
|
|
|
if numOutOfOrder > 0 {
|
2016-02-03 01:39:34 -08:00
|
|
|
logger.With("numDropped", numOutOfOrder).Warn("Error on ingesting out-of-order samples")
|
2016-02-02 05:01:44 -08:00
|
|
|
}
|
2015-08-20 10:02:29 -07:00
|
|
|
|
|
|
|
if err == io.EOF {
|
2016-02-15 08:23:04 -08:00
|
|
|
// Set err to nil since it is used in the scrape health recording.
|
|
|
|
err = nil
|
2015-08-20 10:02:29 -07:00
|
|
|
}
|
2015-03-14 19:36:15 -07:00
|
|
|
return err
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
2013-01-12 12:22:59 -08:00
|
|
|
|
2016-02-15 07:21:03 -08:00
|
|
|
func (t *Target) report(app storage.SampleAppender, start time.Time, duration time.Duration, err error) {
|
|
|
|
t.status.setLastScrape(start)
|
|
|
|
t.status.setLastError(err)
|
|
|
|
|
|
|
|
ts := model.TimeFromUnixNano(start.UnixNano())
|
|
|
|
|
|
|
|
var health model.SampleValue
|
|
|
|
if err == nil {
|
|
|
|
health = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
healthSample := &model.Sample{
|
|
|
|
Metric: model.Metric{
|
|
|
|
model.MetricNameLabel: scrapeHealthMetricName,
|
|
|
|
},
|
|
|
|
Timestamp: ts,
|
|
|
|
Value: health,
|
|
|
|
}
|
|
|
|
durationSample := &model.Sample{
|
|
|
|
Metric: model.Metric{
|
|
|
|
model.MetricNameLabel: scrapeDurationMetricName,
|
|
|
|
},
|
|
|
|
Timestamp: ts,
|
|
|
|
Value: model.SampleValue(float64(duration) / float64(time.Second)),
|
|
|
|
}
|
|
|
|
|
|
|
|
app = t.wrapReportingAppender(app)
|
|
|
|
|
|
|
|
app.Append(healthSample)
|
|
|
|
app.Append(durationSample)
|
|
|
|
}
|
|
|
|
|
2015-08-21 14:34:51 -07:00
|
|
|
// Merges the ingested sample's metric with the label set. On a collision the
|
|
|
|
// value of the ingested label is stored in a label prefixed with 'exported_'.
|
|
|
|
type ruleLabelsAppender struct {
|
2016-01-27 10:07:46 -08:00
|
|
|
storage.SampleAppender
|
2015-08-21 14:34:51 -07:00
|
|
|
labels model.LabelSet
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
func (app ruleLabelsAppender) Append(s *model.Sample) error {
|
2015-08-21 14:34:51 -07:00
|
|
|
for ln, lv := range app.labels {
|
|
|
|
if v, ok := s.Metric[ln]; ok && v != "" {
|
|
|
|
s.Metric[model.ExportedLabelPrefix+ln] = v
|
|
|
|
}
|
|
|
|
s.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
return app.SampleAppender.Append(s)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type honorLabelsAppender struct {
|
2016-01-27 10:07:46 -08:00
|
|
|
storage.SampleAppender
|
2015-08-21 14:34:51 -07:00
|
|
|
labels model.LabelSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merges the sample's metric with the given labels if the label is not
|
|
|
|
// already present in the metric.
|
|
|
|
// This also considers labels explicitly set to the empty string.
|
2016-02-02 05:01:44 -08:00
|
|
|
func (app honorLabelsAppender) Append(s *model.Sample) error {
|
2015-08-21 14:34:51 -07:00
|
|
|
for ln, lv := range app.labels {
|
|
|
|
if _, ok := s.Metric[ln]; !ok {
|
|
|
|
s.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
return app.SampleAppender.Append(s)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Applies a set of relabel configurations to the sample's metric
|
|
|
|
// before actually appending it.
|
|
|
|
type relabelAppender struct {
|
2016-01-27 10:07:46 -08:00
|
|
|
storage.SampleAppender
|
2015-08-21 14:34:51 -07:00
|
|
|
relabelings []*config.RelabelConfig
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
func (app relabelAppender) Append(s *model.Sample) error {
|
2015-08-21 14:34:51 -07:00
|
|
|
labels, err := Relabel(model.LabelSet(s.Metric), app.relabelings...)
|
|
|
|
if err != nil {
|
2016-02-02 05:01:44 -08:00
|
|
|
return fmt.Errorf("metric relabeling error %s: %s", s.Metric, err)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
// Check if the timeseries was dropped.
|
|
|
|
if labels == nil {
|
2016-02-02 05:01:44 -08:00
|
|
|
return nil
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
s.Metric = model.Metric(labels)
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
return app.SampleAppender.Append(s)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
|
2016-02-15 01:31:38 -08:00
|
|
|
// Labels returns a copy of the set of all public labels of the target.
|
|
|
|
func (t *Target) Labels() model.LabelSet {
|
2015-04-20 03:24:25 -07:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-08-21 14:34:51 -07:00
|
|
|
|
2016-02-29 07:33:22 -08:00
|
|
|
return t.unlockedLabels()
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlockedLabels does the same as Labels but does not lock the mutex (useful
|
|
|
|
// for internal usage when the mutex is already locked).
|
|
|
|
func (t *Target) unlockedLabels() model.LabelSet {
|
2016-02-12 06:43:27 -08:00
|
|
|
lset := make(model.LabelSet, len(t.labels))
|
|
|
|
for ln, lv := range t.labels {
|
|
|
|
if !strings.HasPrefix(string(ln), model.ReservedLabelPrefix) {
|
|
|
|
lset[ln] = lv
|
|
|
|
}
|
2015-09-25 13:51:49 -07:00
|
|
|
}
|
2015-04-25 03:59:05 -07:00
|
|
|
|
2016-02-12 06:43:27 -08:00
|
|
|
if _, ok := lset[model.InstanceLabel]; !ok {
|
|
|
|
lset[model.InstanceLabel] = t.labels[model.AddressLabel]
|
2015-03-18 10:53:43 -07:00
|
|
|
}
|
2016-02-12 06:43:27 -08:00
|
|
|
|
2015-05-18 03:16:25 -07:00
|
|
|
return lset
|
2015-03-18 10:53:43 -07:00
|
|
|
}
|
|
|
|
|
2015-06-05 13:42:39 -07:00
|
|
|
// MetaLabels returns a copy of the target's labels before any processing.
|
2015-08-20 08:18:46 -07:00
|
|
|
func (t *Target) MetaLabels() model.LabelSet {
|
2015-06-05 13:42:39 -07:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2016-02-12 06:43:27 -08:00
|
|
|
|
|
|
|
return t.metaLabels.Clone()
|
2015-06-05 13:42:39 -07:00
|
|
|
}
|