2013-01-04 03:17:31 -08: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.
|
2012-12-25 04:50:36 -08:00
|
|
|
package retrieval
|
|
|
|
|
|
|
|
import (
|
2013-01-04 05:41:47 -08:00
|
|
|
"fmt"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
2013-04-10 05:26:07 -07:00
|
|
|
"log"
|
2013-01-04 05:41:47 -08:00
|
|
|
"net/http"
|
2013-04-10 05:26:07 -07:00
|
|
|
"os"
|
|
|
|
"strings"
|
2012-12-25 04:50:36 -08:00
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/extraction"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2012-12-25 04:50:36 -08:00
|
|
|
)
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
const (
|
|
|
|
InstanceLabel clientmodel.LabelName = "instance"
|
|
|
|
// The metric name for the synthetic health variable.
|
|
|
|
ScrapeHealthMetricName clientmodel.LabelValue = "up"
|
2013-04-10 05:26:07 -07:00
|
|
|
)
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
var localhostRepresentations = []string{"http://127.0.0.1", "http://localhost"}
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
// The state of the given Target.
|
2012-12-25 04:50:36 -08:00
|
|
|
type TargetState int
|
|
|
|
|
2013-03-21 03:52:42 -07:00
|
|
|
func (t TargetState) String() string {
|
|
|
|
switch t {
|
|
|
|
case UNKNOWN:
|
|
|
|
return "UNKNOWN"
|
|
|
|
case ALIVE:
|
|
|
|
return "ALIVE"
|
|
|
|
case UNREACHABLE:
|
|
|
|
return "UNREACHABLE"
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unknown state")
|
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
const (
|
2013-01-13 01:46:55 -08:00
|
|
|
// The Target has not been seen; we know nothing about it, except that it is
|
|
|
|
// on our docket for examination.
|
2012-12-25 04:50:36 -08:00
|
|
|
UNKNOWN TargetState = iota
|
2013-01-13 01:46:55 -08:00
|
|
|
// The Target has been found and successfully queried.
|
2012-12-25 04:50:36 -08:00
|
|
|
ALIVE
|
2013-01-13 01:46:55 -08:00
|
|
|
// The Target was either historically found or not found and then determined
|
|
|
|
// to be unhealthy by either not responding or disappearing.
|
2012-12-25 04:50:36 -08:00
|
|
|
UNREACHABLE
|
|
|
|
)
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
// A healthReporter is a type that can provide insight into its health state.
|
|
|
|
//
|
|
|
|
// It mainly exists for testability reasons to decouple the scheduler behaviors
|
|
|
|
// from fully-fledged Target and other types.
|
2013-01-12 12:22:59 -08:00
|
|
|
type healthReporter interface {
|
2013-01-13 01:46:55 -08:00
|
|
|
// Report the last-known health state for this target.
|
|
|
|
State() TargetState
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// Retrieve values from this target.
|
|
|
|
//
|
|
|
|
// earliest refers to the soonest available opportunity to reschedule the
|
|
|
|
// target for a future retrieval. It is up to the underlying scheduler type,
|
|
|
|
// alluded to in the scheduledFor function, to use this as it wants to. The
|
|
|
|
// current use case is to create a common batching time for scraping multiple
|
|
|
|
// Targets in the future through the TargetPool.
|
2013-06-25 05:02:27 -07:00
|
|
|
Scrape(earliest time.Time, results chan<- *extraction.Result) error
|
2013-01-13 01:46:55 -08:00
|
|
|
// Fulfill the healthReporter interface.
|
2013-01-12 12:22:59 -08:00
|
|
|
State() TargetState
|
2013-01-13 01:46:55 -08:00
|
|
|
// Report the soonest time at which this Target may be scheduled for
|
|
|
|
// retrieval. This value needn't convey that the operation occurs at this
|
|
|
|
// time, but it should occur no sooner than it.
|
|
|
|
//
|
|
|
|
// Right now, this is used as the sorting key in TargetPool.
|
|
|
|
scheduledFor() time.Time
|
2013-05-21 06:31:27 -07:00
|
|
|
// Return the last encountered scrape error, if any.
|
|
|
|
LastError() error
|
2013-01-13 01:46:55 -08:00
|
|
|
// The address to which the Target corresponds. Out of all of the available
|
|
|
|
// points in this interface, this one is the best candidate to change given
|
|
|
|
// the ways to express the endpoint.
|
|
|
|
Address() string
|
2013-04-10 05:26:07 -07:00
|
|
|
// The address as seen from other hosts. References to localhost are resolved
|
|
|
|
// to the address of the prometheus server.
|
|
|
|
GlobalAddress() string
|
2013-02-22 12:07:35 -08:00
|
|
|
// Return the target's base labels.
|
2013-06-25 05:02:27 -07:00
|
|
|
BaseLabels() clientmodel.LabelSet
|
2013-02-22 12:07:35 -08:00
|
|
|
// Merge a new externally supplied target definition (e.g. with changed base
|
|
|
|
// labels) into an old target definition for the same endpoint. Preserve
|
|
|
|
// remaining information - like health state - from the old target.
|
|
|
|
Merge(newTarget Target)
|
2013-01-12 12:22:59 -08:00
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
// target is a Target that refers to a singular HTTP or HTTPS endpoint.
|
|
|
|
type target struct {
|
|
|
|
// scheduler provides the scheduling strategy that is used to formulate what
|
|
|
|
// is returned in Target.scheduledFor.
|
2013-01-12 12:22:59 -08:00
|
|
|
scheduler scheduler
|
2013-05-21 06:31:27 -07:00
|
|
|
// The current health state of the target.
|
|
|
|
state TargetState
|
|
|
|
// The last encountered scrape error, if any.
|
|
|
|
lastError error
|
2013-01-04 03:17:31 -08:00
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
address string
|
|
|
|
// 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 05:02:27 -07:00
|
|
|
baseLabels clientmodel.LabelSet
|
2013-05-21 06:31:27 -07:00
|
|
|
// The HTTP client used to scrape the target's endpoint.
|
|
|
|
client http.Client
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
// Furnish a reasonably configured target for querying.
|
2013-06-25 05:02:27 -07:00
|
|
|
func NewTarget(address string, deadline time.Duration, baseLabels clientmodel.LabelSet) Target {
|
2013-01-13 01:46:55 -08:00
|
|
|
target := &target{
|
|
|
|
address: address,
|
2013-01-12 12:22:59 -08:00
|
|
|
Deadline: deadline,
|
2013-02-22 12:07:35 -08:00
|
|
|
baseLabels: baseLabels,
|
2013-04-26 08:26:52 -07:00
|
|
|
client: NewDeadlineClient(deadline),
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-12 12:22:59 -08:00
|
|
|
scheduler := &healthScheduler{
|
|
|
|
target: target,
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
2013-01-12 12:22:59 -08:00
|
|
|
target.scheduler = scheduler
|
|
|
|
|
|
|
|
return target
|
|
|
|
}
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *target) recordScrapeHealth(results chan<- *extraction.Result, timestamp time.Time, healthy bool) {
|
|
|
|
metric := clientmodel.Metric{}
|
2013-04-16 08:22:10 -07:00
|
|
|
for label, value := range t.baseLabels {
|
|
|
|
metric[label] = value
|
|
|
|
}
|
2013-06-25 05:02:27 -07:00
|
|
|
metric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(ScrapeHealthMetricName)
|
|
|
|
metric[InstanceLabel] = clientmodel.LabelValue(t.Address())
|
2013-04-16 08:22:10 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
healthValue := clientmodel.SampleValue(0)
|
2013-04-16 08:22:10 -07:00
|
|
|
if healthy {
|
2013-06-25 05:02:27 -07:00
|
|
|
healthValue = clientmodel.SampleValue(1)
|
2013-04-16 08:22:10 -07:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
sample := &clientmodel.Sample{
|
2013-04-16 08:22:10 -07:00
|
|
|
Metric: metric,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Value: healthValue,
|
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
results <- &extraction.Result{
|
2013-04-29 07:55:18 -07:00
|
|
|
Err: nil,
|
2013-06-25 05:02:27 -07:00
|
|
|
Samples: clientmodel.Samples{sample},
|
2013-04-16 08:22:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *target) Scrape(earliest time.Time, results chan<- *extraction.Result) (err error) {
|
2013-04-16 08:22:10 -07:00
|
|
|
now := time.Now()
|
2013-04-26 08:26:52 -07:00
|
|
|
futureState := t.state
|
|
|
|
|
|
|
|
if err = t.scrape(now, results); err != nil {
|
|
|
|
t.recordScrapeHealth(results, now, false)
|
|
|
|
futureState = UNREACHABLE
|
|
|
|
} else {
|
|
|
|
t.recordScrapeHealth(results, now, true)
|
|
|
|
futureState = ALIVE
|
|
|
|
}
|
2013-04-16 08:22:10 -07:00
|
|
|
|
2013-04-26 08:26:52 -07:00
|
|
|
t.scheduler.Reschedule(earliest, futureState)
|
|
|
|
t.state = futureState
|
2013-05-21 06:31:27 -07:00
|
|
|
t.lastError = err
|
2013-04-28 10:40:30 -07:00
|
|
|
|
2013-05-15 22:38:31 -07:00
|
|
|
return err
|
2013-04-26 08:26:52 -07:00
|
|
|
}
|
2013-01-22 09:37:01 -08:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *target) scrape(timestamp time.Time, results chan<- *extraction.Result) (err error) {
|
2013-04-26 08:26:52 -07:00
|
|
|
defer func(start time.Time) {
|
|
|
|
ms := float64(time.Since(start)) / float64(time.Millisecond)
|
|
|
|
labels := map[string]string{address: t.Address(), outcome: success}
|
2013-01-04 05:41:47 -08:00
|
|
|
if err != nil {
|
2013-04-26 08:26:52 -07:00
|
|
|
labels[outcome] = failure
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2013-04-26 08:26:52 -07:00
|
|
|
targetOperationLatencies.Add(labels, ms)
|
|
|
|
targetOperations.Increment(labels)
|
|
|
|
}(time.Now())
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2013-04-26 08:26:52 -07:00
|
|
|
resp, err := t.client.Get(t.Address())
|
|
|
|
if err != nil {
|
2013-05-15 22:38:31 -07:00
|
|
|
return err
|
2013-04-26 08:26:52 -07:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
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
|
|
|
|
2013-04-26 08:26:52 -07:00
|
|
|
// XXX: This is a wart; we need to handle this more gracefully down the
|
|
|
|
// road, especially once we have service discovery support.
|
2013-06-25 05:02:27 -07:00
|
|
|
baseLabels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.Address())}
|
2013-04-26 08:26:52 -07:00
|
|
|
for baseLabel, baseValue := range t.baseLabels {
|
|
|
|
baseLabels[baseLabel] = baseValue
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
processOptions := &extraction.ProcessOptions{
|
|
|
|
Timestamp: timestamp,
|
|
|
|
BaseLabels: baseLabels,
|
|
|
|
}
|
|
|
|
|
|
|
|
return processor.ProcessSingle(resp.Body, results, processOptions)
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
2013-01-12 12:22:59 -08:00
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
func (t target) State() TargetState {
|
2013-01-12 12:22:59 -08:00
|
|
|
return t.state
|
|
|
|
}
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
func (t target) scheduledFor() time.Time {
|
2013-01-12 12:22:59 -08:00
|
|
|
return t.scheduler.ScheduledFor()
|
|
|
|
}
|
2013-01-13 01:46:55 -08:00
|
|
|
|
2013-05-21 06:31:27 -07:00
|
|
|
func (t target) LastError() error {
|
|
|
|
return t.lastError
|
|
|
|
}
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
func (t target) Address() string {
|
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
2013-04-10 05:26:07 -07:00
|
|
|
func (t target) GlobalAddress() string {
|
|
|
|
address := t.address
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2013-04-19 07:29:58 -07:00
|
|
|
log.Printf("Couldn't get hostname: %s, returning target.Address()", err)
|
2013-04-10 05:26:07 -07:00
|
|
|
return address
|
|
|
|
}
|
|
|
|
for _, localhostRepresentation := range localhostRepresentations {
|
|
|
|
address = strings.Replace(address, localhostRepresentation, fmt.Sprintf("http://%s", hostname), -1)
|
|
|
|
}
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t target) BaseLabels() clientmodel.LabelSet {
|
2013-02-22 12:07:35 -08:00
|
|
|
return t.baseLabels
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge a new externally supplied target definition (e.g. with changed base
|
|
|
|
// labels) into an old target definition for the same endpoint. Preserve
|
|
|
|
// remaining information - like health state - from the old target.
|
|
|
|
func (t *target) Merge(newTarget Target) {
|
|
|
|
if t.Address() != newTarget.Address() {
|
|
|
|
panic("targets don't refer to the same endpoint")
|
|
|
|
}
|
|
|
|
t.baseLabels = newTarget.BaseLabels()
|
2013-01-13 01:46:55 -08:00
|
|
|
}
|
2013-06-05 05:44:20 -07:00
|
|
|
|
|
|
|
type targets []Target
|
|
|
|
|
|
|
|
func (t targets) Len() int {
|
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t targets) Less(i, j int) bool {
|
|
|
|
return t[i].scheduledFor().Before(t[j].scheduledFor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t targets) Swap(i, j int) {
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|