registry_opts.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package prometheus
  2. import (
  3. "context"
  4. "github.com/prometheus/client_golang/prometheus"
  5. "github.com/prometheus/common/expfmt"
  6. "github.com/ydb-platform/ydb/library/go/core/metrics"
  7. "github.com/ydb-platform/ydb/library/go/core/metrics/collect"
  8. "github.com/ydb-platform/ydb/library/go/core/metrics/internal/pkg/registryutil"
  9. )
  10. type PrometheusRegistry interface {
  11. prometheus.Registerer
  12. prometheus.Gatherer
  13. }
  14. type RegistryOpts struct {
  15. Prefix string
  16. Tags map[string]string
  17. rg PrometheusRegistry
  18. Collectors []func(metrics.Registry)
  19. NameSanitizer func(string) string
  20. StreamFormat expfmt.Format
  21. }
  22. // NewRegistryOpts returns new initialized instance of RegistryOpts.
  23. func NewRegistryOpts() *RegistryOpts {
  24. return &RegistryOpts{
  25. Tags: make(map[string]string),
  26. StreamFormat: StreamCompact,
  27. }
  28. }
  29. // SetTags overrides existing tags.
  30. func (o *RegistryOpts) SetTags(tags map[string]string) *RegistryOpts {
  31. o.Tags = tags
  32. return o
  33. }
  34. // AddTags merges given tags with existing.
  35. func (o *RegistryOpts) AddTags(tags map[string]string) *RegistryOpts {
  36. for k, v := range tags {
  37. o.Tags[k] = v
  38. }
  39. return o
  40. }
  41. // SetPrefix overrides existing prefix.
  42. func (o *RegistryOpts) SetPrefix(prefix string) *RegistryOpts {
  43. o.Prefix = prefix
  44. return o
  45. }
  46. // AppendPrefix adds given prefix as postfix to existing using separator.
  47. func (o *RegistryOpts) AppendPrefix(prefix string) *RegistryOpts {
  48. o.Prefix = registryutil.BuildFQName("_", o.Prefix, prefix)
  49. return o
  50. }
  51. // SetRegistry sets the given prometheus registry for further usage instead
  52. // of creating a new one.
  53. //
  54. // This is primarily used to unite externally defined metrics with metrics kept
  55. // in the core registry.
  56. func (o *RegistryOpts) SetRegistry(rg PrometheusRegistry) *RegistryOpts {
  57. o.rg = rg
  58. return o
  59. }
  60. // AddCollectors adds collectors that handle their metrics automatically (e.g. system metrics).
  61. func (o *RegistryOpts) AddCollectors(
  62. ctx context.Context, c metrics.CollectPolicy, collectors ...collect.Func,
  63. ) *RegistryOpts {
  64. if len(collectors) == 0 {
  65. return o
  66. }
  67. o.Collectors = append(o.Collectors, func(r metrics.Registry) {
  68. for _, collector := range collectors {
  69. collector(ctx, r, c)
  70. }
  71. })
  72. return o
  73. }
  74. // SetNameSanitizer sets a functions which will be called for each metric's name.
  75. // It allows to alter names, for example to replace invalid characters
  76. func (o *RegistryOpts) SetNameSanitizer(v func(string) string) *RegistryOpts {
  77. o.NameSanitizer = v
  78. return o
  79. }
  80. // SetStreamFormat sets default metrics stream format
  81. func (o *RegistryOpts) SetStreamFormat(format expfmt.Format) *RegistryOpts {
  82. o.StreamFormat = format
  83. return o
  84. }