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-01-22 09:37:01 -08:00
|
|
|
"github.com/prometheus/client_golang/metrics"
|
2013-01-27 09:49:45 -08:00
|
|
|
"github.com/prometheus/prometheus/model"
|
|
|
|
"github.com/prometheus/prometheus/retrieval/format"
|
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-01-22 09:37:01 -08:00
|
|
|
const (
|
|
|
|
instance = "instance"
|
|
|
|
)
|
|
|
|
|
2013-04-10 05:26:07 -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-01-22 09:37:01 -08:00
|
|
|
Scrape(earliest time.Time, results chan format.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
|
|
|
|
// 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.
|
|
|
|
BaseLabels() model.LabelSet
|
|
|
|
// 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
|
|
|
|
state TargetState
|
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-02-22 12:07:35 -08:00
|
|
|
baseLabels model.LabelSet
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2013-01-13 01:46:55 -08:00
|
|
|
// Furnish a reasonably configured target for querying.
|
2013-02-22 12:07:35 -08:00
|
|
|
func NewTarget(address string, deadline time.Duration, baseLabels model.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-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-01-22 09:37:01 -08:00
|
|
|
func (t *target) Scrape(earliest time.Time, results chan format.Result) (err error) {
|
2013-01-04 05:41:47 -08:00
|
|
|
defer func() {
|
|
|
|
futureState := t.state
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
futureState = ALIVE
|
|
|
|
default:
|
|
|
|
futureState = UNREACHABLE
|
|
|
|
}
|
|
|
|
|
2013-01-12 12:22:59 -08:00
|
|
|
t.scheduler.Reschedule(earliest, futureState)
|
2013-02-24 17:52:52 -08:00
|
|
|
t.state = futureState
|
2013-01-04 05:41:47 -08:00
|
|
|
}()
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-04 05:41:47 -08:00
|
|
|
done := make(chan bool)
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-04 08:55:58 -08:00
|
|
|
request := func() {
|
2013-01-22 09:37:01 -08:00
|
|
|
defer func() {
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
2013-04-12 18:45:37 -07:00
|
|
|
now := time.Now()
|
|
|
|
|
2013-02-21 10:48:54 -08:00
|
|
|
var resp *http.Response // Don't shadow "err" from the enclosing function.
|
|
|
|
resp, err = http.Get(t.Address())
|
2013-01-04 05:41:47 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2013-01-04 05:41:47 -08:00
|
|
|
defer resp.Body.Close()
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-22 09:37:01 -08:00
|
|
|
processor, err := format.DefaultRegistry.ProcessorForRequestHeader(resp.Header)
|
2013-01-04 05:41:47 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-01-22 09:37:01 -08:00
|
|
|
// XXX: This is a wart; we need to handle this more gracefully down the
|
|
|
|
// road, especially once we have service discovery support.
|
|
|
|
baseLabels := model.LabelSet{instance: model.LabelValue(t.Address())}
|
2013-02-22 12:07:35 -08:00
|
|
|
for baseLabel, baseValue := range t.baseLabels {
|
2013-01-22 09:37:01 -08:00
|
|
|
baseLabels[baseLabel] = baseValue
|
2013-01-17 16:05:14 -08:00
|
|
|
}
|
2013-01-04 05:41:47 -08:00
|
|
|
|
2013-04-12 18:45:37 -07:00
|
|
|
err = processor.Process(resp.Body, now, baseLabels, results)
|
2013-01-22 09:37:01 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
2013-01-04 08:55:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
accumulator := func(d time.Duration) {
|
|
|
|
ms := float64(d) / float64(time.Millisecond)
|
2013-01-23 08:18:45 -08:00
|
|
|
labels := map[string]string{address: t.Address(), outcome: success}
|
|
|
|
if err != nil {
|
|
|
|
labels[outcome] = failure
|
2013-01-04 08:55:58 -08:00
|
|
|
}
|
2013-01-23 08:18:45 -08:00
|
|
|
|
|
|
|
targetOperationLatencies.Add(labels, ms)
|
|
|
|
targetOperations.Increment(labels)
|
2013-01-04 08:55:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
go metrics.InstrumentCall(request, accumulator)
|
2013-01-04 05:41:47 -08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
break
|
|
|
|
case <-time.After(t.Deadline):
|
|
|
|
err = fmt.Errorf("Target %s exceeded %s deadline.", t, t.Deadline)
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Printf("Couldn't get hostname: %s, returning target.Address()")
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
for _, localhostRepresentation := range localhostRepresentations {
|
|
|
|
address = strings.Replace(address, localhostRepresentation, fmt.Sprintf("http://%s", hostname), -1)
|
|
|
|
}
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2013-02-22 12:07:35 -08:00
|
|
|
func (t target) BaseLabels() model.LabelSet {
|
|
|
|
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
|
|
|
}
|