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 (
|
2013-01-04 05:41:47 -08:00
|
|
|
"fmt"
|
2016-02-28 10:56:18 -08:00
|
|
|
"hash/fnv"
|
2015-07-22 08:48:22 -07:00
|
|
|
"io/ioutil"
|
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
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2013-07-30 08:18:07 -07:00
|
|
|
|
2015-04-20 03:24:25 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2016-08-09 03:36:23 -07:00
|
|
|
"github.com/prometheus/prometheus/relabel"
|
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
|
|
|
)
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// TargetHealth describes the health state of a target.
|
2016-02-28 11:23:26 -08:00
|
|
|
type TargetHealth string
|
2015-08-21 14:01:08 -07:00
|
|
|
|
2016-02-28 11:23:26 -08:00
|
|
|
// The possible health states of a target based on the last performed scrape.
|
2012-12-25 04:50:36 -08:00
|
|
|
const (
|
2016-02-28 11:23:26 -08:00
|
|
|
HealthUnknown TargetHealth = "unknown"
|
|
|
|
HealthGood TargetHealth = "up"
|
|
|
|
HealthBad TargetHealth = "down"
|
2012-12-25 04:50:36 -08:00
|
|
|
)
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
// Target refers to a singular HTTP or HTTPS endpoint.
|
|
|
|
type Target struct {
|
2015-06-05 13:42:39 -07:00
|
|
|
// Labels before any processing.
|
2015-08-20 08:18:46 -07:00
|
|
|
metaLabels model.LabelSet
|
2016-02-15 01:31:38 -08:00
|
|
|
// Any labels that are added to this target and its metrics.
|
2016-02-12 06:43:27 -08:00
|
|
|
labels model.LabelSet
|
2016-02-28 10:56:18 -08:00
|
|
|
// Additional URL parmeters that are part of the target URL.
|
|
|
|
params url.Values
|
2016-02-28 11:23:26 -08:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
lastError error
|
|
|
|
lastScrape time.Time
|
|
|
|
health TargetHealth
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewTarget creates a reasonably configured target for querying.
|
2016-02-28 10:56:18 -08:00
|
|
|
func NewTarget(labels, metaLabels model.LabelSet, params url.Values) *Target {
|
2016-02-28 10:21:50 -08:00
|
|
|
return &Target{
|
2016-02-28 10:56:18 -08:00
|
|
|
labels: labels,
|
|
|
|
metaLabels: metaLabels,
|
|
|
|
params: params,
|
2016-02-28 11:23:26 -08:00
|
|
|
health: HealthUnknown,
|
2013-01-04 05:41:47 -08:00
|
|
|
}
|
2015-03-14 19:36:15 -07:00
|
|
|
}
|
2013-04-16 08:22:10 -07:00
|
|
|
|
2016-09-05 07:44:40 -07:00
|
|
|
// NewHTTPClient returns a new HTTP client configured for the given scrape configuration.
|
|
|
|
func NewHTTPClient(cfg *config.ScrapeConfig) (*http.Client, error) {
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsOpts := httputil.TLSOptions{
|
|
|
|
InsecureSkipVerify: cfg.TLSConfig.InsecureSkipVerify,
|
|
|
|
CAFile: cfg.TLSConfig.CAFile,
|
|
|
|
}
|
|
|
|
if len(cfg.TLSConfig.CertFile) > 0 && len(cfg.TLSConfig.KeyFile) > 0 {
|
|
|
|
tlsOpts.CertFile = cfg.TLSConfig.CertFile
|
|
|
|
tlsOpts.KeyFile = cfg.TLSConfig.KeyFile
|
|
|
|
}
|
2016-05-26 14:24:49 -07:00
|
|
|
if len(cfg.TLSConfig.ServerName) > 0 {
|
|
|
|
tlsOpts.ServerName = cfg.TLSConfig.ServerName
|
|
|
|
}
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsConfig, err := httputil.NewTLSConfig(tlsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
2016-02-16 02:44:42 -08:00
|
|
|
// The only timeout we care about is the configured scrape timeout.
|
|
|
|
// It is applied on request. So we leave out any timings here.
|
|
|
|
var rt http.RoundTripper = &http.Transport{
|
|
|
|
Proxy: http.ProxyURL(cfg.ProxyURL.URL),
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
2015-07-22 08:48:22 -07:00
|
|
|
|
|
|
|
// If a bearer token is provided, create a round tripper that will set the
|
|
|
|
// Authorization header correctly on each request.
|
|
|
|
bearerToken := cfg.BearerToken
|
|
|
|
if len(bearerToken) == 0 && len(cfg.BearerTokenFile) > 0 {
|
2015-08-24 06:07:27 -07:00
|
|
|
b, err := ioutil.ReadFile(cfg.BearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read bearer token file %s: %s", cfg.BearerTokenFile, err)
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
2016-08-17 06:16:24 -07:00
|
|
|
bearerToken = strings.TrimSpace(string(b))
|
2015-07-22 08:48:22 -07:00
|
|
|
}
|
2015-08-24 06:07:27 -07:00
|
|
|
|
2015-07-22 08:48:22 -07:00
|
|
|
if len(bearerToken) > 0 {
|
|
|
|
rt = httputil.NewBearerAuthRoundTripper(bearerToken, rt)
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
if cfg.BasicAuth != nil {
|
|
|
|
rt = httputil.NewBasicAuthRoundTripper(cfg.BasicAuth.Username, cfg.BasicAuth.Password, rt)
|
|
|
|
}
|
|
|
|
|
2015-07-22 08:48:22 -07:00
|
|
|
// Return a new client with the configured round tripper.
|
|
|
|
return httputil.NewClient(rt), nil
|
|
|
|
}
|
|
|
|
|
2015-05-18 04:14:41 -07:00
|
|
|
func (t *Target) String() string {
|
2016-02-28 10:56:18 -08:00
|
|
|
return t.URL().String()
|
2016-02-12 06:43:27 -08:00
|
|
|
}
|
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
// hash returns an identifying hash for the target.
|
|
|
|
func (t *Target) hash() uint64 {
|
|
|
|
h := fnv.New64a()
|
|
|
|
h.Write([]byte(t.labels.Fingerprint().String()))
|
|
|
|
h.Write([]byte(t.URL().String()))
|
2016-02-15 06:22:57 -08:00
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
return h.Sum64()
|
2016-02-15 06:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// offset returns the time until the next scrape cycle for the target.
|
|
|
|
func (t *Target) offset(interval time.Duration) time.Duration {
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
|
|
|
|
var (
|
|
|
|
base = now % int64(interval)
|
2016-02-28 10:56:18 -08:00
|
|
|
offset = t.hash() % uint64(interval)
|
2016-02-15 06:22:57 -08:00
|
|
|
next = base + int64(offset)
|
|
|
|
)
|
|
|
|
|
|
|
|
if next > int64(interval) {
|
|
|
|
next -= int64(interval)
|
|
|
|
}
|
|
|
|
return time.Duration(next)
|
|
|
|
}
|
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
// Labels returns a copy of the set of all public labels of the target.
|
|
|
|
func (t *Target) Labels() model.LabelSet {
|
|
|
|
lset := make(model.LabelSet, len(t.labels))
|
|
|
|
for ln, lv := range t.labels {
|
|
|
|
if !strings.HasPrefix(string(ln), model.ReservedLabelPrefix) {
|
|
|
|
lset[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lset
|
2016-02-12 06:43:27 -08:00
|
|
|
}
|
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
// MetaLabels returns a copy of the target's labels before any processing.
|
|
|
|
func (t *Target) MetaLabels() model.LabelSet {
|
|
|
|
return t.metaLabels.Clone()
|
2016-02-12 06:43:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// URL returns a copy of the target's URL.
|
|
|
|
func (t *Target) URL() *url.URL {
|
|
|
|
params := url.Values{}
|
|
|
|
|
2016-02-28 10:56:18 -08:00
|
|
|
for k, v := range t.params {
|
2016-02-12 06:43:27 -08:00
|
|
|
params[k] = make([]string, len(v))
|
|
|
|
copy(params[k], v)
|
|
|
|
}
|
|
|
|
for k, v := range t.labels {
|
|
|
|
if !strings.HasPrefix(string(k), model.ParamLabelPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ks := string(k[len(model.ParamLabelPrefix):])
|
|
|
|
|
|
|
|
if len(params[ks]) > 0 {
|
|
|
|
params[ks][0] = string(v)
|
|
|
|
} else {
|
|
|
|
params[ks] = []string{string(v)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: string(t.labels[model.SchemeLabel]),
|
|
|
|
Host: string(t.labels[model.AddressLabel]),
|
|
|
|
Path: string(t.labels[model.MetricsPathLabel]),
|
|
|
|
RawQuery: params.Encode(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:46:55 -08:00
|
|
|
func (t *Target) report(start time.Time, dur time.Duration, err error) {
|
2016-02-28 11:23:26 -08:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.health = HealthGood
|
|
|
|
} else {
|
|
|
|
t.health = HealthBad
|
|
|
|
}
|
|
|
|
|
|
|
|
t.lastError = err
|
|
|
|
t.lastScrape = start
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastError returns the error encountered during the last scrape.
|
|
|
|
func (t *Target) LastError() error {
|
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
|
|
|
return t.lastError
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastScrape returns the time of the last scrape.
|
|
|
|
func (t *Target) LastScrape() time.Time {
|
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
|
|
|
return t.lastScrape
|
|
|
|
}
|
|
|
|
|
|
|
|
// Health returns the last known health state of the target.
|
|
|
|
func (t *Target) Health() TargetHealth {
|
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
|
|
|
return t.health
|
2016-02-15 07:21:03 -08:00
|
|
|
}
|
|
|
|
|
2016-03-02 00:10:20 -08:00
|
|
|
// Targets is a sortable list of targets.
|
|
|
|
type Targets []*Target
|
|
|
|
|
|
|
|
func (ts Targets) Len() int { return len(ts) }
|
|
|
|
func (ts Targets) Less(i, j int) bool { return ts[i].URL().String() < ts[j].URL().String() }
|
|
|
|
func (ts Targets) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
|
|
|
|
|
2015-08-21 14:34:51 -07:00
|
|
|
// Merges the ingested sample's metric with the label set. On a collision the
|
|
|
|
// value of the ingested label is stored in a label prefixed with 'exported_'.
|
|
|
|
type ruleLabelsAppender struct {
|
2016-01-27 10:07:46 -08:00
|
|
|
storage.SampleAppender
|
2015-08-21 14:34:51 -07:00
|
|
|
labels model.LabelSet
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
func (app ruleLabelsAppender) Append(s *model.Sample) error {
|
2015-08-21 14:34:51 -07:00
|
|
|
for ln, lv := range app.labels {
|
|
|
|
if v, ok := s.Metric[ln]; ok && v != "" {
|
|
|
|
s.Metric[model.ExportedLabelPrefix+ln] = v
|
|
|
|
}
|
|
|
|
s.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
return app.SampleAppender.Append(s)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type honorLabelsAppender struct {
|
2016-01-27 10:07:46 -08:00
|
|
|
storage.SampleAppender
|
2015-08-21 14:34:51 -07:00
|
|
|
labels model.LabelSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merges the sample's metric with the given labels if the label is not
|
|
|
|
// already present in the metric.
|
|
|
|
// This also considers labels explicitly set to the empty string.
|
2016-02-02 05:01:44 -08:00
|
|
|
func (app honorLabelsAppender) Append(s *model.Sample) error {
|
2015-08-21 14:34:51 -07:00
|
|
|
for ln, lv := range app.labels {
|
|
|
|
if _, ok := s.Metric[ln]; !ok {
|
|
|
|
s.Metric[ln] = lv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
return app.SampleAppender.Append(s)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Applies a set of relabel configurations to the sample's metric
|
|
|
|
// before actually appending it.
|
|
|
|
type relabelAppender struct {
|
2016-01-27 10:07:46 -08:00
|
|
|
storage.SampleAppender
|
2015-08-21 14:34:51 -07:00
|
|
|
relabelings []*config.RelabelConfig
|
|
|
|
}
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
func (app relabelAppender) Append(s *model.Sample) error {
|
2016-08-09 03:36:23 -07:00
|
|
|
labels := relabel.Process(model.LabelSet(s.Metric), app.relabelings...)
|
|
|
|
|
2015-08-21 14:34:51 -07:00
|
|
|
// Check if the timeseries was dropped.
|
|
|
|
if labels == nil {
|
2016-02-02 05:01:44 -08:00
|
|
|
return nil
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|
|
|
|
s.Metric = model.Metric(labels)
|
|
|
|
|
2016-02-02 05:01:44 -08:00
|
|
|
return app.SampleAppender.Append(s)
|
2015-08-21 14:34:51 -07:00
|
|
|
}
|