mirror of https://github.com/prometheus/prometheus
web: implement federation for new storage
parent
7322c46b8e
commit
61bd698143
222
web/federate.go
222
web/federate.go
|
@ -15,14 +15,19 @@ package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/gogo/protobuf/proto"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
dto "github.com/prometheus/client_model/go"
|
||||||
"github.com/prometheus/common/expfmt"
|
"github.com/prometheus/common/expfmt"
|
||||||
|
"github.com/prometheus/common/log"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/pkg/labels"
|
"github.com/prometheus/prometheus/pkg/labels"
|
||||||
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
|
"github.com/prometheus/prometheus/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -49,124 +54,145 @@ func (h *Handler) federation(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// minTimestamp = h.now().Add(-promql.StalenessDelta)
|
mint = timestamp.FromTime(h.now().Time().Add(-promql.StalenessDelta))
|
||||||
|
maxt = timestamp.FromTime(h.now().Time())
|
||||||
format = expfmt.Negotiate(req.Header)
|
format = expfmt.Negotiate(req.Header)
|
||||||
// enc = expfmt.NewEncoder(w, format)
|
enc = expfmt.NewEncoder(w, format)
|
||||||
)
|
)
|
||||||
w.Header().Set("Content-Type", string(format))
|
w.Header().Set("Content-Type", string(format))
|
||||||
|
|
||||||
|
q, err := h.storage.Querier(mint, maxt)
|
||||||
|
if err != nil {
|
||||||
federationErrors.Inc()
|
federationErrors.Inc()
|
||||||
http.Error(w, errors.Errorf("federation disabled").Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
defer q.Close()
|
||||||
|
|
||||||
// q, err := h.storage.Querier()
|
// TODO(fabxc): expose merge functionality in storage interface.
|
||||||
// if err != nil {
|
// We just concatenate results for all sets of matchers, which may produce
|
||||||
// federationErrors.Inc()
|
// duplicated results.
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
vec := make(promql.Vector, 0, 8000)
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// defer q.Close()
|
|
||||||
|
|
||||||
// TODO(fabxc): support via TSDB storage.
|
for _, mset := range matcherSets {
|
||||||
|
series := q.Select(mset...)
|
||||||
|
for series.Next() {
|
||||||
|
s := series.Series()
|
||||||
|
// TODO(fabxc): allow fast path for most recent sample either
|
||||||
|
// in the storage itself or caching layer in Prometheus.
|
||||||
|
it := storage.NewBuffer(s.Iterator(), int64(promql.StalenessDelta/1e6))
|
||||||
|
|
||||||
// var sets []tsdb.SeriesSet
|
var t int64
|
||||||
// for _, matchers := range matcherSets {
|
var v float64
|
||||||
// set, err := q.Select(matchers)
|
|
||||||
// sets = append(sets, set)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// vector, err := q.LastSampleForLabelMatchers(h.context, minTimestamp, matcherSets...)
|
ok := it.Seek(maxt)
|
||||||
// if err != nil {
|
if ok {
|
||||||
// federationErrors.Inc()
|
t, v = it.Values()
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
} else {
|
||||||
// return
|
t, v, ok = it.PeekBack()
|
||||||
// }
|
if !ok {
|
||||||
// sort.Sort(byName(vector))
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// var (
|
vec = append(vec, promql.Sample{
|
||||||
// lastMetricName model.LabelValue
|
Metric: s.Labels(),
|
||||||
// protMetricFam *dto.MetricFamily
|
Point: promql.Point{T: t, V: v},
|
||||||
// )
|
})
|
||||||
// for _, s := range vector {
|
}
|
||||||
// nameSeen := false
|
if series.Err() != nil {
|
||||||
// globalUsed := map[model.LabelName]struct{}{}
|
federationErrors.Inc()
|
||||||
// protMetric := &dto.Metric{
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// Untyped: &dto.Untyped{},
|
return
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// for ln, lv := range s.Metric {
|
sort.Sort(byName(vec))
|
||||||
// if lv == "" {
|
|
||||||
// // No value means unset. Never consider those labels.
|
|
||||||
// // This is also important to protect against nameless metrics.
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// if ln == model.MetricNameLabel {
|
|
||||||
// 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 {
|
|
||||||
// federationErrors.Inc()
|
|
||||||
// log.With("err", err).Error("federation failed")
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// protMetricFam = &dto.MetricFamily{
|
|
||||||
// Type: dto.MetricType_UNTYPED.Enum(),
|
|
||||||
// Name: proto.String(string(lv)),
|
|
||||||
// }
|
|
||||||
// lastMetricName = lv
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// protMetric.Label = append(protMetric.Label, &dto.LabelPair{
|
|
||||||
// Name: proto.String(string(ln)),
|
|
||||||
// Value: proto.String(string(lv)),
|
|
||||||
// })
|
|
||||||
// if _, ok := h.externalLabels[ln]; ok {
|
|
||||||
// globalUsed[ln] = struct{}{}
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if !nameSeen {
|
|
||||||
// log.With("metric", s.Metric).Warn("Ignoring nameless metric during federation.")
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// // Attach global labels if they do not exist yet.
|
|
||||||
// for ln, lv := range h.externalLabels {
|
|
||||||
// if _, ok := globalUsed[ln]; !ok {
|
|
||||||
// protMetric.Label = append(protMetric.Label, &dto.LabelPair{
|
|
||||||
// Name: proto.String(string(ln)),
|
|
||||||
// Value: proto.String(string(lv)),
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// protMetric.TimestampMs = proto.Int64(int64(s.Timestamp))
|
var (
|
||||||
// protMetric.Untyped.Value = proto.Float64(float64(s.Value))
|
lastMetricName string
|
||||||
|
protMetricFam *dto.MetricFamily
|
||||||
|
)
|
||||||
|
for _, s := range vec {
|
||||||
|
nameSeen := false
|
||||||
|
globalUsed := map[string]struct{}{}
|
||||||
|
protMetric := &dto.Metric{
|
||||||
|
Untyped: &dto.Untyped{},
|
||||||
|
}
|
||||||
|
|
||||||
// protMetricFam.Metric = append(protMetricFam.Metric, protMetric)
|
for _, l := range s.Metric {
|
||||||
// }
|
if l.Value == "" {
|
||||||
// // Still have to ship off the last MetricFamily, if any.
|
// No value means unset. Never consider those labels.
|
||||||
// if protMetricFam != nil {
|
// This is also important to protect against nameless metrics.
|
||||||
// if err := enc.Encode(protMetricFam); err != nil {
|
continue
|
||||||
// federationErrors.Inc()
|
}
|
||||||
// log.With("err", err).Error("federation failed")
|
if l.Name == labels.MetricName {
|
||||||
// }
|
nameSeen = true
|
||||||
// }
|
if l.Value == 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 {
|
||||||
|
federationErrors.Inc()
|
||||||
|
log.With("err", err).Error("federation failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protMetricFam = &dto.MetricFamily{
|
||||||
|
Type: dto.MetricType_UNTYPED.Enum(),
|
||||||
|
Name: proto.String(l.Value),
|
||||||
|
}
|
||||||
|
lastMetricName = l.Value
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
protMetric.Label = append(protMetric.Label, &dto.LabelPair{
|
||||||
|
Name: proto.String(l.Name),
|
||||||
|
Value: proto.String(l.Value),
|
||||||
|
})
|
||||||
|
if _, ok := h.externalLabels[model.LabelName(l.Name)]; ok {
|
||||||
|
globalUsed[l.Name] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !nameSeen {
|
||||||
|
log.With("metric", s.Metric).Warn("Ignoring nameless metric during federation.")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Attach global labels if they do not exist yet.
|
||||||
|
for ln, lv := range h.externalLabels {
|
||||||
|
if _, ok := globalUsed[string(ln)]; !ok {
|
||||||
|
protMetric.Label = append(protMetric.Label, &dto.LabelPair{
|
||||||
|
Name: proto.String(string(ln)),
|
||||||
|
Value: proto.String(string(lv)),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protMetric.TimestampMs = proto.Int64(s.T)
|
||||||
|
protMetric.Untyped.Value = proto.Float64(s.V)
|
||||||
|
|
||||||
|
protMetricFam.Metric = append(protMetricFam.Metric, protMetric)
|
||||||
|
}
|
||||||
|
// Still have to ship off the last MetricFamily, if any.
|
||||||
|
if protMetricFam != nil {
|
||||||
|
if err := enc.Encode(protMetricFam); err != nil {
|
||||||
|
federationErrors.Inc()
|
||||||
|
log.With("err", err).Error("federation failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// byName makes a model.Vector sortable by metric name.
|
// byName makes a model.Vector sortable by metric name.
|
||||||
type byName model.Vector
|
type byName promql.Vector
|
||||||
|
|
||||||
func (vec byName) Len() int { return len(vec) }
|
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) Swap(i, j int) { vec[i], vec[j] = vec[j], vec[i] }
|
||||||
|
|
||||||
func (vec byName) Less(i, j int) bool {
|
func (vec byName) Less(i, j int) bool {
|
||||||
ni := vec[i].Metric[model.MetricNameLabel]
|
ni := vec[i].Metric.Get(labels.MetricName)
|
||||||
nj := vec[j].Metric[model.MetricNameLabel]
|
nj := vec[j].Metric.Get(labels.MetricName)
|
||||||
return ni < nj
|
return ni < nj
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue