aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/prometheus/registry_opts.go
diff options
context:
space:
mode:
authorhcpp <hcpp@ydb.tech>2023-11-08 12:09:41 +0300
committerhcpp <hcpp@ydb.tech>2023-11-08 12:56:14 +0300
commita361f5b98b98b44ea510d274f6769164640dd5e1 (patch)
treec47c80962c6e2e7b06798238752fd3da0191a3f6 /library/go/core/metrics/prometheus/registry_opts.go
parent9478806fde1f4d40bd5a45e7cbe77237dab613e9 (diff)
downloadydb-a361f5b98b98b44ea510d274f6769164640dd5e1.tar.gz
metrics have been added
Diffstat (limited to 'library/go/core/metrics/prometheus/registry_opts.go')
-rw-r--r--library/go/core/metrics/prometheus/registry_opts.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/library/go/core/metrics/prometheus/registry_opts.go b/library/go/core/metrics/prometheus/registry_opts.go
new file mode 100644
index 0000000000..fedb019d85
--- /dev/null
+++ b/library/go/core/metrics/prometheus/registry_opts.go
@@ -0,0 +1,84 @@
+package prometheus
+
+import (
+ "context"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/ydb-platform/ydb/library/go/core/metrics"
+ "github.com/ydb-platform/ydb/library/go/core/metrics/collect"
+ "github.com/ydb-platform/ydb/library/go/core/metrics/internal/pkg/registryutil"
+)
+
+type RegistryOpts struct {
+ Prefix string
+ Tags map[string]string
+ rg *prometheus.Registry
+ Collectors []func(metrics.Registry)
+ NameSanitizer func(string) string
+}
+
+// NewRegistryOpts returns new initialized instance of RegistryOpts.
+func NewRegistryOpts() *RegistryOpts {
+ return &RegistryOpts{
+ Tags: make(map[string]string),
+ }
+}
+
+// SetTags overrides existing tags.
+func (o *RegistryOpts) SetTags(tags map[string]string) *RegistryOpts {
+ o.Tags = tags
+ return o
+}
+
+// AddTags merges given tags with existing.
+func (o *RegistryOpts) AddTags(tags map[string]string) *RegistryOpts {
+ for k, v := range tags {
+ o.Tags[k] = v
+ }
+ return o
+}
+
+// SetPrefix overrides existing prefix.
+func (o *RegistryOpts) SetPrefix(prefix string) *RegistryOpts {
+ o.Prefix = prefix
+ return o
+}
+
+// AppendPrefix adds given prefix as postfix to existing using separator.
+func (o *RegistryOpts) AppendPrefix(prefix string) *RegistryOpts {
+ o.Prefix = registryutil.BuildFQName("_", o.Prefix, prefix)
+ return o
+}
+
+// SetRegistry sets the given prometheus registry for further usage instead
+// of creating a new one.
+//
+// This is primarily used to unite externally defined metrics with metrics kept
+// in the core registry.
+func (o *RegistryOpts) SetRegistry(rg *prometheus.Registry) *RegistryOpts {
+ o.rg = rg
+ return o
+}
+
+// AddCollectors adds collectors that handle their metrics automatically (e.g. system metrics).
+func (o *RegistryOpts) AddCollectors(
+ ctx context.Context, c metrics.CollectPolicy, collectors ...collect.Func,
+) *RegistryOpts {
+ if len(collectors) == 0 {
+ return o
+ }
+
+ o.Collectors = append(o.Collectors, func(r metrics.Registry) {
+ for _, collector := range collectors {
+ collector(ctx, r, c)
+ }
+ })
+ return o
+}
+
+// SetNameSanitizer sets a functions which will be called for each metric's name.
+// It allows to alter names, for example to replace invalid characters
+func (o *RegistryOpts) SetNameSanitizer(v func(string) string) *RegistryOpts {
+ o.NameSanitizer = v
+ return o
+}