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"
|
2014-07-29 11:31:11 -07:00
|
|
|
"math/rand"
|
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
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/extraction"
|
2014-06-18 10:43:15 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-20 09:10:29 -07:00
|
|
|
"github.com/prometheus/log"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
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"
|
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 (
|
2014-12-10 07:16:49 -08:00
|
|
|
// ScrapeHealthMetricName is the metric name for the synthetic health
|
|
|
|
// variable.
|
2014-12-26 04:37:30 -08:00
|
|
|
scrapeHealthMetricName clientmodel.LabelValue = "up"
|
|
|
|
// ScrapeTimeMetricName is the metric name for the synthetic scrape duration
|
|
|
|
// variable.
|
|
|
|
scrapeDurationMetricName clientmodel.LabelValue = "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 (
|
2015-03-14 19:36:15 -07:00
|
|
|
errIngestChannelFull = errors.New("ingestion channel full")
|
|
|
|
|
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},
|
|
|
|
)
|
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)
|
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:
|
2013-03-21 03:52:42 -07:00
|
|
|
return "UNKNOWN"
|
2015-05-18 04:14:41 -07:00
|
|
|
case HealthGood:
|
2015-03-07 12:50:02 -08:00
|
|
|
return "HEALTHY"
|
2015-05-18 04:14:41 -07:00
|
|
|
case HealthBad:
|
2015-03-07 12:50:02 -08:00
|
|
|
return "UNHEALTHY"
|
2013-03-21 03:52:42 -07:00
|
|
|
}
|
|
|
|
panic("unknown state")
|
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
const (
|
2015-03-07 12:50:02 -08:00
|
|
|
// Unknown is the state of a Target before it is first scraped.
|
2015-05-18 04:14:41 -07:00
|
|
|
HealthUnknown TargetHealth = iota
|
2015-03-07 12:50:02 -08:00
|
|
|
// Healthy is the state of a Target that has been successfully scraped.
|
2015-05-18 04:14:41 -07:00
|
|
|
HealthGood
|
2015-03-07 12:50:02 -08:00
|
|
|
// Unhealthy 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()
|
|
|
|
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 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()
|
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()
|
|
|
|
ts.lastScrape = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TargetStatus) setLastError(err error) {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
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
|
|
|
|
// The HTTP client used to scrape the target's endpoint.
|
|
|
|
httpClient *http.Client
|
|
|
|
|
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{}
|
2015-03-14 19:36:15 -07:00
|
|
|
// Channel to buffer ingested samples.
|
|
|
|
ingestedSamples chan clientmodel.Samples
|
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
|
2015-05-18 04:14:41 -07:00
|
|
|
// url is the URL to be scraped. Its host is immutable.
|
2015-04-20 03:24:25 -07:00
|
|
|
url *url.URL
|
2015-06-05 13:42:39 -07:00
|
|
|
// Labels before any processing.
|
|
|
|
metaLabels clientmodel.LabelSet
|
2015-04-20 03:24:25 -07:00
|
|
|
// Any base labels that are added to this target and its metrics.
|
|
|
|
baseLabels clientmodel.LabelSet
|
|
|
|
// What is the deadline for the HTTP or HTTPS against this endpoint.
|
|
|
|
deadline time.Duration
|
|
|
|
// The time between two scrapes.
|
|
|
|
scrapeInterval time.Duration
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewTarget creates a reasonably configured target for querying.
|
2015-06-05 13:42:39 -07:00
|
|
|
func NewTarget(cfg *config.ScrapeConfig, baseLabels, metaLabels clientmodel.LabelSet) *Target {
|
2015-05-18 04:14:41 -07:00
|
|
|
t := &Target{
|
2015-04-20 03:24:25 -07:00
|
|
|
url: &url.URL{
|
2015-04-28 15:08:58 -07:00
|
|
|
Host: string(baseLabels[clientmodel.AddressLabel]),
|
2015-04-20 03:24:25 -07:00
|
|
|
},
|
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
|
|
|
}
|
2015-06-05 13:42:39 -07:00
|
|
|
t.Update(cfg, baseLabels, metaLabels)
|
2015-03-14 19:36:15 -07:00
|
|
|
return t
|
|
|
|
}
|
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.
|
2015-06-05 13:42:39 -07:00
|
|
|
func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels clientmodel.LabelSet) {
|
2015-04-20 03:24:25 -07:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2015-05-07 01:55:03 -07:00
|
|
|
t.url.Scheme = cfg.Scheme
|
2015-04-25 03:59:05 -07:00
|
|
|
t.url.Path = string(baseLabels[clientmodel.MetricsPathLabel])
|
2015-05-15 03:47:50 -07:00
|
|
|
if cfg.BasicAuth != nil {
|
|
|
|
t.url.User = url.UserPassword(cfg.BasicAuth.Username, cfg.BasicAuth.Password)
|
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2015-05-07 01:55:03 -07:00
|
|
|
t.scrapeInterval = time.Duration(cfg.ScrapeInterval)
|
|
|
|
t.deadline = time.Duration(cfg.ScrapeTimeout)
|
2015-05-28 12:11:48 -07:00
|
|
|
t.httpClient = httputil.NewDeadlineClient(time.Duration(cfg.ScrapeTimeout))
|
2015-04-20 03:24:25 -07:00
|
|
|
|
2015-06-05 13:42:39 -07:00
|
|
|
t.metaLabels = metaLabels
|
2015-04-28 15:08:58 -07:00
|
|
|
t.baseLabels = clientmodel.LabelSet{}
|
2015-04-25 03:59:05 -07:00
|
|
|
// All remaining internal labels will not be part of the label set.
|
2015-04-20 03:24:25 -07:00
|
|
|
for name, val := range baseLabels {
|
2015-04-25 03:59:05 -07:00
|
|
|
if !strings.HasPrefix(string(name), clientmodel.ReservedLabelPrefix) {
|
|
|
|
t.baseLabels[name] = val
|
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
2015-04-28 15:08:58 -07:00
|
|
|
if _, ok := t.baseLabels[clientmodel.InstanceLabel]; !ok {
|
|
|
|
t.baseLabels[clientmodel.InstanceLabel] = clientmodel.LabelValue(t.InstanceIdentifier())
|
|
|
|
}
|
2015-04-20 03:24:25 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) String() string {
|
2015-04-20 03:24:25 -07:00
|
|
|
return t.url.Host
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// Ingest implements an extraction.Ingester.
|
|
|
|
func (t *Target) Ingest(s clientmodel.Samples) error {
|
|
|
|
t.RLock()
|
|
|
|
deadline := t.deadline
|
|
|
|
t.RUnlock()
|
2015-03-14 19:36:15 -07:00
|
|
|
// 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
|
2015-05-18 04:14:41 -07:00
|
|
|
case <-time.After(deadline / 10):
|
2015-03-14 19:36:15 -07:00
|
|
|
return errIngestChannelFull
|
|
|
|
}
|
2014-12-26 04:37:30 -08:00
|
|
|
}
|
2013-04-16 08:22:10 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// Ensure that Target implements extraction.Ingester at compile time.
|
|
|
|
var _ extraction.Ingester = (*Target)(nil)
|
|
|
|
|
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
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
t.RLock()
|
|
|
|
lastScrapeInterval := t.scrapeInterval
|
|
|
|
t.RUnlock()
|
|
|
|
|
2015-05-20 09:10:29 -07:00
|
|
|
log.Debugf("Starting scraper for target %v...", t)
|
2015-04-20 03:24:25 -07:00
|
|
|
|
|
|
|
jitterTimer := time.NewTimer(time.Duration(float64(lastScrapeInterval) * rand.Float64()))
|
2014-07-29 11:31:11 -07:00
|
|
|
select {
|
|
|
|
case <-jitterTimer.C:
|
2014-12-03 09:07:23 -08:00
|
|
|
case <-t.scraperStopping:
|
2014-11-11 15:38:28 -08:00
|
|
|
jitterTimer.Stop()
|
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
|
|
|
jitterTimer.Stop()
|
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
ticker := time.NewTicker(lastScrapeInterval)
|
2014-07-29 11:31:11 -07:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
t.status.setLastScrape(time.Now())
|
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())
|
|
|
|
t.status.setLastScrape(time.Now())
|
2015-04-20 03:24:25 -07:00
|
|
|
|
|
|
|
intervalStr := lastScrapeInterval.String()
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
t.RLock()
|
2015-04-20 03:24:25 -07:00
|
|
|
// On changed scrape interval the new interval becomes effective
|
|
|
|
// after the next scrape.
|
|
|
|
if lastScrapeInterval != t.scrapeInterval {
|
2015-05-12 07:52:56 -07:00
|
|
|
ticker.Stop()
|
2015-04-20 03:24:25 -07:00
|
|
|
ticker = time.NewTicker(t.scrapeInterval)
|
|
|
|
lastScrapeInterval = t.scrapeInterval
|
|
|
|
}
|
2015-05-18 04:14:41 -07:00
|
|
|
t.RUnlock()
|
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.
|
|
|
|
)
|
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
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) scrape(sampleAppender storage.SampleAppender) (err error) {
|
2015-05-18 02:13:13 -07:00
|
|
|
start := time.Now()
|
2015-05-18 04:14:41 -07:00
|
|
|
baseLabels := t.BaseLabels()
|
2015-04-18 23:39:33 -07:00
|
|
|
|
2015-05-18 02:13:13 -07:00
|
|
|
defer func() {
|
|
|
|
t.status.setLastError(err)
|
2015-05-18 04:14:41 -07:00
|
|
|
recordScrapeHealth(sampleAppender, clientmodel.TimestampFromTime(start), baseLabels, t.status.Health(), time.Since(start))
|
2015-05-18 02:13:13 -07:00
|
|
|
}()
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
req, err := http.NewRequest("GET", t.URL(), nil)
|
2013-06-27 09:32:05 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", acceptHeader)
|
|
|
|
|
2013-08-09 10:32:55 -07:00
|
|
|
resp, err := t.httpClient.Do(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
|
|
|
}
|
2014-07-26 14:41:05 -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
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
processor, err := extraction.ProcessorForRequestHeader(resp.Header)
|
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
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2015-03-14 19:36:15 -07:00
|
|
|
t.ingestedSamples = make(chan clientmodel.Samples, ingestedSamplesCap)
|
2013-08-10 08:02:28 -07:00
|
|
|
|
2013-08-12 06:15:41 -07:00
|
|
|
processOptions := &extraction.ProcessOptions{
|
2015-05-18 02:13:13 -07:00
|
|
|
Timestamp: clientmodel.TimestampFromTime(start),
|
2013-08-12 06:15:41 -07:00
|
|
|
}
|
2015-03-14 19:36:15 -07:00
|
|
|
go func() {
|
|
|
|
err = processor.ProcessSingle(resp.Body, t, processOptions)
|
|
|
|
close(t.ingestedSamples)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for samples := range t.ingestedSamples {
|
|
|
|
for _, s := range samples {
|
2015-05-18 04:14:41 -07:00
|
|
|
s.Metric.MergeFromLabelSet(baseLabels, clientmodel.ExporterLabelPrefix)
|
2015-03-14 19:36:15 -07:00
|
|
|
sampleAppender.Append(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
2013-01-12 12:22:59 -08:00
|
|
|
|
2014-12-17 10:40:59 -08:00
|
|
|
// URL implements Target.
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) URL() string {
|
2015-04-20 03:24:25 -07:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
return t.url.String()
|
2013-01-13 01:46:55 -08:00
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// InstanceIdentifier returns the identifier for the target.
|
|
|
|
func (t *Target) InstanceIdentifier() string {
|
2015-04-20 03:24:25 -07:00
|
|
|
return t.url.Host
|
2015-02-19 09:58:47 -08:00
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// fullLabels returns the base labels plus internal labels defining the target.
|
|
|
|
func (t *Target) fullLabels() clientmodel.LabelSet {
|
2015-04-25 03:59:05 -07:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-05-18 03:16:25 -07:00
|
|
|
lset := make(clientmodel.LabelSet, len(t.baseLabels)+2)
|
2015-04-25 03:59:05 -07:00
|
|
|
for ln, lv := range t.baseLabels {
|
2015-05-18 03:16:25 -07:00
|
|
|
lset[ln] = lv
|
2015-04-25 03:59:05 -07:00
|
|
|
}
|
2015-05-18 03:16:25 -07:00
|
|
|
lset[clientmodel.MetricsPathLabel] = clientmodel.LabelValue(t.url.Path)
|
|
|
|
lset[clientmodel.AddressLabel] = clientmodel.LabelValue(t.url.Host)
|
|
|
|
return lset
|
2015-04-25 03:59:05 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// BaseLabels returns a copy of the target's base labels.
|
|
|
|
func (t *Target) BaseLabels() clientmodel.LabelSet {
|
2015-04-18 23:39:33 -07:00
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
2015-05-18 03:16:25 -07:00
|
|
|
lset := make(clientmodel.LabelSet, len(t.baseLabels))
|
2015-04-25 03:59:05 -07:00
|
|
|
for ln, lv := range t.baseLabels {
|
2015-05-18 03:16:25 -07:00
|
|
|
lset[ln] = lv
|
2015-03-18 10:53:43 -07: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.
|
|
|
|
func (t *Target) MetaLabels() clientmodel.LabelSet {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
lset := make(clientmodel.LabelSet, len(t.metaLabels))
|
|
|
|
for ln, lv := range t.metaLabels {
|
|
|
|
lset[ln] = lv
|
|
|
|
}
|
|
|
|
return lset
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
func recordScrapeHealth(
|
|
|
|
sampleAppender storage.SampleAppender,
|
|
|
|
timestamp clientmodel.Timestamp,
|
|
|
|
baseLabels clientmodel.LabelSet,
|
|
|
|
health TargetHealth,
|
|
|
|
scrapeDuration time.Duration,
|
|
|
|
) {
|
|
|
|
healthMetric := make(clientmodel.Metric, len(baseLabels)+1)
|
|
|
|
durationMetric := make(clientmodel.Metric, len(baseLabels)+1)
|
2015-05-18 03:16:25 -07:00
|
|
|
|
|
|
|
healthMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeHealthMetricName)
|
|
|
|
durationMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeDurationMetricName)
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
for label, value := range baseLabels {
|
2015-03-14 19:36:15 -07:00
|
|
|
healthMetric[label] = value
|
|
|
|
durationMetric[label] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
healthValue := clientmodel.SampleValue(0)
|
2015-05-18 04:14:41 -07:00
|
|
|
if health == HealthGood {
|
2015-03-14 19:36:15 -07:00
|
|
|
healthValue = clientmodel.SampleValue(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
healthSample := &clientmodel.Sample{
|
|
|
|
Metric: healthMetric,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Value: healthValue,
|
|
|
|
}
|
|
|
|
durationSample := &clientmodel.Sample{
|
|
|
|
Metric: durationMetric,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Value: clientmodel.SampleValue(float64(scrapeDuration) / float64(time.Second)),
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleAppender.Append(healthSample)
|
|
|
|
sampleAppender.Append(durationSample)
|
|
|
|
}
|