2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-04 11:17:31 +00: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 21:03:56 +00:00
|
|
|
|
2012-12-25 12:50:36 +00:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
2015-03-15 02:36:15 +00:00
|
|
|
"errors"
|
2013-01-04 13:41:47 +00:00
|
|
|
"fmt"
|
2015-08-20 17:02:29 +00:00
|
|
|
"io"
|
2015-07-22 15:48:22 +00:00
|
|
|
"io/ioutil"
|
2014-07-29 18:31:11 +00:00
|
|
|
"math/rand"
|
2013-01-04 13:41:47 +00:00
|
|
|
"net/http"
|
2015-02-19 17:58:47 +00:00
|
|
|
"net/url"
|
2013-04-10 12:26:07 +00:00
|
|
|
"strings"
|
2014-12-03 17:07:23 +00:00
|
|
|
"sync"
|
2012-12-25 12:50:36 +00:00
|
|
|
"time"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 17:02:29 +00:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-08-20 17:02:29 +00:00
|
|
|
"github.com/prometheus/log"
|
2013-07-30 15:18:07 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2015-03-15 02:36:15 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2015-05-29 11:30:30 +00:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2012-12-25 12:50:36 +00:00
|
|
|
)
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
const (
|
2015-08-21 21:01:08 +00:00
|
|
|
scrapeHealthMetricName = "up"
|
|
|
|
scrapeDurationMetricName = "scrape_duration_seconds"
|
|
|
|
|
2015-03-15 02:36:15 +00:00
|
|
|
// Capacity of the channel to buffer samples during ingestion.
|
|
|
|
ingestedSamplesCap = 256
|
2014-06-18 17:43:15 +00:00
|
|
|
|
|
|
|
// Constants for instrumentation.
|
2014-07-23 17:55:33 +00:00
|
|
|
namespace = "prometheus"
|
2014-07-29 18:31:11 +00:00
|
|
|
interval = "interval"
|
2014-06-18 17:43:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-03-15 02:36:15 +00:00
|
|
|
errIngestChannelFull = errors.New("ingestion channel full")
|
|
|
|
|
2014-07-29 18:31:11 +00:00
|
|
|
targetIntervalLength = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "target_interval_length_seconds",
|
|
|
|
Help: "Actual intervals between scrapes.",
|
2015-01-21 14:42:25 +00: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 18:31:11 +00:00
|
|
|
},
|
|
|
|
[]string{interval},
|
|
|
|
)
|
2013-04-10 12:26:07 +00:00
|
|
|
)
|
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
func init() {
|
2014-07-29 18:31:11 +00:00
|
|
|
prometheus.MustRegister(targetIntervalLength)
|
2014-06-18 17:43:15 +00:00
|
|
|
}
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
// TargetHealth describes the health state of a target.
|
|
|
|
type TargetHealth int
|
2012-12-25 12:50:36 +00:00
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
func (t TargetHealth) String() string {
|
2013-03-21 10:52:42 +00:00
|
|
|
switch t {
|
2015-05-18 11:14:41 +00:00
|
|
|
case HealthUnknown:
|
2015-06-23 15:46:57 +00:00
|
|
|
return "unknown"
|
2015-05-18 11:14:41 +00:00
|
|
|
case HealthGood:
|
2015-06-23 15:46:57 +00:00
|
|
|
return "healthy"
|
2015-05-18 11:14:41 +00:00
|
|
|
case HealthBad:
|
2015-06-23 15:46:57 +00:00
|
|
|
return "unhealthy"
|
2013-03-21 10:52:42 +00:00
|
|
|
}
|
|
|
|
panic("unknown state")
|
|
|
|
}
|
|
|
|
|
2015-08-21 21:01:08 +00:00
|
|
|
func (t TargetHealth) value() model.SampleValue {
|
|
|
|
if t == HealthGood {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2012-12-25 12:50:36 +00:00
|
|
|
const (
|
2015-08-24 13:07:27 +00:00
|
|
|
// HealthUnknown is the state of a Target before it is first scraped.
|
2015-05-18 11:14:41 +00:00
|
|
|
HealthUnknown TargetHealth = iota
|
2015-08-24 13:07:27 +00:00
|
|
|
// HealthGood is the state of a Target that has been successfully scraped.
|
2015-05-18 11:14:41 +00:00
|
|
|
HealthGood
|
2015-08-24 13:07:27 +00:00
|
|
|
// HealthBad is the state of a Target that was scraped unsuccessfully.
|
2015-05-18 11:14:41 +00:00
|
|
|
HealthBad
|
2012-12-25 12:50:36 +00:00
|
|
|
)
|
|
|
|
|
2015-05-18 09:13:13 +00:00
|
|
|
// TargetStatus contains information about the current status of a scrape target.
|
|
|
|
type TargetStatus struct {
|
|
|
|
lastError error
|
|
|
|
lastScrape time.Time
|
2015-05-18 11:14:41 +00:00
|
|
|
health TargetHealth
|
2015-05-18 09:13:13 +00: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()
|
|
|
|
return ts.lastError
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastScrape returns the time of the last scrape.
|
|
|
|
func (ts *TargetStatus) LastScrape() time.Time {
|
|
|
|
ts.mu.RLock()
|
|
|
|
defer ts.mu.RUnlock()
|
|
|
|
return ts.lastScrape
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
// Health returns the last known health state of the target.
|
|
|
|
func (ts *TargetStatus) Health() TargetHealth {
|
2015-05-18 09:13:13 +00:00
|
|
|
ts.mu.RLock()
|
|
|
|
defer ts.mu.RUnlock()
|
2015-05-18 11:14:41 +00:00
|
|
|
return ts.health
|
2015-05-18 09:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TargetStatus) setLastScrape(t time.Time) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
ts.lastScrape = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TargetStatus) setLastError(err error) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
if err == nil {
|
2015-05-18 11:14:41 +00:00
|
|
|
ts.health = HealthGood
|
2015-05-18 09:13:13 +00:00
|
|
|
} else {
|
2015-05-18 11:14:41 +00:00
|
|
|
ts.health = HealthBad
|
2015-05-18 09:13:13 +00:00
|
|
|
}
|
|
|
|
ts.lastError = err
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00: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 17:07:23 +00:00
|
|
|
// Closing scraperStopping signals that scraping should stop.
|
|
|
|
scraperStopping chan struct{}
|
|
|
|
// Closing scraperStopped signals that scraping has been stopped.
|
|
|
|
scraperStopped chan struct{}
|
2015-03-15 02:36:15 +00:00
|
|
|
// Channel to buffer ingested samples.
|
2015-08-21 15:44:24 +00:00
|
|
|
ingestedSamples chan model.Vector
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
// Mutex protects the members below.
|
2015-04-19 06:39:33 +00:00
|
|
|
sync.RWMutex
|
2015-07-08 19:27:52 +00:00
|
|
|
// The HTTP client used to scrape the target's endpoint.
|
|
|
|
httpClient *http.Client
|
2015-05-18 11:14:41 +00:00
|
|
|
// url is the URL to be scraped. Its host is immutable.
|
2015-04-20 10:24:25 +00:00
|
|
|
url *url.URL
|
2015-06-05 20:42:39 +00:00
|
|
|
// Labels before any processing.
|
2015-08-20 15:18:46 +00:00
|
|
|
metaLabels model.LabelSet
|
2015-04-20 10:24:25 +00:00
|
|
|
// Any base labels that are added to this target and its metrics.
|
2015-08-20 15:18:46 +00:00
|
|
|
baseLabels model.LabelSet
|
2015-04-20 10:24:25 +00:00
|
|
|
// What is the deadline for the HTTP or HTTPS against this endpoint.
|
|
|
|
deadline time.Duration
|
|
|
|
// The time between two scrapes.
|
|
|
|
scrapeInterval time.Duration
|
2015-06-22 20:35:19 +00:00
|
|
|
// Whether the target's labels have precedence over the base labels
|
|
|
|
// assigned by the scraping instance.
|
|
|
|
honorLabels bool
|
2015-07-08 19:27:52 +00:00
|
|
|
// Metric relabel configuration.
|
|
|
|
metricRelabelConfigs []*config.RelabelConfig
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// NewTarget creates a reasonably configured target for querying.
|
2015-08-20 15:18:46 +00:00
|
|
|
func NewTarget(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet) *Target {
|
2015-05-18 11:14:41 +00:00
|
|
|
t := &Target{
|
2015-04-20 10:24:25 +00:00
|
|
|
url: &url.URL{
|
2015-08-20 15:18:46 +00:00
|
|
|
Scheme: string(baseLabels[model.SchemeLabel]),
|
|
|
|
Host: string(baseLabels[model.AddressLabel]),
|
2015-04-20 10:24:25 +00:00
|
|
|
},
|
2015-05-18 09:13:13 +00:00
|
|
|
status: &TargetStatus{},
|
2014-12-03 17:07:23 +00:00
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2015-06-05 20:42:39 +00:00
|
|
|
t.Update(cfg, baseLabels, metaLabels)
|
2015-03-15 02:36:15 +00:00
|
|
|
return t
|
|
|
|
}
|
2013-04-16 15:22:10 +00:00
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
// Status returns the status of the target.
|
|
|
|
func (t *Target) Status() *TargetStatus {
|
2015-05-18 09:13:13 +00:00
|
|
|
return t.status
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
// Update overwrites settings in the target that are derived from the job config
|
|
|
|
// it belongs to.
|
2015-08-20 15:18:46 +00:00
|
|
|
func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels model.LabelSet) {
|
2015-04-20 10:24:25 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2015-07-22 15:48:22 +00:00
|
|
|
httpClient, err := newHTTPClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot create HTTP client: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.httpClient = httpClient
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
t.url.Scheme = string(baseLabels[model.SchemeLabel])
|
|
|
|
t.url.Path = string(baseLabels[model.MetricsPathLabel])
|
2015-08-21 21:34:51 +00:00
|
|
|
|
2015-07-05 19:25:45 +00:00
|
|
|
params := url.Values{}
|
2015-08-21 21:34:51 +00:00
|
|
|
|
2015-07-05 19:25:45 +00:00
|
|
|
for k, v := range cfg.Params {
|
|
|
|
params[k] = make([]string, len(v))
|
|
|
|
copy(params[k], v)
|
|
|
|
}
|
|
|
|
for k, v := range baseLabels {
|
2015-08-20 15:18:46 +00:00
|
|
|
if strings.HasPrefix(string(k), model.ParamLabelPrefix) {
|
|
|
|
if len(params[string(k[len(model.ParamLabelPrefix):])]) > 0 {
|
|
|
|
params[string(k[len(model.ParamLabelPrefix):])][0] = string(v)
|
2015-07-05 19:25:45 +00:00
|
|
|
} else {
|
2015-08-20 15:18:46 +00:00
|
|
|
params[string(k[len(model.ParamLabelPrefix):])] = []string{string(v)}
|
2015-07-05 19:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.url.RawQuery = params.Encode()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-07 08:55:03 +00:00
|
|
|
t.scrapeInterval = time.Duration(cfg.ScrapeInterval)
|
|
|
|
t.deadline = time.Duration(cfg.ScrapeTimeout)
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-06-22 20:35:19 +00:00
|
|
|
t.honorLabels = cfg.HonorLabels
|
2015-06-05 20:42:39 +00:00
|
|
|
t.metaLabels = metaLabels
|
2015-08-20 15:18:46 +00:00
|
|
|
t.baseLabels = model.LabelSet{}
|
2015-04-25 10:59:05 +00:00
|
|
|
// All remaining internal labels will not be part of the label set.
|
2015-04-20 10:24:25 +00:00
|
|
|
for name, val := range baseLabels {
|
2015-08-20 15:18:46 +00:00
|
|
|
if !strings.HasPrefix(string(name), model.ReservedLabelPrefix) {
|
2015-04-25 10:59:05 +00:00
|
|
|
t.baseLabels[name] = val
|
|
|
|
}
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
if _, ok := t.baseLabels[model.InstanceLabel]; !ok {
|
|
|
|
t.baseLabels[model.InstanceLabel] = model.LabelValue(t.InstanceIdentifier())
|
2015-04-28 22:08:58 +00:00
|
|
|
}
|
2015-06-12 21:16:13 +00:00
|
|
|
t.metricRelabelConfigs = cfg.MetricRelabelConfigs
|
2015-04-20 10:24:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-22 15:48:22 +00:00
|
|
|
func newHTTPClient(cfg *config.ScrapeConfig) (*http.Client, error) {
|
2015-09-06 23:07:44 +00:00
|
|
|
rt := httputil.NewDeadlineRoundTripper(time.Duration(cfg.ScrapeTimeout), cfg.ProxyURL.URL)
|
2015-07-22 15:48:22 +00:00
|
|
|
|
2015-09-06 23:07:44 +00: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 15:48:22 +00:00
|
|
|
}
|
|
|
|
// Get a default roundtripper with the scrape timeout.
|
|
|
|
tr := rt.(*http.Transport)
|
|
|
|
// Set the TLS config from above
|
|
|
|
tr.TLSClientConfig = tlsConfig
|
|
|
|
rt = tr
|
|
|
|
|
|
|
|
// 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 13:07:27 +00: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 15:48:22 +00:00
|
|
|
}
|
2015-08-24 13:07:27 +00:00
|
|
|
bearerToken = string(b)
|
2015-07-22 15:48:22 +00:00
|
|
|
}
|
2015-08-24 13:07:27 +00:00
|
|
|
|
2015-07-22 15:48:22 +00:00
|
|
|
if len(bearerToken) > 0 {
|
|
|
|
rt = httputil.NewBearerAuthRoundTripper(bearerToken, rt)
|
|
|
|
}
|
|
|
|
|
2015-07-18 21:23:58 +00:00
|
|
|
if cfg.BasicAuth != nil {
|
|
|
|
rt = httputil.NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, rt)
|
|
|
|
}
|
|
|
|
|
2015-07-22 15:48:22 +00:00
|
|
|
// Return a new client with the configured round tripper.
|
|
|
|
return httputil.NewClient(rt), nil
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
func (t *Target) String() string {
|
2015-04-20 10:24:25 +00:00
|
|
|
return t.url.Host
|
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// RunScraper implements Target.
|
2015-05-18 11:14:41 +00:00
|
|
|
func (t *Target) RunScraper(sampleAppender storage.SampleAppender) {
|
2015-04-19 06:39:33 +00:00
|
|
|
defer close(t.scraperStopped)
|
2014-11-11 15:41:20 +00:00
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
t.RLock()
|
|
|
|
lastScrapeInterval := t.scrapeInterval
|
|
|
|
t.RUnlock()
|
|
|
|
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Debugf("Starting scraper for target %v...", t)
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
jitterTimer := time.NewTimer(time.Duration(float64(lastScrapeInterval) * rand.Float64()))
|
2014-07-29 18:31:11 +00:00
|
|
|
select {
|
|
|
|
case <-jitterTimer.C:
|
2014-12-03 17:07:23 +00:00
|
|
|
case <-t.scraperStopping:
|
2014-11-11 23:38:28 +00:00
|
|
|
jitterTimer.Stop()
|
2014-07-29 18:31:11 +00:00
|
|
|
return
|
2013-04-26 15:26:52 +00:00
|
|
|
}
|
2014-07-29 18:31:11 +00:00
|
|
|
jitterTimer.Stop()
|
|
|
|
|
2015-04-20 10:24:25 +00:00
|
|
|
ticker := time.NewTicker(lastScrapeInterval)
|
2014-07-29 18:31:11 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2015-05-18 09:13:13 +00:00
|
|
|
t.status.setLastScrape(time.Now())
|
2015-03-15 02:36:15 +00:00
|
|
|
t.scrape(sampleAppender)
|
2014-11-11 23:38:28 +00:00
|
|
|
|
|
|
|
// Explanation of the contraption below:
|
|
|
|
//
|
2015-04-19 06:39:33 +00: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 18:31:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
2014-12-03 17:07:23 +00:00
|
|
|
case <-t.scraperStopping:
|
2014-07-29 18:31:11 +00:00
|
|
|
return
|
2014-11-11 23:38:28 +00:00
|
|
|
default:
|
|
|
|
select {
|
2014-12-03 17:07:23 +00:00
|
|
|
case <-t.scraperStopping:
|
2014-11-11 23:38:28 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2015-05-18 09:13:13 +00:00
|
|
|
took := time.Since(t.status.LastScrape())
|
|
|
|
t.status.setLastScrape(time.Now())
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
intervalStr := lastScrapeInterval.String()
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
t.RLock()
|
2015-04-20 10:24:25 +00:00
|
|
|
// On changed scrape interval the new interval becomes effective
|
|
|
|
// after the next scrape.
|
|
|
|
if lastScrapeInterval != t.scrapeInterval {
|
2015-05-12 14:52:56 +00:00
|
|
|
ticker.Stop()
|
2015-04-20 10:24:25 +00:00
|
|
|
ticker = time.NewTicker(t.scrapeInterval)
|
|
|
|
lastScrapeInterval = t.scrapeInterval
|
|
|
|
}
|
2015-05-18 11:14:41 +00:00
|
|
|
t.RUnlock()
|
2015-04-20 10:24:25 +00:00
|
|
|
|
|
|
|
targetIntervalLength.WithLabelValues(intervalStr).Observe(
|
2015-02-06 15:44:56 +00:00
|
|
|
float64(took) / float64(time.Second), // Sub-second precision.
|
|
|
|
)
|
2015-03-15 02:36:15 +00:00
|
|
|
t.scrape(sampleAppender)
|
2014-11-11 23:38:28 +00:00
|
|
|
}
|
2014-07-29 18:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// StopScraper implements Target.
|
2015-05-18 11:14:41 +00:00
|
|
|
func (t *Target) StopScraper() {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Debugf("Stopping scraper for target %v...", t)
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2014-12-03 17:07:23 +00:00
|
|
|
close(t.scraperStopping)
|
|
|
|
<-t.scraperStopped
|
2015-04-20 10:24:25 +00:00
|
|
|
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Debugf("Scraper for target %v stopped.", t)
|
2013-04-26 15:26:52 +00:00
|
|
|
}
|
2013-01-22 17:37:01 +00:00
|
|
|
|
2015-08-21 15:44:24 +00:00
|
|
|
func (t *Target) ingest(s model.Vector) error {
|
|
|
|
t.RLock()
|
|
|
|
deadline := t.deadline
|
|
|
|
t.RUnlock()
|
|
|
|
// Since the regular case is that ingestedSamples is ready to receive,
|
|
|
|
// first try without setting a timeout so that we don't need to allocate
|
|
|
|
// a timer most of the time.
|
|
|
|
select {
|
|
|
|
case t.ingestedSamples <- s:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case t.ingestedSamples <- s:
|
|
|
|
return nil
|
|
|
|
case <-time.After(deadline / 10):
|
|
|
|
return errIngestChannelFull
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:46:29 +00: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 16:32:05 +00:00
|
|
|
|
2015-08-21 21:34:51 +00:00
|
|
|
func (t *Target) scrape(appender storage.SampleAppender) (err error) {
|
2015-05-18 09:13:13 +00:00
|
|
|
start := time.Now()
|
2015-05-18 11:14:41 +00:00
|
|
|
baseLabels := t.BaseLabels()
|
2015-04-19 06:39:33 +00:00
|
|
|
|
2015-08-21 21:34:51 +00:00
|
|
|
defer func(appender storage.SampleAppender) {
|
|
|
|
t.status.setLastError(err)
|
|
|
|
recordScrapeHealth(appender, start, baseLabels, t.status.Health(), time.Since(start))
|
|
|
|
}(appender)
|
|
|
|
|
2015-07-08 19:27:52 +00:00
|
|
|
t.RLock()
|
|
|
|
|
2015-08-21 21:34:51 +00:00
|
|
|
// The relabelAppender has to be inside the label-modifying appenders
|
|
|
|
// so the relabeling rules are applied to the correct label set.
|
|
|
|
if len(t.metricRelabelConfigs) > 0 {
|
|
|
|
appender = relabelAppender{
|
|
|
|
app: appender,
|
|
|
|
relabelings: t.metricRelabelConfigs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.honorLabels {
|
|
|
|
appender = honorLabelsAppender{
|
|
|
|
app: appender,
|
|
|
|
labels: baseLabels,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
appender = ruleLabelsAppender{
|
|
|
|
app: appender,
|
|
|
|
labels: baseLabels,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient := t.httpClient
|
|
|
|
|
|
|
|
t.RUnlock()
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2015-06-22 20:57:32 +00:00
|
|
|
req, err := http.NewRequest("GET", t.URL().String(), nil)
|
2013-06-27 16:32:05 +00:00
|
|
|
if err != nil {
|
2015-09-10 10:10:12 +00:00
|
|
|
return err
|
2013-06-27 16:32:05 +00:00
|
|
|
}
|
|
|
|
req.Header.Add("Accept", acceptHeader)
|
|
|
|
|
2015-07-08 19:27:52 +00:00
|
|
|
resp, err := httpClient.Do(req)
|
2013-04-26 15:26:52 +00:00
|
|
|
if err != nil {
|
2013-05-16 05:38:31 +00:00
|
|
|
return err
|
2013-04-26 15:26:52 +00:00
|
|
|
}
|
2014-06-16 15:04:08 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("server returned HTTP status %s", resp.Status)
|
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2015-08-20 17:02:29 +00:00
|
|
|
dec, err := expfmt.NewDecoder(resp.Body, resp.Header)
|
2013-04-26 15:26:52 +00:00
|
|
|
if err != nil {
|
2013-05-16 05:38:31 +00:00
|
|
|
return err
|
2013-04-26 15:26:52 +00:00
|
|
|
}
|
2015-08-20 17:02:29 +00:00
|
|
|
defer resp.Body.Close()
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2015-08-20 17:02:29 +00:00
|
|
|
sdec := expfmt.SampleDecoder{
|
|
|
|
Dec: dec,
|
|
|
|
Opts: &expfmt.DecodeOptions{
|
|
|
|
Timestamp: model.TimeFromUnixNano(start.UnixNano()),
|
|
|
|
},
|
2013-08-12 13:15:41 +00:00
|
|
|
}
|
2015-03-15 02:36:15 +00:00
|
|
|
|
2015-08-21 15:44:24 +00:00
|
|
|
t.ingestedSamples = make(chan model.Vector, ingestedSamplesCap)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2015-08-21 21:34:51 +00:00
|
|
|
// TODO(fabxc): Change the SampleAppender interface to return an error
|
2015-08-21 15:44:24 +00:00
|
|
|
// so we can proceed based on the status and don't leak goroutines trying
|
|
|
|
// to append a single sample after dropping all the other ones.
|
|
|
|
//
|
|
|
|
// This will also allow use to reuse this vector and save allocations.
|
|
|
|
var samples model.Vector
|
|
|
|
if err = sdec.Decode(&samples); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err = t.ingest(samples); err != nil {
|
|
|
|
break
|
|
|
|
}
|
2015-08-20 17:02:29 +00:00
|
|
|
}
|
2015-08-21 15:44:24 +00:00
|
|
|
close(t.ingestedSamples)
|
|
|
|
}()
|
2015-08-20 17:02:29 +00:00
|
|
|
|
2015-08-21 15:44:24 +00:00
|
|
|
for samples := range t.ingestedSamples {
|
2015-03-15 02:36:15 +00:00
|
|
|
for _, s := range samples {
|
2015-08-21 21:34:51 +00:00
|
|
|
appender.Append(s)
|
2015-03-15 02:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 17:02:29 +00:00
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-15 02:36:15 +00:00
|
|
|
return err
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
2013-01-12 20:22:59 +00:00
|
|
|
|
2015-08-21 21:34:51 +00: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 {
|
|
|
|
app storage.SampleAppender
|
|
|
|
labels model.LabelSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app ruleLabelsAppender) Append(s *model.Sample) {
|
|
|
|
for ln, lv := range app.labels {
|
|
|
|
if v, ok := s.Metric[ln]; ok && v != "" {
|
|
|
|
s.Metric[model.ExportedLabelPrefix+ln] = v
|
|
|
|
}
|
|
|
|
s.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
|
|
|
|
app.app.Append(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type honorLabelsAppender struct {
|
|
|
|
app storage.SampleAppender
|
|
|
|
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.
|
|
|
|
func (app honorLabelsAppender) Append(s *model.Sample) {
|
|
|
|
for ln, lv := range app.labels {
|
|
|
|
if _, ok := s.Metric[ln]; !ok {
|
|
|
|
s.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.app.Append(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applies a set of relabel configurations to the sample's metric
|
|
|
|
// before actually appending it.
|
|
|
|
type relabelAppender struct {
|
|
|
|
app storage.SampleAppender
|
|
|
|
relabelings []*config.RelabelConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app relabelAppender) Append(s *model.Sample) {
|
|
|
|
labels, err := Relabel(model.LabelSet(s.Metric), app.relabelings...)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error while relabeling metric %s: %s", s.Metric, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check if the timeseries was dropped.
|
|
|
|
if labels == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.Metric = model.Metric(labels)
|
|
|
|
|
|
|
|
app.app.Append(s)
|
|
|
|
}
|
|
|
|
|
2015-06-22 20:57:32 +00:00
|
|
|
// URL returns a copy of the target's URL.
|
|
|
|
func (t *Target) URL() *url.URL {
|
2015-04-20 10:24:25 +00:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-08-21 21:34:51 +00:00
|
|
|
|
2015-06-22 20:57:32 +00:00
|
|
|
u := &url.URL{}
|
|
|
|
*u = *t.url
|
|
|
|
return u
|
2013-01-13 09:46:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
// InstanceIdentifier returns the identifier for the target.
|
|
|
|
func (t *Target) InstanceIdentifier() string {
|
2015-04-20 10:24:25 +00:00
|
|
|
return t.url.Host
|
2015-02-19 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
// fullLabels returns the base labels plus internal labels defining the target.
|
2015-08-20 15:18:46 +00:00
|
|
|
func (t *Target) fullLabels() model.LabelSet {
|
2015-04-25 10:59:05 +00:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-08-20 15:18:46 +00:00
|
|
|
lset := make(model.LabelSet, len(t.baseLabels)+2)
|
2015-04-25 10:59:05 +00:00
|
|
|
for ln, lv := range t.baseLabels {
|
2015-05-18 10:16:25 +00:00
|
|
|
lset[ln] = lv
|
2015-04-25 10:59:05 +00:00
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
lset[model.MetricsPathLabel] = model.LabelValue(t.url.Path)
|
|
|
|
lset[model.AddressLabel] = model.LabelValue(t.url.Host)
|
|
|
|
lset[model.SchemeLabel] = model.LabelValue(t.url.Scheme)
|
2015-05-18 10:16:25 +00:00
|
|
|
return lset
|
2015-04-25 10:59:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
// BaseLabels returns a copy of the target's base labels.
|
2015-08-20 15:18:46 +00:00
|
|
|
func (t *Target) BaseLabels() model.LabelSet {
|
2015-04-19 06:39:33 +00:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-08-20 15:18:46 +00:00
|
|
|
lset := make(model.LabelSet, len(t.baseLabels))
|
2015-04-25 10:59:05 +00:00
|
|
|
for ln, lv := range t.baseLabels {
|
2015-05-18 10:16:25 +00:00
|
|
|
lset[ln] = lv
|
2015-03-18 17:53:43 +00:00
|
|
|
}
|
2015-05-18 10:16:25 +00:00
|
|
|
return lset
|
2015-03-18 17:53:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:42:39 +00:00
|
|
|
// MetaLabels returns a copy of the target's labels before any processing.
|
2015-08-20 15:18:46 +00:00
|
|
|
func (t *Target) MetaLabels() model.LabelSet {
|
2015-06-05 20:42:39 +00:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-08-20 15:18:46 +00:00
|
|
|
lset := make(model.LabelSet, len(t.metaLabels))
|
2015-06-05 20:42:39 +00:00
|
|
|
for ln, lv := range t.metaLabels {
|
|
|
|
lset[ln] = lv
|
|
|
|
}
|
|
|
|
return lset
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:14:41 +00:00
|
|
|
func recordScrapeHealth(
|
|
|
|
sampleAppender storage.SampleAppender,
|
2015-08-20 17:02:29 +00:00
|
|
|
timestamp time.Time,
|
2015-08-20 15:18:46 +00:00
|
|
|
baseLabels model.LabelSet,
|
2015-05-18 11:14:41 +00:00
|
|
|
health TargetHealth,
|
|
|
|
scrapeDuration time.Duration,
|
|
|
|
) {
|
2015-08-20 15:18:46 +00:00
|
|
|
healthMetric := make(model.Metric, len(baseLabels)+1)
|
|
|
|
durationMetric := make(model.Metric, len(baseLabels)+1)
|
2015-05-18 10:16:25 +00:00
|
|
|
|
2015-08-21 21:01:08 +00:00
|
|
|
healthMetric[model.MetricNameLabel] = scrapeHealthMetricName
|
|
|
|
durationMetric[model.MetricNameLabel] = scrapeDurationMetricName
|
2015-03-15 02:36:15 +00:00
|
|
|
|
2015-08-21 21:01:08 +00:00
|
|
|
for ln, lv := range baseLabels {
|
|
|
|
healthMetric[ln] = lv
|
|
|
|
durationMetric[ln] = lv
|
2015-03-15 02:36:15 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:02:29 +00:00
|
|
|
ts := model.TimeFromUnixNano(timestamp.UnixNano())
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
healthSample := &model.Sample{
|
2015-03-15 02:36:15 +00:00
|
|
|
Metric: healthMetric,
|
2015-08-20 17:02:29 +00:00
|
|
|
Timestamp: ts,
|
2015-08-21 21:01:08 +00:00
|
|
|
Value: health.value(),
|
2015-03-15 02:36:15 +00:00
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
durationSample := &model.Sample{
|
2015-03-15 02:36:15 +00:00
|
|
|
Metric: durationMetric,
|
2015-08-20 17:02:29 +00:00
|
|
|
Timestamp: ts,
|
2015-08-20 15:18:46 +00:00
|
|
|
Value: model.SampleValue(float64(scrapeDuration) / float64(time.Second)),
|
2015-03-15 02:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sampleAppender.Append(healthSample)
|
|
|
|
sampleAppender.Append(durationSample)
|
|
|
|
}
|