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.
 
 

59 regels
2.0 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. // options store options for both a handler or round tripper.
  23. type options struct {
  24. extraMethods []string
  25. getExemplarFn func(requestCtx context.Context) prometheus.Labels
  26. }
  27. func defaultOptions() *options {
  28. return &options{getExemplarFn: func(ctx context.Context) prometheus.Labels { return nil }}
  29. }
  30. type optionApplyFunc func(*options)
  31. func (o optionApplyFunc) apply(opt *options) { o(opt) }
  32. // WithExtraMethods adds additional HTTP methods to the list of allowed methods.
  33. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods for the default list.
  34. //
  35. // See the example for ExampleInstrumentHandlerWithExtraMethods for example usage.
  36. func WithExtraMethods(methods ...string) Option {
  37. return optionApplyFunc(func(o *options) {
  38. o.extraMethods = methods
  39. })
  40. }
  41. // WithExemplarFromContext adds allows to put a hook to all counter and histogram metrics.
  42. // If the hook function returns non-nil labels, exemplars will be added for that request, otherwise metric
  43. // will get instrumented without exemplar.
  44. func WithExemplarFromContext(getExemplarFn func(requestCtx context.Context) prometheus.Labels) Option {
  45. return optionApplyFunc(func(o *options) {
  46. o.getExemplarFn = getExemplarFn
  47. })
  48. }