2017-04-24 22:13:19 +03:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
package promhttp
|
|
|
|
|
|
|
|
import (
|
2017-12-23 03:06:17 +03:00
|
|
|
"errors"
|
2017-04-24 22:13:19 +03:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2017-05-10 21:39:36 +03:00
|
|
|
// magicString is used for the hacky label test in checkLabels. Remove once fixed.
|
|
|
|
const magicString = "zZgWfBxLqvG8kc8IMv3POi2Bb0tZI3vAnBx+gBaFi9FyPzB/CzKUer1yufDa"
|
|
|
|
|
2022-08-22 18:31:08 +03:00
|
|
|
// observeWithExemplar is a wrapper for [prometheus.ExemplarAdder.ExemplarObserver],
|
|
|
|
// which falls back to [prometheus.Observer.Observe] if no labels are provided.
|
|
|
|
func observeWithExemplar(obs prometheus.Observer, val float64, labels map[string]string) {
|
2022-08-02 20:33:08 +03:00
|
|
|
if labels == nil {
|
|
|
|
obs.Observe(val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obs.(prometheus.ExemplarObserver).ObserveWithExemplar(val, labels)
|
|
|
|
}
|
|
|
|
|
2022-08-22 18:31:08 +03:00
|
|
|
// addWithExemplar is a wrapper for [prometheus.ExemplarAdder.AddWithExemplar],
|
|
|
|
// which falls back to [prometheus.Counter.Add] if no labels are provided.
|
|
|
|
func addWithExemplar(obs prometheus.Counter, val float64, labels map[string]string) {
|
2022-08-02 20:33:08 +03:00
|
|
|
if labels == nil {
|
|
|
|
obs.Add(val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obs.(prometheus.ExemplarAdder).AddWithExemplar(val, labels)
|
|
|
|
}
|
|
|
|
|
2017-04-24 22:13:19 +03:00
|
|
|
// InstrumentHandlerInFlight is a middleware that wraps the provided
|
|
|
|
// http.Handler. It sets the provided prometheus.Gauge to the number of
|
|
|
|
// requests currently handled by the wrapped http.Handler.
|
|
|
|
//
|
|
|
|
// See the example for InstrumentHandlerDuration for example usage.
|
|
|
|
func InstrumentHandlerInFlight(g prometheus.Gauge, next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
g.Inc()
|
|
|
|
defer g.Dec()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentHandlerDuration is a middleware that wraps the provided
|
|
|
|
// http.Handler to observe the request duration with the provided ObserverVec.
|
2020-12-09 19:58:53 +03:00
|
|
|
// The ObserverVec must have valid metric and label names and must have zero,
|
|
|
|
// one, or two non-const non-curried labels. For those, the only allowed label
|
2022-01-18 12:19:28 +03:00
|
|
|
// names are "code" and "method". The function panics otherwise. For the "method"
|
|
|
|
// label a predefined default label value set is used to filter given values.
|
|
|
|
// Values besides predefined values will count as `unknown` method.
|
2022-08-03 07:30:51 +03:00
|
|
|
// `WithExtraMethods` can be used to add more methods to the set. The Observe
|
2020-12-09 19:58:53 +03:00
|
|
|
// method of the Observer in the ObserverVec is called with the request duration
|
|
|
|
// in seconds. Partitioning happens by HTTP status code and/or HTTP method if
|
|
|
|
// the respective instance label names are present in the ObserverVec. For
|
|
|
|
// unpartitioned observations, use an ObserverVec with zero labels. Note that
|
|
|
|
// partitioning of Histograms is expensive and should be used judiciously.
|
2017-04-24 22:13:19 +03:00
|
|
|
//
|
|
|
|
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
|
|
|
|
//
|
|
|
|
// If the wrapped Handler panics, no values are reported.
|
2017-06-29 17:07:12 +03:00
|
|
|
//
|
|
|
|
// Note that this method is only guaranteed to never observe negative durations
|
|
|
|
// if used with Go1.9+.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.HandlerFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
hOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(hOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
// Curry the observer with dynamic labels before checking the remaining labels.
|
|
|
|
code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
|
2017-04-24 22:13:19 +03:00
|
|
|
|
|
|
|
if code {
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-04-24 22:13:19 +03:00
|
|
|
now := time.Now()
|
2017-05-29 12:42:43 +03:00
|
|
|
d := newDelegator(w, nil)
|
2017-04-24 22:13:19 +03:00
|
|
|
next.ServeHTTP(d, r)
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
observeWithExemplar(obs.With(l), time.Since(now).Seconds(), hOpts.getExemplarFn(r.Context()))
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-04-24 22:13:19 +03:00
|
|
|
now := time.Now()
|
|
|
|
next.ServeHTTP(w, r)
|
2023-01-19 13:19:08 +03:00
|
|
|
l := labels(code, method, r.Method, 0, hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
observeWithExemplar(obs.With(l), time.Since(now).Seconds(), hOpts.getExemplarFn(r.Context()))
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2017-12-23 03:06:17 +03:00
|
|
|
// InstrumentHandlerCounter is a middleware that wraps the provided http.Handler
|
2020-12-09 19:58:53 +03:00
|
|
|
// to observe the request result with the provided CounterVec. The CounterVec
|
|
|
|
// must have valid metric and label names and must have zero, one, or two
|
|
|
|
// non-const non-curried labels. For those, the only allowed label names are
|
2022-01-18 12:19:28 +03:00
|
|
|
// "code" and "method". The function panics otherwise. For the "method"
|
|
|
|
// label a predefined default label value set is used to filter given values.
|
|
|
|
// Values besides predefined values will count as `unknown` method.
|
|
|
|
// `WithExtraMethods` can be used to add more methods to the set. Partitioning of the
|
2020-12-09 19:58:53 +03:00
|
|
|
// CounterVec happens by HTTP status code and/or HTTP method if the respective
|
|
|
|
// instance label names are present in the CounterVec. For unpartitioned
|
|
|
|
// counting, use a CounterVec with zero labels.
|
2017-04-24 22:13:19 +03:00
|
|
|
//
|
|
|
|
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
|
|
|
|
//
|
|
|
|
// If the wrapped Handler panics, the Counter is not incremented.
|
|
|
|
//
|
|
|
|
// See the example for InstrumentHandlerDuration for example usage.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler, opts ...Option) http.HandlerFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
hOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(hOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
// Curry the counter with dynamic labels before checking the remaining labels.
|
|
|
|
code, method := checkLabels(counter.MustCurryWith(hOpts.emptyDynamicLabels()))
|
2017-04-24 22:13:19 +03:00
|
|
|
|
|
|
|
if code {
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-05-29 12:42:43 +03:00
|
|
|
d := newDelegator(w, nil)
|
2017-04-24 22:13:19 +03:00
|
|
|
next.ServeHTTP(d, r)
|
2022-08-02 20:33:08 +03:00
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
addWithExemplar(counter.With(l), 1, hOpts.getExemplarFn(r.Context()))
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-04-24 22:13:19 +03:00
|
|
|
next.ServeHTTP(w, r)
|
2023-01-19 13:19:08 +03:00
|
|
|
|
|
|
|
l := labels(code, method, r.Method, 0, hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
addWithExemplar(counter.With(l), 1, hOpts.getExemplarFn(r.Context()))
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2017-05-29 12:42:43 +03:00
|
|
|
// InstrumentHandlerTimeToWriteHeader is a middleware that wraps the provided
|
|
|
|
// http.Handler to observe with the provided ObserverVec the request duration
|
2020-12-09 19:58:53 +03:00
|
|
|
// until the response headers are written. The ObserverVec must have valid
|
|
|
|
// metric and label names and must have zero, one, or two non-const non-curried
|
|
|
|
// labels. For those, the only allowed label names are "code" and "method". The
|
2022-01-18 12:19:28 +03:00
|
|
|
// function panics otherwise. For the "method" label a predefined default label
|
|
|
|
// value set is used to filter given values. Values besides predefined values
|
|
|
|
// will count as `unknown` method.`WithExtraMethods` can be used to add more
|
|
|
|
// methods to the set. The Observe method of the Observer in the
|
2020-12-09 19:58:53 +03:00
|
|
|
// ObserverVec is called with the request duration in seconds. Partitioning
|
|
|
|
// happens by HTTP status code and/or HTTP method if the respective instance
|
|
|
|
// label names are present in the ObserverVec. For unpartitioned observations,
|
|
|
|
// use an ObserverVec with zero labels. Note that partitioning of Histograms is
|
|
|
|
// expensive and should be used judiciously.
|
2017-05-29 12:42:43 +03:00
|
|
|
//
|
|
|
|
// If the wrapped Handler panics before calling WriteHeader, no value is
|
|
|
|
// reported.
|
|
|
|
//
|
2017-06-29 17:07:12 +03:00
|
|
|
// Note that this method is only guaranteed to never observe negative durations
|
|
|
|
// if used with Go1.9+.
|
|
|
|
//
|
2017-05-29 12:42:43 +03:00
|
|
|
// See the example for InstrumentHandlerDuration for example usage.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentHandlerTimeToWriteHeader(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.HandlerFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
hOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(hOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
// Curry the observer with dynamic labels before checking the remaining labels.
|
|
|
|
code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
|
2017-05-29 12:42:43 +03:00
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-05-29 12:42:43 +03:00
|
|
|
now := time.Now()
|
|
|
|
d := newDelegator(w, func(status int) {
|
2023-01-19 13:19:08 +03:00
|
|
|
l := labels(code, method, r.Method, status, hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
observeWithExemplar(obs.With(l), time.Since(now).Seconds(), hOpts.getExemplarFn(r.Context()))
|
2017-05-29 12:42:43 +03:00
|
|
|
})
|
|
|
|
next.ServeHTTP(d, r)
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-05-29 12:42:43 +03:00
|
|
|
}
|
|
|
|
|
2017-04-24 22:13:19 +03:00
|
|
|
// InstrumentHandlerRequestSize is a middleware that wraps the provided
|
2020-12-09 19:58:53 +03:00
|
|
|
// http.Handler to observe the request size with the provided ObserverVec. The
|
|
|
|
// ObserverVec must have valid metric and label names and must have zero, one,
|
|
|
|
// or two non-const non-curried labels. For those, the only allowed label names
|
2022-01-18 12:19:28 +03:00
|
|
|
// are "code" and "method". The function panics otherwise. For the "method"
|
|
|
|
// label a predefined default label value set is used to filter given values.
|
|
|
|
// Values besides predefined values will count as `unknown` method.
|
|
|
|
// `WithExtraMethods` can be used to add more methods to the set. The Observe
|
|
|
|
// method of the Observer in the ObserverVec is called with the request size in
|
2020-12-09 19:58:53 +03:00
|
|
|
// bytes. Partitioning happens by HTTP status code and/or HTTP method if the
|
|
|
|
// respective instance label names are present in the ObserverVec. For
|
|
|
|
// unpartitioned observations, use an ObserverVec with zero labels. Note that
|
|
|
|
// partitioning of Histograms is expensive and should be used judiciously.
|
2017-04-24 22:13:19 +03:00
|
|
|
//
|
|
|
|
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
|
|
|
|
//
|
|
|
|
// If the wrapped Handler panics, no values are reported.
|
|
|
|
//
|
|
|
|
// See the example for InstrumentHandlerDuration for example usage.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentHandlerRequestSize(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.HandlerFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
hOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(hOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
// Curry the observer with dynamic labels before checking the remaining labels.
|
|
|
|
code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
|
|
|
|
|
2017-04-24 22:13:19 +03:00
|
|
|
if code {
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-05-29 12:42:43 +03:00
|
|
|
d := newDelegator(w, nil)
|
2017-04-24 22:13:19 +03:00
|
|
|
next.ServeHTTP(d, r)
|
|
|
|
size := computeApproximateRequestSize(r)
|
2023-01-19 13:19:08 +03:00
|
|
|
|
|
|
|
l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
observeWithExemplar(obs.With(l), float64(size), hOpts.getExemplarFn(r.Context()))
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-04-24 22:13:19 +03:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
size := computeApproximateRequestSize(r)
|
2023-01-19 13:19:08 +03:00
|
|
|
|
|
|
|
l := labels(code, method, r.Method, 0, hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
observeWithExemplar(obs.With(l), float64(size), hOpts.getExemplarFn(r.Context()))
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentHandlerResponseSize is a middleware that wraps the provided
|
2020-12-09 19:58:53 +03:00
|
|
|
// http.Handler to observe the response size with the provided ObserverVec. The
|
|
|
|
// ObserverVec must have valid metric and label names and must have zero, one,
|
|
|
|
// or two non-const non-curried labels. For those, the only allowed label names
|
2022-01-18 12:19:28 +03:00
|
|
|
// are "code" and "method". The function panics otherwise. For the "method"
|
|
|
|
// label a predefined default label value set is used to filter given values.
|
|
|
|
// Values besides predefined values will count as `unknown` method.
|
|
|
|
// `WithExtraMethods` can be used to add more methods to the set. The Observe
|
|
|
|
// method of the Observer in the ObserverVec is called with the response size in
|
2020-12-09 19:58:53 +03:00
|
|
|
// bytes. Partitioning happens by HTTP status code and/or HTTP method if the
|
|
|
|
// respective instance label names are present in the ObserverVec. For
|
|
|
|
// unpartitioned observations, use an ObserverVec with zero labels. Note that
|
|
|
|
// partitioning of Histograms is expensive and should be used judiciously.
|
2017-04-24 22:13:19 +03:00
|
|
|
//
|
|
|
|
// If the wrapped Handler does not set a status code, a status code of 200 is assumed.
|
|
|
|
//
|
|
|
|
// If the wrapped Handler panics, no values are reported.
|
|
|
|
//
|
|
|
|
// See the example for InstrumentHandlerDuration for example usage.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentHandlerResponseSize(obs prometheus.ObserverVec, next http.Handler, opts ...Option) http.Handler {
|
2022-08-02 20:33:08 +03:00
|
|
|
hOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(hOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
// Curry the observer with dynamic labels before checking the remaining labels.
|
|
|
|
code, method := checkLabels(obs.MustCurryWith(hOpts.emptyDynamicLabels()))
|
2022-01-18 12:19:28 +03:00
|
|
|
|
2017-04-24 22:13:19 +03:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-05-29 12:42:43 +03:00
|
|
|
d := newDelegator(w, nil)
|
2017-04-24 22:13:19 +03:00
|
|
|
next.ServeHTTP(d, r)
|
2023-01-19 13:19:08 +03:00
|
|
|
|
|
|
|
l := labels(code, method, r.Method, d.Status(), hOpts.extraMethods...)
|
|
|
|
for label, resolve := range hOpts.extraLabelsFromCtx {
|
|
|
|
l[label] = resolve(r.Context())
|
|
|
|
}
|
|
|
|
observeWithExemplar(obs.With(l), float64(d.Written()), hOpts.getExemplarFn(r.Context()))
|
2017-04-24 22:13:19 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:58:53 +03:00
|
|
|
// checkLabels returns whether the provided Collector has a non-const,
|
|
|
|
// non-curried label named "code" and/or "method". It panics if the provided
|
|
|
|
// Collector does not have a Desc or has more than one Desc or its Desc is
|
|
|
|
// invalid. It also panics if the Collector has any non-const, non-curried
|
|
|
|
// labels that are not named "code" or "method".
|
2022-06-17 10:04:06 +03:00
|
|
|
func checkLabels(c prometheus.Collector) (code, method bool) {
|
2017-04-24 22:13:19 +03:00
|
|
|
// TODO(beorn7): Remove this hacky way to check for instance labels
|
|
|
|
// once Descriptors can have their dimensionality queried.
|
|
|
|
var (
|
|
|
|
desc *prometheus.Desc
|
2017-12-23 03:06:17 +03:00
|
|
|
m prometheus.Metric
|
2017-04-24 22:13:19 +03:00
|
|
|
pm dto.Metric
|
2017-12-23 03:06:17 +03:00
|
|
|
lvs []string
|
2017-04-24 22:13:19 +03:00
|
|
|
)
|
|
|
|
|
2017-12-23 03:06:17 +03:00
|
|
|
// Get the Desc from the Collector.
|
2017-04-24 22:13:19 +03:00
|
|
|
descc := make(chan *prometheus.Desc, 1)
|
|
|
|
c.Describe(descc)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case desc = <-descc:
|
|
|
|
default:
|
|
|
|
panic("no description provided by collector")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-descc:
|
|
|
|
panic("more than one description provided by collector")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
close(descc)
|
|
|
|
|
2020-12-09 19:58:53 +03:00
|
|
|
// Make sure the Collector has a valid Desc by registering it with a
|
|
|
|
// temporary registry.
|
|
|
|
prometheus.NewRegistry().MustRegister(c)
|
|
|
|
|
2017-12-23 03:06:17 +03:00
|
|
|
// Create a ConstMetric with the Desc. Since we don't know how many
|
|
|
|
// variable labels there are, try for as long as it needs.
|
|
|
|
for err := errors.New("dummy"); err != nil; lvs = append(lvs, magicString) {
|
|
|
|
m, err = prometheus.NewConstMetric(desc, prometheus.UntypedValue, 0, lvs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the metric into a proto message and look at the labels.
|
|
|
|
// If the value is not the magicString, it is a constLabel, which doesn't interest us.
|
|
|
|
// If the label is curried, it doesn't interest us.
|
|
|
|
// In all other cases, only "code" or "method" is allowed.
|
|
|
|
if err := m.Write(&pm); err != nil {
|
|
|
|
panic("error checking metric for labels")
|
2017-05-10 21:39:36 +03:00
|
|
|
}
|
2017-12-23 03:06:17 +03:00
|
|
|
for _, label := range pm.Label {
|
|
|
|
name, value := label.GetName(), label.GetValue()
|
|
|
|
if value != magicString || isLabelCurried(c, name) {
|
|
|
|
continue
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
2017-12-23 03:06:17 +03:00
|
|
|
switch name {
|
|
|
|
case "code":
|
|
|
|
code = true
|
|
|
|
case "method":
|
|
|
|
method = true
|
|
|
|
default:
|
|
|
|
panic("metric partitioned with non-supported labels")
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
2017-05-10 21:39:36 +03:00
|
|
|
}
|
2017-12-23 03:06:17 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLabelCurried(c prometheus.Collector, label string) bool {
|
|
|
|
// This is even hackier than the label test above.
|
|
|
|
// We essentially try to curry again and see if it works.
|
|
|
|
// But for that, we need to type-convert to the two
|
|
|
|
// types we use here, ObserverVec or *CounterVec.
|
|
|
|
switch v := c.(type) {
|
|
|
|
case *prometheus.CounterVec:
|
|
|
|
if _, err := v.CurryWith(prometheus.Labels{label: "dummy"}); err == nil {
|
|
|
|
return false
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
2017-12-23 03:06:17 +03:00
|
|
|
case prometheus.ObserverVec:
|
|
|
|
if _, err := v.CurryWith(prometheus.Labels{label: "dummy"}); err == nil {
|
|
|
|
return false
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
2017-12-23 03:06:17 +03:00
|
|
|
default:
|
|
|
|
panic("unsupported metric vec type")
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
2017-12-23 03:06:17 +03:00
|
|
|
return true
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// emptyLabels is a one-time allocation for non-partitioned metrics to avoid
|
|
|
|
// unnecessary allocations on each request.
|
|
|
|
var emptyLabels = prometheus.Labels{}
|
|
|
|
|
2022-01-18 12:19:28 +03:00
|
|
|
func labels(code, method bool, reqMethod string, status int, extraMethods ...string) prometheus.Labels {
|
2017-04-24 22:13:19 +03:00
|
|
|
if !(code || method) {
|
|
|
|
return emptyLabels
|
|
|
|
}
|
|
|
|
labels := prometheus.Labels{}
|
|
|
|
|
|
|
|
if code {
|
|
|
|
labels["code"] = sanitizeCode(status)
|
|
|
|
}
|
|
|
|
if method {
|
2022-01-18 12:19:28 +03:00
|
|
|
labels["method"] = sanitizeMethod(reqMethod, extraMethods...)
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
func computeApproximateRequestSize(r *http.Request) int {
|
|
|
|
s := 0
|
|
|
|
if r.URL != nil {
|
|
|
|
s += len(r.URL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
s += len(r.Method)
|
|
|
|
s += len(r.Proto)
|
|
|
|
for name, values := range r.Header {
|
|
|
|
s += len(name)
|
|
|
|
for _, value := range values {
|
|
|
|
s += len(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s += len(r.Host)
|
|
|
|
|
|
|
|
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
|
|
|
|
|
|
|
|
if r.ContentLength != -1 {
|
|
|
|
s += int(r.ContentLength)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-01-18 12:19:28 +03:00
|
|
|
// If the wrapped http.Handler has a known method, it will be sanitized and returned.
|
|
|
|
// Otherwise, "unknown" will be returned. The known method list can be extended
|
|
|
|
// as needed by using extraMethods parameter.
|
|
|
|
func sanitizeMethod(m string, extraMethods ...string) string {
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for
|
|
|
|
// the methods chosen as default.
|
2017-04-24 22:13:19 +03:00
|
|
|
switch m {
|
|
|
|
case "GET", "get":
|
|
|
|
return "get"
|
|
|
|
case "PUT", "put":
|
|
|
|
return "put"
|
|
|
|
case "HEAD", "head":
|
|
|
|
return "head"
|
|
|
|
case "POST", "post":
|
|
|
|
return "post"
|
|
|
|
case "DELETE", "delete":
|
|
|
|
return "delete"
|
|
|
|
case "CONNECT", "connect":
|
|
|
|
return "connect"
|
|
|
|
case "OPTIONS", "options":
|
|
|
|
return "options"
|
|
|
|
case "NOTIFY", "notify":
|
|
|
|
return "notify"
|
2022-01-18 12:19:28 +03:00
|
|
|
case "TRACE", "trace":
|
|
|
|
return "trace"
|
|
|
|
case "PATCH", "patch":
|
|
|
|
return "patch"
|
2017-04-24 22:13:19 +03:00
|
|
|
default:
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, method := range extraMethods {
|
|
|
|
if strings.EqualFold(m, method) {
|
|
|
|
return strings.ToLower(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unknown"
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the wrapped http.Handler has not set a status code, i.e. the value is
|
2022-01-18 12:19:28 +03:00
|
|
|
// currently 0, sanitizeCode will return 200, for consistency with behavior in
|
2017-04-24 22:13:19 +03:00
|
|
|
// the stdlib.
|
|
|
|
func sanitizeCode(s int) string {
|
2022-01-18 12:19:28 +03:00
|
|
|
// See for accepted codes https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
2017-04-24 22:13:19 +03:00
|
|
|
switch s {
|
|
|
|
case 100:
|
|
|
|
return "100"
|
|
|
|
case 101:
|
|
|
|
return "101"
|
|
|
|
|
|
|
|
case 200, 0:
|
|
|
|
return "200"
|
|
|
|
case 201:
|
|
|
|
return "201"
|
|
|
|
case 202:
|
|
|
|
return "202"
|
|
|
|
case 203:
|
|
|
|
return "203"
|
|
|
|
case 204:
|
|
|
|
return "204"
|
|
|
|
case 205:
|
|
|
|
return "205"
|
|
|
|
case 206:
|
|
|
|
return "206"
|
|
|
|
|
|
|
|
case 300:
|
|
|
|
return "300"
|
|
|
|
case 301:
|
|
|
|
return "301"
|
|
|
|
case 302:
|
|
|
|
return "302"
|
|
|
|
case 304:
|
|
|
|
return "304"
|
|
|
|
case 305:
|
|
|
|
return "305"
|
|
|
|
case 307:
|
|
|
|
return "307"
|
|
|
|
|
|
|
|
case 400:
|
|
|
|
return "400"
|
|
|
|
case 401:
|
|
|
|
return "401"
|
|
|
|
case 402:
|
|
|
|
return "402"
|
|
|
|
case 403:
|
|
|
|
return "403"
|
|
|
|
case 404:
|
|
|
|
return "404"
|
|
|
|
case 405:
|
|
|
|
return "405"
|
|
|
|
case 406:
|
|
|
|
return "406"
|
|
|
|
case 407:
|
|
|
|
return "407"
|
|
|
|
case 408:
|
|
|
|
return "408"
|
|
|
|
case 409:
|
|
|
|
return "409"
|
|
|
|
case 410:
|
|
|
|
return "410"
|
|
|
|
case 411:
|
|
|
|
return "411"
|
|
|
|
case 412:
|
|
|
|
return "412"
|
|
|
|
case 413:
|
|
|
|
return "413"
|
|
|
|
case 414:
|
|
|
|
return "414"
|
|
|
|
case 415:
|
|
|
|
return "415"
|
|
|
|
case 416:
|
|
|
|
return "416"
|
|
|
|
case 417:
|
|
|
|
return "417"
|
|
|
|
case 418:
|
|
|
|
return "418"
|
|
|
|
|
|
|
|
case 500:
|
|
|
|
return "500"
|
|
|
|
case 501:
|
|
|
|
return "501"
|
|
|
|
case 502:
|
|
|
|
return "502"
|
|
|
|
case 503:
|
|
|
|
return "503"
|
|
|
|
case 504:
|
|
|
|
return "504"
|
|
|
|
case 505:
|
|
|
|
return "505"
|
|
|
|
|
|
|
|
case 428:
|
|
|
|
return "428"
|
|
|
|
case 429:
|
|
|
|
return "429"
|
|
|
|
case 431:
|
|
|
|
return "431"
|
|
|
|
case 511:
|
|
|
|
return "511"
|
|
|
|
|
|
|
|
default:
|
2022-01-18 12:19:28 +03:00
|
|
|
if s >= 100 && s <= 599 {
|
|
|
|
return strconv.Itoa(s)
|
|
|
|
}
|
|
|
|
return "unknown"
|
2017-04-24 22:13:19 +03:00
|
|
|
}
|
|
|
|
}
|