2022-01-18 12:19:28 +03:00
|
|
|
// Copyright 2022 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
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2022-01-18 12:19:28 +03:00
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option are used to configure both handler (middleware) or round tripper.
|
|
|
|
type Option interface {
|
|
|
|
apply(*options)
|
|
|
|
}
|
|
|
|
|
2023-01-19 13:19:08 +03:00
|
|
|
// LabelValueFromCtx are used to compute the label value from request context.
|
|
|
|
// Context can be filled with values from request through middleware.
|
|
|
|
type LabelValueFromCtx func(ctx context.Context) string
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
// options store options for both a handler or round tripper.
|
|
|
|
type options struct {
|
2023-01-19 13:19:08 +03:00
|
|
|
extraMethods []string
|
|
|
|
getExemplarFn func(requestCtx context.Context) prometheus.Labels
|
|
|
|
extraLabelsFromCtx map[string]LabelValueFromCtx
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultOptions() *options {
|
2023-01-19 13:19:08 +03:00
|
|
|
return &options{
|
|
|
|
getExemplarFn: func(ctx context.Context) prometheus.Labels { return nil },
|
|
|
|
extraLabelsFromCtx: map[string]LabelValueFromCtx{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) emptyDynamicLabels() prometheus.Labels {
|
|
|
|
labels := prometheus.Labels{}
|
|
|
|
|
|
|
|
for label := range o.extraLabelsFromCtx {
|
|
|
|
labels[label] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return labels
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
type optionApplyFunc func(*options)
|
|
|
|
|
|
|
|
func (o optionApplyFunc) apply(opt *options) { o(opt) }
|
|
|
|
|
2022-01-18 12:19:28 +03:00
|
|
|
// WithExtraMethods adds additional HTTP methods to the list of allowed methods.
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for the default list.
|
|
|
|
//
|
|
|
|
// See the example for ExampleInstrumentHandlerWithExtraMethods for example usage.
|
|
|
|
func WithExtraMethods(methods ...string) Option {
|
2022-08-02 20:33:08 +03:00
|
|
|
return optionApplyFunc(func(o *options) {
|
2022-01-18 12:19:28 +03:00
|
|
|
o.extraMethods = methods
|
2022-08-02 20:33:08 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithExemplarFromContext adds allows to put a hook to all counter and histogram metrics.
|
|
|
|
// If the hook function returns non-nil labels, exemplars will be added for that request, otherwise metric
|
|
|
|
// will get instrumented without exemplar.
|
|
|
|
func WithExemplarFromContext(getExemplarFn func(requestCtx context.Context) prometheus.Labels) Option {
|
|
|
|
return optionApplyFunc(func(o *options) {
|
|
|
|
o.getExemplarFn = getExemplarFn
|
|
|
|
})
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
2023-01-19 13:19:08 +03:00
|
|
|
|
|
|
|
// WithLabelFromCtx registers a label for dynamic resolution with access to context.
|
|
|
|
// See the example for ExampleInstrumentHandlerWithLabelResolver for example usage
|
|
|
|
func WithLabelFromCtx(name string, valueFn LabelValueFromCtx) Option {
|
|
|
|
return optionApplyFunc(func(o *options) {
|
|
|
|
o.extraLabelsFromCtx[name] = valueFn
|
|
|
|
})
|
|
|
|
}
|