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"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/promql"
|
2015-08-24 09:04:41 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2015-06-22 13:46:55 -07:00
|
|
|
|
2015-08-21 04:16:50 -07:00
|
|
|
"github.com/prometheus/common/expfmt"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-01 09:47:48 -07:00
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
2015-06-22 13:46:55 -07:00
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
metrics := map[model.Fingerprint]metric.Metric{}
|
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
|
|
|
|
}
|
2015-09-01 09:47:48 -07:00
|
|
|
for fp, met := range h.storage.MetricsForLabelMatchers(matchers...) {
|
2015-06-22 13:46:55 -07:00
|
|
|
metrics[fp] = met
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 04:45:44 -08:00
|
|
|
var (
|
|
|
|
minTimestamp = model.Now().Add(-promql.StalenessDelta)
|
|
|
|
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))
|
|
|
|
|
2015-06-22 13:46:55 -07:00
|
|
|
protMetric := &dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{},
|
|
|
|
Untyped: &dto.Untyped{},
|
|
|
|
}
|
|
|
|
protMetricFam := &dto.MetricFamily{
|
|
|
|
Metric: []*dto.Metric{protMetric},
|
|
|
|
Type: dto.MetricType_UNTYPED.Enum(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for fp, met := range metrics {
|
2015-09-01 09:47:48 -07:00
|
|
|
globalUsed := map[model.LabelName]struct{}{}
|
|
|
|
|
|
|
|
sp := h.storage.LastSamplePairForFingerprint(fp)
|
2015-12-16 04:45:44 -08:00
|
|
|
// Discard if sample does not exist or lays before the staleness interval.
|
|
|
|
if sp == nil || sp.Timestamp.Before(minTimestamp) {
|
2015-06-22 13:46:55 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset label slice.
|
|
|
|
protMetric.Label = protMetric.Label[:0]
|
|
|
|
|
|
|
|
for ln, lv := range met.Metric {
|
2015-08-20 08:18:46 -07:00
|
|
|
if ln == model.MetricNameLabel {
|
2015-06-22 13:46:55 -07:00
|
|
|
protMetricFam.Name = proto.String(string(lv))
|
|
|
|
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
|
|
|
}
|
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)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 10:00:53 -07:00
|
|
|
protMetric.TimestampMs = proto.Int64(int64(sp.Timestamp))
|
|
|
|
protMetric.Untyped.Value = proto.Float64(float64(sp.Value))
|
2015-06-22 13:46:55 -07:00
|
|
|
|
2015-08-21 04:16:50 -07:00
|
|
|
if err := enc.Encode(protMetricFam); err != nil {
|
2015-06-22 13:46:55 -07:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|