2013-01-04 11:17:31 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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 (
|
2013-01-04 13:41:47 +00:00
|
|
|
"fmt"
|
2014-07-29 18:31:11 +00:00
|
|
|
"math/rand"
|
2013-01-04 13:41:47 +00:00
|
|
|
"net/http"
|
2013-04-10 12:26:07 +00:00
|
|
|
"os"
|
|
|
|
"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
|
|
|
|
2013-08-12 15:18:02 +00:00
|
|
|
"github.com/golang/glog"
|
2013-06-25 12:02:27 +00:00
|
|
|
"github.com/prometheus/client_golang/extraction"
|
2014-06-18 17:43:15 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2013-07-30 15:18:07 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/utility"
|
2012-12-25 12:50:36 +00:00
|
|
|
)
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
const (
|
2014-12-10 15:16:49 +00:00
|
|
|
// InstanceLabel is the label value used for the instance label.
|
2013-06-25 12:02:27 +00:00
|
|
|
InstanceLabel clientmodel.LabelName = "instance"
|
2014-12-10 15:16:49 +00:00
|
|
|
// ScrapeHealthMetricName is the metric name for the synthetic health
|
|
|
|
// variable.
|
2014-12-26 12:37:30 +00:00
|
|
|
scrapeHealthMetricName clientmodel.LabelValue = "up"
|
|
|
|
// ScrapeTimeMetricName is the metric name for the synthetic scrape duration
|
|
|
|
// variable.
|
|
|
|
scrapeDurationMetricName clientmodel.LabelValue = "scrape_duration_seconds"
|
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 (
|
|
|
|
localhostRepresentations = []string{"http://127.0.0.1", "http://localhost"}
|
|
|
|
|
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.",
|
|
|
|
Objectives: []float64{0.01, 0.05, 0.5, 0.90, 0.99},
|
|
|
|
},
|
|
|
|
[]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
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// TargetState describes the state of a Target.
|
2012-12-25 12:50:36 +00:00
|
|
|
type TargetState int
|
|
|
|
|
2013-03-21 10:52:42 +00:00
|
|
|
func (t TargetState) String() string {
|
|
|
|
switch t {
|
2014-12-10 15:16:49 +00:00
|
|
|
case Unknown:
|
2013-03-21 10:52:42 +00:00
|
|
|
return "UNKNOWN"
|
2014-12-10 15:16:49 +00:00
|
|
|
case Alive:
|
2013-03-21 10:52:42 +00:00
|
|
|
return "ALIVE"
|
2014-12-10 15:16:49 +00:00
|
|
|
case Unreachable:
|
2013-03-21 10:52:42 +00:00
|
|
|
return "UNREACHABLE"
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unknown state")
|
|
|
|
}
|
|
|
|
|
2012-12-25 12:50:36 +00:00
|
|
|
const (
|
2014-12-10 15:16:49 +00:00
|
|
|
// Unknown is the state of a Target that has not been seen; we know
|
|
|
|
// nothing about it, except that it is on our docket for examination.
|
|
|
|
Unknown TargetState = iota
|
|
|
|
// Alive is the state of a Target that has been found and successfully
|
|
|
|
// queried.
|
|
|
|
Alive
|
|
|
|
// Unreachable is the state of a Target that was either historically
|
|
|
|
// found or not found and then determined to be unhealthy by either not
|
|
|
|
// responding or disappearing.
|
|
|
|
Unreachable
|
2012-12-25 12:50:36 +00:00
|
|
|
)
|
|
|
|
|
2013-01-13 09:46:55 +00:00
|
|
|
// A Target represents an endpoint that should be interrogated for metrics.
|
|
|
|
//
|
|
|
|
// The protocol described by this type will likely change in future iterations,
|
|
|
|
// as it offers no good support for aggregated targets and fan out. Thusly,
|
|
|
|
// it is likely that the current Target and target uses will be
|
|
|
|
// wrapped with some resolver type.
|
|
|
|
//
|
|
|
|
// For the future, the Target protocol will abstract away the exact means that
|
|
|
|
// metrics are retrieved and deserialized from the given instance to which it
|
|
|
|
// refers.
|
|
|
|
type Target interface {
|
2013-05-21 13:31:27 +00:00
|
|
|
// Return the last encountered scrape error, if any.
|
|
|
|
LastError() error
|
2014-07-29 16:28:48 +00:00
|
|
|
// Return the health of the target.
|
|
|
|
State() TargetState
|
|
|
|
// Return the last time a scrape was attempted.
|
|
|
|
LastScrape() time.Time
|
2014-12-17 18:40:59 +00:00
|
|
|
// The URL to which the Target corresponds. Out of all of the available
|
2013-01-13 09:46:55 +00:00
|
|
|
// points in this interface, this one is the best candidate to change given
|
|
|
|
// the ways to express the endpoint.
|
2014-12-17 18:40:59 +00:00
|
|
|
URL() string
|
|
|
|
// The URL as seen from other hosts. References to localhost are resolved
|
2013-04-10 12:26:07 +00:00
|
|
|
// to the address of the prometheus server.
|
2014-12-17 18:40:59 +00:00
|
|
|
GlobalURL() string
|
2013-02-22 20:07:35 +00:00
|
|
|
// Return the target's base labels.
|
2013-06-25 12:02:27 +00:00
|
|
|
BaseLabels() clientmodel.LabelSet
|
2014-10-09 13:59:47 +00:00
|
|
|
// SetBaseLabelsFrom queues a replacement of the current base labels by
|
|
|
|
// the labels of the given target. The method returns immediately after
|
|
|
|
// queuing. The actual replacement of the base labels happens
|
|
|
|
// asynchronously (but most likely before the next scrape for the target
|
|
|
|
// begins).
|
|
|
|
SetBaseLabelsFrom(Target)
|
2014-07-29 18:31:11 +00:00
|
|
|
// Scrape target at the specified interval.
|
|
|
|
RunScraper(extraction.Ingester, time.Duration)
|
|
|
|
// Stop scraping, synchronous.
|
|
|
|
StopScraper()
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2013-01-13 09:46:55 +00:00
|
|
|
// target is a Target that refers to a singular HTTP or HTTPS endpoint.
|
|
|
|
type target struct {
|
2013-05-21 13:31:27 +00:00
|
|
|
// The current health state of the target.
|
|
|
|
state TargetState
|
|
|
|
// The last encountered scrape error, if any.
|
|
|
|
lastError error
|
2014-07-29 16:28:48 +00:00
|
|
|
// The last time a scrape was attempted.
|
|
|
|
lastScrape time.Time
|
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{}
|
2014-10-09 13:59:47 +00:00
|
|
|
// Channel to queue base labels to be replaced.
|
|
|
|
newBaseLabels chan clientmodel.LabelSet
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
url string
|
2013-01-13 09:46:55 +00:00
|
|
|
// What is the deadline for the HTTP or HTTPS against this endpoint.
|
|
|
|
Deadline time.Duration
|
|
|
|
// Any base labels that are added to this target and its metrics.
|
2013-06-25 12:02:27 +00:00
|
|
|
baseLabels clientmodel.LabelSet
|
2013-05-21 13:31:27 +00:00
|
|
|
// The HTTP client used to scrape the target's endpoint.
|
2013-08-09 17:32:55 +00:00
|
|
|
httpClient *http.Client
|
2014-12-03 17:07:23 +00:00
|
|
|
|
|
|
|
// Mutex protects lastError, lastScrape, state, and baseLabels. Writing
|
|
|
|
// the above must only happen in the goroutine running the RunScraper
|
|
|
|
// loop, and it must happen under the lock. In that way, no mutex lock
|
|
|
|
// is required for reading the above in the goroutine running the
|
|
|
|
// RunScraper loop, but only for reading in other goroutines.
|
|
|
|
sync.Mutex
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// NewTarget creates a reasonably configured target for querying.
|
2014-12-17 18:40:59 +00:00
|
|
|
func NewTarget(url string, deadline time.Duration, baseLabels clientmodel.LabelSet) Target {
|
2013-01-13 09:46:55 +00:00
|
|
|
target := &target{
|
2014-12-17 18:40:59 +00:00
|
|
|
url: url,
|
2014-12-03 17:07:23 +00:00
|
|
|
Deadline: deadline,
|
|
|
|
baseLabels: baseLabels,
|
|
|
|
httpClient: utility.NewDeadlineClient(deadline),
|
|
|
|
scraperStopping: make(chan struct{}),
|
|
|
|
scraperStopped: make(chan struct{}),
|
|
|
|
newBaseLabels: make(chan clientmodel.LabelSet, 1),
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2012-12-25 12:50:36 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
return target
|
|
|
|
}
|
2012-12-25 12:50:36 +00:00
|
|
|
|
2014-12-26 12:37:30 +00:00
|
|
|
func (t *target) recordScrapeHealth(ingester extraction.Ingester, timestamp clientmodel.Timestamp, healthy bool, scrapeDuration time.Duration) {
|
|
|
|
healthMetric := clientmodel.Metric{}
|
|
|
|
durationMetric := clientmodel.Metric{}
|
2013-04-16 15:22:10 +00:00
|
|
|
for label, value := range t.baseLabels {
|
2014-12-26 12:37:30 +00:00
|
|
|
healthMetric[label] = value
|
|
|
|
durationMetric[label] = value
|
2013-04-16 15:22:10 +00:00
|
|
|
}
|
2014-12-26 12:37:30 +00:00
|
|
|
healthMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeHealthMetricName)
|
|
|
|
durationMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeDurationMetricName)
|
|
|
|
healthMetric[InstanceLabel] = clientmodel.LabelValue(t.URL())
|
|
|
|
durationMetric[InstanceLabel] = clientmodel.LabelValue(t.URL())
|
2013-04-16 15:22:10 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
healthValue := clientmodel.SampleValue(0)
|
2013-04-16 15:22:10 +00:00
|
|
|
if healthy {
|
2013-06-25 12:02:27 +00:00
|
|
|
healthValue = clientmodel.SampleValue(1)
|
2013-04-16 15:22:10 +00:00
|
|
|
}
|
|
|
|
|
2014-12-26 12:37:30 +00:00
|
|
|
healthSample := &clientmodel.Sample{
|
|
|
|
Metric: healthMetric,
|
2013-04-16 15:22:10 +00:00
|
|
|
Timestamp: timestamp,
|
|
|
|
Value: healthValue,
|
|
|
|
}
|
2014-12-26 12:37:30 +00:00
|
|
|
durationSample := &clientmodel.Sample{
|
|
|
|
Metric: durationMetric,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Value: clientmodel.SampleValue(float64(scrapeDuration) / float64(time.Second)),
|
|
|
|
}
|
2013-04-16 15:22:10 +00:00
|
|
|
|
2014-12-31 13:01:19 +00:00
|
|
|
ingester.Ingest(clientmodel.Samples{healthSample, durationSample})
|
2013-04-16 15:22:10 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// RunScraper implements Target.
|
2014-07-29 18:31:11 +00:00
|
|
|
func (t *target) RunScraper(ingester extraction.Ingester, interval time.Duration) {
|
2014-11-11 15:41:20 +00:00
|
|
|
defer func() {
|
|
|
|
// Need to drain t.newBaseLabels to not make senders block during shutdown.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.newBaseLabels:
|
|
|
|
// Do nothing.
|
|
|
|
default:
|
2014-12-03 17:07:23 +00:00
|
|
|
close(t.scraperStopped)
|
2014-11-11 15:41:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-07-29 18:31:11 +00:00
|
|
|
jitterTimer := time.NewTimer(time.Duration(float64(interval) * rand.Float64()))
|
|
|
|
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()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock() // Writing t.lastScrape requires the lock.
|
2014-07-29 16:28:48 +00:00
|
|
|
t.lastScrape = time.Now()
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Unlock()
|
2014-07-29 18:31:11 +00:00
|
|
|
t.scrape(ingester)
|
2014-11-11 23:38:28 +00:00
|
|
|
|
|
|
|
// Explanation of the contraption below:
|
|
|
|
//
|
2014-12-03 17:07:23 +00:00
|
|
|
// In case t.newBaseLabels or t.scraperStopping have something to receive,
|
2014-11-11 23:38:28 +00:00
|
|
|
// we want to read from those channels rather than starting a new scrape
|
|
|
|
// (which might take very long). That's why the outer select has no
|
2014-12-03 17:07:23 +00:00
|
|
|
// ticker.C. Should neither t.newBaseLabels nor t.scraperStopping have
|
2014-11-11 23:38:28 +00:00
|
|
|
// 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-10-09 13:59:47 +00:00
|
|
|
case newBaseLabels := <-t.newBaseLabels:
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock() // Writing t.baseLabels requires the lock.
|
2014-10-09 13:59:47 +00:00
|
|
|
t.baseLabels = newBaseLabels
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Unlock()
|
|
|
|
case <-t.scraperStopping:
|
2014-07-29 18:31:11 +00:00
|
|
|
return
|
2014-11-11 23:38:28 +00:00
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case newBaseLabels := <-t.newBaseLabels:
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock() // Writing t.baseLabels requires the lock.
|
2014-11-11 23:38:28 +00:00
|
|
|
t.baseLabels = newBaseLabels
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Unlock()
|
|
|
|
case <-t.scraperStopping:
|
2014-11-11 23:38:28 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
targetIntervalLength.WithLabelValues(interval.String()).Observe(float64(time.Since(t.lastScrape) / time.Second))
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock() // Write t.lastScrape requires locking.
|
2014-11-11 23:38:28 +00:00
|
|
|
t.lastScrape = time.Now()
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Unlock()
|
2014-11-11 23:38:28 +00:00
|
|
|
t.scrape(ingester)
|
|
|
|
}
|
2014-07-29 18:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// StopScraper implements Target.
|
2014-07-29 18:31:11 +00:00
|
|
|
func (t *target) StopScraper() {
|
2014-12-03 17:07:23 +00:00
|
|
|
close(t.scraperStopping)
|
|
|
|
<-t.scraperStopped
|
2013-04-26 15:26:52 +00:00
|
|
|
}
|
2013-01-22 17:37:01 +00:00
|
|
|
|
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
|
|
|
|
2014-07-29 18:31:11 +00:00
|
|
|
func (t *target) scrape(ingester extraction.Ingester) (err error) {
|
|
|
|
timestamp := clientmodel.Now()
|
2013-04-26 15:26:52 +00:00
|
|
|
defer func(start time.Time) {
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock() // Writing t.state and t.lastError requires the lock.
|
2014-07-29 18:31:11 +00:00
|
|
|
if err == nil {
|
2014-12-10 15:16:49 +00:00
|
|
|
t.state = Alive
|
2014-07-29 18:31:11 +00:00
|
|
|
} else {
|
2014-12-10 15:16:49 +00:00
|
|
|
t.state = Unreachable
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
2014-07-29 18:31:11 +00:00
|
|
|
t.lastError = err
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Unlock()
|
2014-12-26 12:37:30 +00:00
|
|
|
t.recordScrapeHealth(ingester, timestamp, err == nil, time.Since(start))
|
2013-04-26 15:26:52 +00:00
|
|
|
}(time.Now())
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
req, err := http.NewRequest("GET", t.URL(), nil)
|
2013-06-27 16:32:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", acceptHeader)
|
|
|
|
|
2013-08-09 17:32:55 +00:00
|
|
|
resp, err := t.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-07-26 21:41:05 +00:00
|
|
|
defer resp.Body.Close()
|
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
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
processor, err := extraction.ProcessorForRequestHeader(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
|
|
|
}
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
baseLabels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.URL())}
|
2013-04-26 15:26:52 +00:00
|
|
|
for baseLabel, baseValue := range t.baseLabels {
|
|
|
|
baseLabels[baseLabel] = baseValue
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 14:14:18 +00:00
|
|
|
i := &MergeLabelsIngester{
|
|
|
|
Labels: baseLabels,
|
|
|
|
CollisionPrefix: clientmodel.ExporterLabelPrefix,
|
2013-08-10 15:02:28 +00:00
|
|
|
|
2013-08-13 14:14:18 +00:00
|
|
|
Ingester: ingester,
|
2013-08-12 13:15:41 +00:00
|
|
|
}
|
|
|
|
processOptions := &extraction.ProcessOptions{
|
2013-08-10 15:02:28 +00:00
|
|
|
Timestamp: timestamp,
|
2013-08-12 13:15:41 +00:00
|
|
|
}
|
2014-07-02 18:43:15 +00:00
|
|
|
return processor.ProcessSingle(resp.Body, i, processOptions)
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
2013-01-12 20:22:59 +00:00
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// LastError implements Target.
|
2014-07-29 16:28:48 +00:00
|
|
|
func (t *target) LastError() error {
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-07-29 16:28:48 +00:00
|
|
|
return t.lastError
|
2013-01-12 20:22:59 +00:00
|
|
|
}
|
2013-01-13 09:46:55 +00:00
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// State implements Target.
|
2014-07-29 16:28:48 +00:00
|
|
|
func (t *target) State() TargetState {
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-07-29 16:28:48 +00:00
|
|
|
return t.state
|
2013-07-15 13:11:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// LastScrape implements Target.
|
2014-07-29 16:28:48 +00:00
|
|
|
func (t *target) LastScrape() time.Time {
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2014-07-29 16:28:48 +00:00
|
|
|
return t.lastScrape
|
2013-05-21 13:31:27 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
// URL implements Target.
|
|
|
|
func (t *target) URL() string {
|
|
|
|
return t.url
|
2013-01-13 09:46:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:40:59 +00:00
|
|
|
// GlobalURL implements Target.
|
|
|
|
func (t *target) GlobalURL() string {
|
|
|
|
url := t.url
|
2013-04-10 12:26:07 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2014-12-17 18:40:59 +00:00
|
|
|
glog.Warningf("Couldn't get hostname: %s, returning target.URL()", err)
|
|
|
|
return url
|
2013-04-10 12:26:07 +00:00
|
|
|
}
|
|
|
|
for _, localhostRepresentation := range localhostRepresentations {
|
2014-12-17 18:40:59 +00:00
|
|
|
url = strings.Replace(url, localhostRepresentation, fmt.Sprintf("http://%s", hostname), -1)
|
2013-04-10 12:26:07 +00:00
|
|
|
}
|
2014-12-17 18:40:59 +00:00
|
|
|
return url
|
2013-04-10 12:26:07 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// BaseLabels implements Target.
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) BaseLabels() clientmodel.LabelSet {
|
2014-12-03 17:07:23 +00:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
2013-02-22 20:07:35 +00:00
|
|
|
return t.baseLabels
|
|
|
|
}
|
|
|
|
|
2014-10-09 13:59:47 +00:00
|
|
|
// SetBaseLabelsFrom implements Target.
|
|
|
|
func (t *target) SetBaseLabelsFrom(newTarget Target) {
|
2014-12-17 18:40:59 +00:00
|
|
|
if t.URL() != newTarget.URL() {
|
2013-02-22 20:07:35 +00:00
|
|
|
panic("targets don't refer to the same endpoint")
|
|
|
|
}
|
2014-10-09 13:59:47 +00:00
|
|
|
t.newBaseLabels <- newTarget.BaseLabels()
|
2013-01-13 09:46:55 +00:00
|
|
|
}
|