You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

85 lines
2.8 KiB

  1. // Copyright 2022 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package promhttp
  14. import (
  15. "context"
  16. "github.com/prometheus/client_golang/prometheus"
  17. )
  18. // Option are used to configure both handler (middleware) or round tripper.
  19. type Option interface {
  20. apply(*options)
  21. }
  22. // LabelValueFromCtx are used to compute the label value from request context.
  23. // Context can be filled with values from request through middleware.
  24. type LabelValueFromCtx func(ctx context.Context) string
  25. // options store options for both a handler or round tripper.
  26. type options struct {
  27. extraMethods []string
  28. getExemplarFn func(requestCtx context.Context) prometheus.Labels
  29. extraLabelsFromCtx map[string]LabelValueFromCtx
  30. }
  31. func defaultOptions() *options {
  32. return &options{
  33. getExemplarFn: func(ctx context.Context) prometheus.Labels { return nil },
  34. extraLabelsFromCtx: map[string]LabelValueFromCtx{},
  35. }
  36. }
  37. func (o *options) emptyDynamicLabels() prometheus.Labels {
  38. labels := prometheus.Labels{}
  39. for label := range o.extraLabelsFromCtx {
  40. labels[label] = ""
  41. }
  42. return labels
  43. }
  44. type optionApplyFunc func(*options)
  45. func (o optionApplyFunc) apply(opt *options) { o(opt) }
  46. // WithExtraMethods adds additional HTTP methods to the list of allowed methods.
  47. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for the default list.
  48. //
  49. // See the example for ExampleInstrumentHandlerWithExtraMethods for example usage.
  50. func WithExtraMethods(methods ...string) Option {
  51. return optionApplyFunc(func(o *options) {
  52. o.extraMethods = methods
  53. })
  54. }
  55. // WithExemplarFromContext allows to inject function that will get exemplar from context that will be put to counter and histogram metrics.
  56. // If the function returns nil labels or the metric does not support exemplars, no exemplar will be added (noop), but
  57. // metric will continue to observe/increment.
  58. func WithExemplarFromContext(getExemplarFn func(requestCtx context.Context) prometheus.Labels) Option {
  59. return optionApplyFunc(func(o *options) {
  60. o.getExemplarFn = getExemplarFn
  61. })
  62. }
  63. // WithLabelFromCtx registers a label for dynamic resolution with access to context.
  64. // See the example for ExampleInstrumentHandlerWithLabelResolver for example usage
  65. func WithLabelFromCtx(name string, valueFn LabelValueFromCtx) Option {
  66. return optionApplyFunc(func(o *options) {
  67. o.extraLabelsFromCtx[name] = valueFn
  68. })
  69. }