2015-08-24 10:19:21 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2015-06-22 13:46:55 -07:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-09-08 08:39:52 -07:00
|
|
|
"sort"
|
2015-06-22 13:46:55 -07:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2016-12-06 07:09:50 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-07-11 11:27:25 -07:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2015-08-21 04:16:50 -07:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2016-09-08 08:39:52 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-01 09:47:48 -07:00
|
|
|
|
2016-07-11 11:27:25 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2015-06-22 13:46:55 -07:00
|
|
|
)
|
|
|
|
|
2016-12-06 07:09:50 -08:00
|
|
|
var (
|
|
|
|
federationErrors = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "prometheus_web_federation_errors_total",
|
|
|
|
Help: "Total number of errors that occurred while sending federation responses.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2015-09-01 09:47:48 -07:00
|
|
|
func (h *Handler) federation(w http.ResponseWriter, req *http.Request) {
|
|
|
|
h.mtx.RLock()
|
|
|
|
defer h.mtx.RUnlock()
|
2015-06-22 13:46:55 -07:00
|
|
|
|
|
|
|
req.ParseForm()
|
|
|
|
|
2016-07-11 11:27:25 -07:00
|
|
|
var matcherSets []metric.LabelMatchers
|
2015-06-22 13:46:55 -07:00
|
|
|
for _, s := range req.Form["match[]"] {
|
|
|
|
matchers, err := promql.ParseMetricSelector(s)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-07-11 11:27:25 -07:00
|
|
|
matcherSets = append(matcherSets, matchers)
|
2015-06-22 13:46:55 -07:00
|
|
|
}
|
|
|
|
|
2015-12-16 04:45:44 -08:00
|
|
|
var (
|
2016-09-08 08:39:52 -07:00
|
|
|
minTimestamp = h.now().Add(-promql.StalenessDelta)
|
2015-12-16 04:45:44 -08:00
|
|
|
format = expfmt.Negotiate(req.Header)
|
|
|
|
enc = expfmt.NewEncoder(w, format)
|
|
|
|
)
|
2015-08-21 04:16:50 -07:00
|
|
|
w.Header().Set("Content-Type", string(format))
|
|
|
|
|
2016-10-12 10:34:22 -07:00
|
|
|
q, err := h.storage.Querier()
|
|
|
|
if err != nil {
|
2016-12-06 07:09:50 -08:00
|
|
|
federationErrors.Inc()
|
2016-10-12 10:34:22 -07:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer q.Close()
|
|
|
|
|
|
|
|
vector, err := q.LastSampleForLabelMatchers(h.context, minTimestamp, matcherSets...)
|
2016-07-11 11:27:25 -07:00
|
|
|
if err != nil {
|
2016-12-06 07:09:50 -08:00
|
|
|
federationErrors.Inc()
|
2016-07-11 11:27:25 -07:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-09-08 08:39:52 -07:00
|
|
|
sort.Sort(byName(vector))
|
|
|
|
|
|
|
|
var (
|
|
|
|
lastMetricName model.LabelValue
|
|
|
|
protMetricFam *dto.MetricFamily
|
|
|
|
)
|
2016-07-11 11:27:25 -07:00
|
|
|
for _, s := range vector {
|
2016-09-08 08:39:52 -07:00
|
|
|
nameSeen := false
|
2015-09-01 09:47:48 -07:00
|
|
|
globalUsed := map[model.LabelName]struct{}{}
|
2016-09-08 08:39:52 -07:00
|
|
|
protMetric := &dto.Metric{
|
|
|
|
Untyped: &dto.Untyped{},
|
|
|
|
}
|
2015-06-22 13:46:55 -07:00
|
|
|
|
2016-03-08 15:09:42 -08:00
|
|
|
for ln, lv := range s.Metric {
|
2016-09-08 08:39:52 -07:00
|
|
|
if lv == "" {
|
|
|
|
// No value means unset. Never consider those labels.
|
|
|
|
// This is also important to protect against nameless metrics.
|
|
|
|
continue
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
if ln == model.MetricNameLabel {
|
2016-09-08 08:39:52 -07:00
|
|
|
nameSeen = true
|
|
|
|
if lv == lastMetricName {
|
|
|
|
// We already have the name in the current MetricFamily,
|
|
|
|
// and we ignore nameless metrics.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Need to start a new MetricFamily. Ship off the old one (if any) before
|
|
|
|
// creating the new one.
|
|
|
|
if protMetricFam != nil {
|
|
|
|
if err := enc.Encode(protMetricFam); err != nil {
|
2016-12-06 07:09:50 -08:00
|
|
|
federationErrors.Inc()
|
2016-12-06 06:52:50 -08:00
|
|
|
log.With("err", err).Error("federation failed")
|
2016-09-08 08:39:52 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protMetricFam = &dto.MetricFamily{
|
|
|
|
Type: dto.MetricType_UNTYPED.Enum(),
|
|
|
|
Name: proto.String(string(lv)),
|
|
|
|
}
|
|
|
|
lastMetricName = lv
|
2015-06-22 13:46:55 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
protMetric.Label = append(protMetric.Label, &dto.LabelPair{
|
|
|
|
Name: proto.String(string(ln)),
|
|
|
|
Value: proto.String(string(lv)),
|
|
|
|
})
|
2015-09-29 08:51:03 -07:00
|
|
|
if _, ok := h.externalLabels[ln]; ok {
|
2015-09-01 09:47:48 -07:00
|
|
|
globalUsed[ln] = struct{}{}
|
|
|
|
}
|
2015-06-22 13:46:55 -07:00
|
|
|
}
|
2016-09-08 08:39:52 -07:00
|
|
|
if !nameSeen {
|
|
|
|
log.With("metric", s.Metric).Warn("Ignoring nameless metric during federation.")
|
|
|
|
continue
|
|
|
|
}
|
2015-09-01 09:47:48 -07:00
|
|
|
// Attach global labels if they do not exist yet.
|
2015-09-29 08:51:03 -07:00
|
|
|
for ln, lv := range h.externalLabels {
|
2015-09-01 09:47:48 -07:00
|
|
|
if _, ok := globalUsed[ln]; !ok {
|
|
|
|
protMetric.Label = append(protMetric.Label, &dto.LabelPair{
|
|
|
|
Name: proto.String(string(ln)),
|
|
|
|
Value: proto.String(string(lv)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 15:09:42 -08:00
|
|
|
protMetric.TimestampMs = proto.Int64(int64(s.Timestamp))
|
|
|
|
protMetric.Untyped.Value = proto.Float64(float64(s.Value))
|
2015-06-22 13:46:55 -07:00
|
|
|
|
2016-09-08 08:39:52 -07:00
|
|
|
protMetricFam.Metric = append(protMetricFam.Metric, protMetric)
|
|
|
|
}
|
|
|
|
// Still have to ship off the last MetricFamily, if any.
|
|
|
|
if protMetricFam != nil {
|
2015-08-21 04:16:50 -07:00
|
|
|
if err := enc.Encode(protMetricFam); err != nil {
|
2016-12-06 07:09:50 -08:00
|
|
|
federationErrors.Inc()
|
2016-12-06 06:52:50 -08:00
|
|
|
log.With("err", err).Error("federation failed")
|
2015-06-22 13:46:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 08:39:52 -07:00
|
|
|
|
|
|
|
// byName makes a model.Vector sortable by metric name.
|
|
|
|
type byName model.Vector
|
|
|
|
|
|
|
|
func (vec byName) Len() int { return len(vec) }
|
|
|
|
func (vec byName) Swap(i, j int) { vec[i], vec[j] = vec[j], vec[i] }
|
|
|
|
|
|
|
|
func (vec byName) Less(i, j int) bool {
|
|
|
|
ni := vec[i].Metric[model.MetricNameLabel]
|
|
|
|
nj := vec[j].Metric[model.MetricNameLabel]
|
|
|
|
return ni < nj
|
|
|
|
}
|