| 
									
										
										
										
											2015-08-25 01:19:21 +08: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-23 04:46:55 +08:00
										 |  |  | package web | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-08-17 23:24:35 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-12 02:45:52 +08:00
										 |  |  | 	"github.com/go-kit/kit/log/level" | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	"github.com/gogo/protobuf/proto" | 
					
						
							| 
									
										
										
										
											2020-04-30 00:16:14 +08:00
										 |  |  | 	"github.com/pkg/errors" | 
					
						
							| 
									
										
										
										
											2016-12-06 23:09:50 +08:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	dto "github.com/prometheus/client_model/go" | 
					
						
							| 
									
										
										
										
											2015-08-21 19:16:50 +08:00
										 |  |  | 	"github.com/prometheus/common/expfmt" | 
					
						
							| 
									
										
										
										
											2015-08-20 23:18:46 +08:00
										 |  |  | 	"github.com/prometheus/common/model" | 
					
						
							| 
									
										
										
										
											2015-09-02 00:47:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 16:27:30 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/pkg/labels" | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/pkg/timestamp" | 
					
						
							| 
									
										
										
										
											2017-05-24 01:03:57 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/pkg/value" | 
					
						
							| 
									
										
										
										
											2016-07-12 02:27:25 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/promql" | 
					
						
							| 
									
										
										
										
											2020-02-04 02:06:39 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/promql/parser" | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/storage" | 
					
						
							| 
									
										
										
										
											2020-10-22 17:00:08 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb" | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-06 23: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.", | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-11-30 22:27:12 +08:00
										 |  |  | 	federationWarnings = prometheus.NewCounter(prometheus.CounterOpts{ | 
					
						
							|  |  |  | 		Name: "prometheus_web_federation_warnings_total", | 
					
						
							|  |  |  | 		Help: "Total number of warnings that occurred while sending federation responses.", | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2016-12-06 23:09:50 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 16:05:01 +08:00
										 |  |  | func registerFederationMetrics(r prometheus.Registerer) { | 
					
						
							|  |  |  | 	r.MustRegister(federationWarnings, federationErrors) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 00:47:48 +08:00
										 |  |  | func (h *Handler) federation(w http.ResponseWriter, req *http.Request) { | 
					
						
							|  |  |  | 	h.mtx.RLock() | 
					
						
							|  |  |  | 	defer h.mtx.RUnlock() | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 23:24:35 +08:00
										 |  |  | 	if err := req.ParseForm(); err != nil { | 
					
						
							|  |  |  | 		http.Error(w, fmt.Sprintf("error parsing form values: %v", err), http.StatusBadRequest) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 16:27:30 +08:00
										 |  |  | 	var matcherSets [][]*labels.Matcher | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 	for _, s := range req.Form["match[]"] { | 
					
						
							| 
									
										
										
										
											2020-02-04 02:06:39 +08:00
										 |  |  | 		matchers, err := parser.ParseMetricSelector(s) | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			http.Error(w, err.Error(), http.StatusBadRequest) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-07-12 02:27:25 +08:00
										 |  |  | 		matcherSets = append(matcherSets, matchers) | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 20:45:44 +08:00
										 |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2020-02-10 07:58:23 +08:00
										 |  |  | 		mint   = timestamp.FromTime(h.now().Time().Add(-h.lookbackDelta)) | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 		maxt   = timestamp.FromTime(h.now().Time()) | 
					
						
							| 
									
										
										
										
											2016-12-25 07:37:46 +08:00
										 |  |  | 		format = expfmt.Negotiate(req.Header) | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 		enc    = expfmt.NewEncoder(w, format) | 
					
						
							| 
									
										
										
										
											2015-12-16 20:45:44 +08:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2015-08-21 19:16:50 +08:00
										 |  |  | 	w.Header().Set("Content-Type", string(format)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 23:42:42 +08:00
										 |  |  | 	q, err := h.localStorage.Querier(req.Context(), mint, maxt) | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		federationErrors.Inc() | 
					
						
							| 
									
										
										
										
											2020-04-30 00:16:14 +08:00
										 |  |  | 		if errors.Cause(err) == tsdb.ErrNotReady { | 
					
						
							|  |  |  | 			http.Error(w, err.Error(), http.StatusServiceUnavailable) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 		http.Error(w, err.Error(), http.StatusInternalServerError) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer q.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	vec := make(promql.Vector, 0, 8000) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 17:36:09 +08:00
										 |  |  | 	hints := &storage.SelectHints{Start: mint, End: maxt} | 
					
						
							| 
									
										
										
										
											2018-08-28 18:23:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 19:00:23 +08:00
										 |  |  | 	var sets []storage.SeriesSet | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	for _, mset := range matcherSets { | 
					
						
							| 
									
										
										
										
											2020-06-10 00:57:31 +08:00
										 |  |  | 		s := q.Select(false, hints, mset...) | 
					
						
							| 
									
										
										
										
											2017-12-10 19:00:23 +08:00
										 |  |  | 		sets = append(sets, s) | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 04:15:47 +08:00
										 |  |  | 	set := storage.NewMergeSeriesSet(sets, storage.ChainedSeriesMerge) | 
					
						
							| 
									
										
										
										
											2020-02-10 07:58:23 +08:00
										 |  |  | 	it := storage.NewBuffer(int64(h.lookbackDelta / 1e6)) | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 	for set.Next() { | 
					
						
							|  |  |  | 		s := set.At() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// TODO(fabxc): allow fast path for most recent sample either
 | 
					
						
							|  |  |  | 		// in the storage itself or caching layer in Prometheus.
 | 
					
						
							| 
									
										
										
										
											2018-07-18 12:10:28 +08:00
										 |  |  | 		it.Reset(s.Iterator()) | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		var t int64 | 
					
						
							|  |  |  | 		var v float64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ok := it.Seek(maxt) | 
					
						
							|  |  |  | 		if ok { | 
					
						
							|  |  |  | 			t, v = it.Values() | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-05-24 00:36:35 +08:00
										 |  |  | 			t, v, ok = it.PeekBack(1) | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 			if !ok { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-24 01:03:57 +08:00
										 |  |  | 		// The exposition formats do not support stale markers, so drop them. This
 | 
					
						
							|  |  |  | 		// is good enough for staleness handling of federated data, as the
 | 
					
						
							|  |  |  | 		// interval-based limits on staleness will do the right thing for supported
 | 
					
						
							|  |  |  | 		// use cases (which is to say federating aggregated time series).
 | 
					
						
							|  |  |  | 		if value.IsStaleNaN(v) { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		vec = append(vec, promql.Sample{ | 
					
						
							|  |  |  | 			Metric: s.Labels(), | 
					
						
							|  |  |  | 			Point:  promql.Point{T: t, V: v}, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-10 00:57:31 +08:00
										 |  |  | 	if ws := set.Warnings(); len(ws) > 0 { | 
					
						
							|  |  |  | 		level.Debug(h.logger).Log("msg", "Federation select returned warnings", "warnings", ws) | 
					
						
							|  |  |  | 		federationWarnings.Add(float64(len(ws))) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 	if set.Err() != nil { | 
					
						
							|  |  |  | 		federationErrors.Inc() | 
					
						
							| 
									
										
										
										
											2019-05-03 21:11:28 +08:00
										 |  |  | 		http.Error(w, set.Err().Error(), http.StatusInternalServerError) | 
					
						
							| 
									
										
										
										
											2017-04-04 17:13:46 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sort.Sort(byName(vec)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-09 00:29:25 +08:00
										 |  |  | 	externalLabels := h.config.GlobalConfig.ExternalLabels.Map() | 
					
						
							| 
									
										
										
										
											2017-03-27 23:18:33 +08:00
										 |  |  | 	if _, ok := externalLabels[model.InstanceLabel]; !ok { | 
					
						
							|  |  |  | 		externalLabels[model.InstanceLabel] = "" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-09 00:29:25 +08:00
										 |  |  | 	externalLabelNames := make([]string, 0, len(externalLabels)) | 
					
						
							| 
									
										
										
										
											2017-03-27 23:18:33 +08:00
										 |  |  | 	for ln := range externalLabels { | 
					
						
							|  |  |  | 		externalLabelNames = append(externalLabelNames, ln) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-09 00:29:25 +08:00
										 |  |  | 	sort.Strings(externalLabelNames) | 
					
						
							| 
									
										
										
										
											2017-03-27 23:18:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		lastMetricName string | 
					
						
							|  |  |  | 		protMetricFam  *dto.MetricFamily | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	for _, s := range vec { | 
					
						
							|  |  |  | 		nameSeen := false | 
					
						
							|  |  |  | 		globalUsed := map[string]struct{}{} | 
					
						
							|  |  |  | 		protMetric := &dto.Metric{ | 
					
						
							|  |  |  | 			Untyped: &dto.Untyped{}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, l := range s.Metric { | 
					
						
							|  |  |  | 			if l.Value == "" { | 
					
						
							|  |  |  | 				// No value means unset. Never consider those labels.
 | 
					
						
							|  |  |  | 				// This is also important to protect against nameless metrics.
 | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			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() | 
					
						
							| 
									
										
										
										
											2017-08-12 02:45:52 +08:00
										 |  |  | 						level.Error(h.logger).Log("msg", "federation failed", "err", err) | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 						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), | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-03-09 00:29:25 +08:00
										 |  |  | 			if _, ok := externalLabels[l.Name]; ok { | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 				globalUsed[l.Name] = struct{}{} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !nameSeen { | 
					
						
							| 
									
										
										
										
											2017-08-12 02:45:52 +08:00
										 |  |  | 			level.Warn(h.logger).Log("msg", "Ignoring nameless metric during federation", "metric", s.Metric) | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// Attach global labels if they do not exist yet.
 | 
					
						
							| 
									
										
										
										
											2017-04-05 20:53:34 +08:00
										 |  |  | 		for _, ln := range externalLabelNames { | 
					
						
							|  |  |  | 			lv := externalLabels[ln] | 
					
						
							| 
									
										
										
										
											2020-04-07 23:42:42 +08:00
										 |  |  | 			if _, ok := globalUsed[ln]; !ok { | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 				protMetric.Label = append(protMetric.Label, &dto.LabelPair{ | 
					
						
							| 
									
										
										
										
											2020-04-07 23:42:42 +08:00
										 |  |  | 					Name:  proto.String(ln), | 
					
						
							|  |  |  | 					Value: proto.String(lv), | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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() | 
					
						
							| 
									
										
										
										
											2017-08-12 02:45:52 +08:00
										 |  |  | 			level.Error(h.logger).Log("msg", "federation failed", "err", err) | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-23 04:46:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-08 23:39:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // byName makes a model.Vector sortable by metric name.
 | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | type byName promql.Vector | 
					
						
							| 
									
										
										
										
											2016-09-08 23:39:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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 { | 
					
						
							| 
									
										
										
										
											2016-12-31 02:34:45 +08:00
										 |  |  | 	ni := vec[i].Metric.Get(labels.MetricName) | 
					
						
							|  |  |  | 	nj := vec[j].Metric.Get(labels.MetricName) | 
					
						
							| 
									
										
										
										
											2016-09-08 23:39:52 +08:00
										 |  |  | 	return ni < nj | 
					
						
							|  |  |  | } |