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-07-14 21:03:56 +00:00
|
|
|
"bytes"
|
2013-01-04 13:41:47 +00:00
|
|
|
"fmt"
|
2013-04-10 12:26:07 +00:00
|
|
|
"log"
|
2013-01-04 13:41:47 +00:00
|
|
|
"net/http"
|
2013-04-10 12:26:07 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2012-12-25 12:50:36 +00:00
|
|
|
"time"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/extraction"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2012-12-25 12:50:36 +00:00
|
|
|
)
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
const (
|
|
|
|
InstanceLabel clientmodel.LabelName = "instance"
|
|
|
|
// The metric name for the synthetic health variable.
|
|
|
|
ScrapeHealthMetricName clientmodel.LabelValue = "up"
|
2013-04-10 12:26:07 +00:00
|
|
|
)
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
var localhostRepresentations = []string{"http://127.0.0.1", "http://localhost"}
|
|
|
|
|
2013-01-13 09:46:55 +00:00
|
|
|
// The state of the given 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 {
|
|
|
|
case UNKNOWN:
|
|
|
|
return "UNKNOWN"
|
|
|
|
case ALIVE:
|
|
|
|
return "ALIVE"
|
|
|
|
case UNREACHABLE:
|
|
|
|
return "UNREACHABLE"
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unknown state")
|
|
|
|
}
|
|
|
|
|
2012-12-25 12:50:36 +00:00
|
|
|
const (
|
2013-01-13 09:46:55 +00:00
|
|
|
// The Target has not been seen; we know nothing about it, except that it is
|
|
|
|
// on our docket for examination.
|
2012-12-25 12:50:36 +00:00
|
|
|
UNKNOWN TargetState = iota
|
2013-01-13 09:46:55 +00:00
|
|
|
// The Target has been found and successfully queried.
|
2012-12-25 12:50:36 +00:00
|
|
|
ALIVE
|
2013-01-13 09:46:55 +00: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 12:50:36 +00:00
|
|
|
UNREACHABLE
|
|
|
|
)
|
|
|
|
|
2013-01-13 09:46:55 +00: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 20:22:59 +00:00
|
|
|
type healthReporter interface {
|
2013-01-13 09:46:55 +00: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 12:02:27 +00:00
|
|
|
Scrape(earliest time.Time, results chan<- *extraction.Result) error
|
2013-01-13 09:46:55 +00:00
|
|
|
// Fulfill the healthReporter interface.
|
2013-01-12 20:22:59 +00:00
|
|
|
State() TargetState
|
2013-01-13 09:46:55 +00: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.
|
2013-07-14 16:48:03 +00:00
|
|
|
ScheduledFor() time.Time
|
2013-07-15 13:11:41 +00:00
|
|
|
// EstimatedTimeToExecute emits the amount of time until the next prospective
|
|
|
|
// scheduling opportunity for this target.
|
|
|
|
EstimatedTimeToExecute() time.Duration
|
2013-05-21 13:31:27 +00:00
|
|
|
// Return the last encountered scrape error, if any.
|
|
|
|
LastError() error
|
2013-01-13 09:46:55 +00: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 12:26:07 +00: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 20:07:35 +00:00
|
|
|
// Return the target's base labels.
|
2013-06-25 12:02:27 +00:00
|
|
|
BaseLabels() clientmodel.LabelSet
|
2013-02-22 20:07:35 +00: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 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 {
|
|
|
|
// scheduler provides the scheduling strategy that is used to formulate what
|
|
|
|
// is returned in Target.scheduledFor.
|
2013-01-12 20:22:59 +00:00
|
|
|
scheduler scheduler
|
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
|
2013-01-04 11:17:31 +00:00
|
|
|
|
2013-01-13 09:46:55 +00: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 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.
|
|
|
|
client http.Client
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 09:46:55 +00:00
|
|
|
// Furnish a reasonably configured target for querying.
|
2013-06-25 12:02:27 +00:00
|
|
|
func NewTarget(address string, deadline time.Duration, baseLabels clientmodel.LabelSet) Target {
|
2013-01-13 09:46:55 +00:00
|
|
|
target := &target{
|
|
|
|
address: address,
|
2013-01-12 20:22:59 +00:00
|
|
|
Deadline: deadline,
|
2013-02-22 20:07:35 +00:00
|
|
|
baseLabels: baseLabels,
|
2013-04-26 15:26:52 +00:00
|
|
|
client: NewDeadlineClient(deadline),
|
2013-01-04 13:41:47 +00:00
|
|
|
}
|
2012-12-25 12:50:36 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
scheduler := &healthScheduler{
|
|
|
|
target: target,
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
2013-01-12 20:22:59 +00:00
|
|
|
target.scheduler = scheduler
|
|
|
|
|
|
|
|
return target
|
|
|
|
}
|
2012-12-25 12:50:36 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (t *target) recordScrapeHealth(results chan<- *extraction.Result, timestamp time.Time, healthy bool) {
|
|
|
|
metric := clientmodel.Metric{}
|
2013-04-16 15:22:10 +00:00
|
|
|
for label, value := range t.baseLabels {
|
|
|
|
metric[label] = value
|
|
|
|
}
|
2013-06-25 12:02:27 +00:00
|
|
|
metric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(ScrapeHealthMetricName)
|
|
|
|
metric[InstanceLabel] = clientmodel.LabelValue(t.Address())
|
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
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
sample := &clientmodel.Sample{
|
2013-04-16 15:22:10 +00:00
|
|
|
Metric: metric,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Value: healthValue,
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
results <- &extraction.Result{
|
2013-04-29 14:55:18 +00:00
|
|
|
Err: nil,
|
2013-06-25 12:02:27 +00:00
|
|
|
Samples: clientmodel.Samples{sample},
|
2013-04-16 15:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) Scrape(earliest time.Time, results chan<- *extraction.Result) error {
|
2013-04-16 15:22:10 +00:00
|
|
|
now := time.Now()
|
2013-04-26 15:26:52 +00:00
|
|
|
futureState := t.state
|
2013-07-14 16:48:03 +00:00
|
|
|
err := t.scrape(now, results)
|
|
|
|
if err != nil {
|
2013-04-26 15:26:52 +00:00
|
|
|
t.recordScrapeHealth(results, now, false)
|
|
|
|
futureState = UNREACHABLE
|
|
|
|
} else {
|
|
|
|
t.recordScrapeHealth(results, now, true)
|
|
|
|
futureState = ALIVE
|
|
|
|
}
|
2013-04-16 15:22:10 +00:00
|
|
|
|
2013-04-26 15:26:52 +00:00
|
|
|
t.scheduler.Reschedule(earliest, futureState)
|
|
|
|
t.state = futureState
|
2013-05-21 13:31:27 +00:00
|
|
|
t.lastError = err
|
2013-04-28 17:40:30 +00:00
|
|
|
|
2013-05-16 05:38:31 +00:00
|
|
|
return err
|
2013-04-26 15:26:52 +00:00
|
|
|
}
|
2013-01-22 17:37:01 +00:00
|
|
|
|
2013-06-27 16:32:05 +00:00
|
|
|
const acceptHeader = `application/vnd.google.protobuf;proto=io.prometheus.client.MetricFamily;encoding=delimited;q=0.7,application/json;schema=prometheus/telemetry;version=0.0.2;q=0.2,*/*;q=0.1`
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (t *target) scrape(timestamp time.Time, results chan<- *extraction.Result) (err error) {
|
2013-04-26 15:26:52 +00: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 13:41:47 +00:00
|
|
|
if err != nil {
|
2013-04-26 15:26:52 +00:00
|
|
|
labels[outcome] = failure
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 15:26:52 +00:00
|
|
|
targetOperationLatencies.Add(labels, ms)
|
|
|
|
targetOperations.Increment(labels)
|
|
|
|
}(time.Now())
|
2013-01-04 13:41:47 +00:00
|
|
|
|
2013-06-27 16:32:05 +00:00
|
|
|
req, err := http.NewRequest("GET", t.Address(), nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", acceptHeader)
|
|
|
|
|
|
|
|
resp, err := t.client.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
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
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
|
|
|
|
2013-04-26 15:26:52 +00: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 12:02:27 +00:00
|
|
|
baseLabels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.Address())}
|
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-06-25 12:02:27 +00:00
|
|
|
processOptions := &extraction.ProcessOptions{
|
|
|
|
Timestamp: timestamp,
|
|
|
|
BaseLabels: baseLabels,
|
|
|
|
}
|
|
|
|
|
2013-07-14 21:03:56 +00:00
|
|
|
// N.B. - It is explicitly required to extract the entire payload before
|
|
|
|
// attempting to deserialize, as the underlying reader expects will
|
|
|
|
// interpret pending data as a truncated message.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := buf.ReadFrom(resp.Body); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return processor.ProcessSingle(buf, results, processOptions)
|
2012-12-25 12:50:36 +00:00
|
|
|
}
|
2013-01-12 20:22:59 +00:00
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) State() TargetState {
|
2013-01-12 20:22:59 +00:00
|
|
|
return t.state
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) ScheduledFor() time.Time {
|
2013-01-12 20:22:59 +00:00
|
|
|
return t.scheduler.ScheduledFor()
|
|
|
|
}
|
2013-01-13 09:46:55 +00:00
|
|
|
|
2013-07-15 13:11:41 +00:00
|
|
|
func (t *target) EstimatedTimeToExecute() time.Duration {
|
|
|
|
return t.scheduler.ScheduledFor().Sub(time.Now())
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) LastError() error {
|
2013-05-21 13:31:27 +00:00
|
|
|
return t.lastError
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) Address() string {
|
2013-01-13 09:46:55 +00:00
|
|
|
return t.address
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) GlobalAddress() string {
|
2013-04-10 12:26:07 +00:00
|
|
|
address := t.address
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2013-04-19 14:29:58 +00:00
|
|
|
log.Printf("Couldn't get hostname: %s, returning target.Address()", err)
|
2013-04-10 12:26:07 +00:00
|
|
|
return address
|
|
|
|
}
|
|
|
|
for _, localhostRepresentation := range localhostRepresentations {
|
|
|
|
address = strings.Replace(address, localhostRepresentation, fmt.Sprintf("http://%s", hostname), -1)
|
|
|
|
}
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:48:03 +00:00
|
|
|
func (t *target) BaseLabels() clientmodel.LabelSet {
|
2013-02-22 20:07:35 +00: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 09:46:55 +00:00
|
|
|
}
|
2013-06-05 12:44:20 +00:00
|
|
|
|
|
|
|
type targets []Target
|
|
|
|
|
|
|
|
func (t targets) Len() int {
|
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t targets) Less(i, j int) bool {
|
2013-07-14 16:48:03 +00:00
|
|
|
return t[i].ScheduledFor().Before(t[j].ScheduledFor())
|
2013-06-05 12:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t targets) Swap(i, j int) {
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|