aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/mock/registry.go
blob: 77f465f8ea0edd3314b40bb133425cd47eed1e6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package mock

import (
	"sync"

	"github.com/ydb-platform/ydb/library/go/core/metrics"
	"github.com/ydb-platform/ydb/library/go/core/metrics/internal/pkg/metricsutil"
	"github.com/ydb-platform/ydb/library/go/core/metrics/internal/pkg/registryutil"
	"go.uber.org/atomic"
)

var _ metrics.Registry = (*Registry)(nil)

type Registry struct {
	separator                  string
	prefix                     string
	tags                       map[string]string
	allowLoadRegisteredMetrics bool

	subregistries map[string]*Registry
	m             *sync.Mutex

	metrics *sync.Map
}

func NewRegistry(opts *RegistryOpts) *Registry {
	r := &Registry{
		separator: ".",

		subregistries: make(map[string]*Registry),
		m:             new(sync.Mutex),

		metrics: new(sync.Map),
	}

	if opts != nil {
		r.separator = string(opts.Separator)
		r.prefix = opts.Prefix
		r.tags = opts.Tags
		r.allowLoadRegisteredMetrics = opts.AllowLoadRegisteredMetrics
	}

	return r
}

// WithTags creates new sub-scope, where each metric has tags attached to it.
func (r Registry) WithTags(tags map[string]string) metrics.Registry {
	return r.newSubregistry(r.prefix, registryutil.MergeTags(r.tags, tags))
}

// WithPrefix creates new sub-scope, where each metric has prefix added to it name.
func (r Registry) WithPrefix(prefix string) metrics.Registry {
	return r.newSubregistry(registryutil.BuildFQName(r.separator, r.prefix, prefix), r.tags)
}

func (r Registry) ComposeName(parts ...string) string {
	return registryutil.BuildFQName(r.separator, parts...)
}

func (r Registry) Counter(name string) metrics.Counter {
	s := &Counter{
		Name:  r.newMetricName(name),
		Tags:  r.tags,
		Value: new(atomic.Int64),
	}

	key := registryutil.BuildRegistryKey(s.Name, r.tags)
	if val, loaded := r.metrics.LoadOrStore(key, s); loaded {
		if r.allowLoadRegisteredMetrics {
			return val.(*Counter)
		}
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r Registry) FuncCounter(name string, function func() int64) metrics.FuncCounter {
	metricName := r.newMetricName(name)
	key := registryutil.BuildRegistryKey(metricName, r.tags)
	s := FuncCounter{function: function}
	if _, loaded := r.metrics.LoadOrStore(key, s); loaded {
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r Registry) Gauge(name string) metrics.Gauge {
	s := &Gauge{
		Name:  r.newMetricName(name),
		Tags:  r.tags,
		Value: new(atomic.Float64),
	}

	key := registryutil.BuildRegistryKey(s.Name, r.tags)
	if val, loaded := r.metrics.LoadOrStore(key, s); loaded {
		if r.allowLoadRegisteredMetrics {
			return val.(*Gauge)
		}
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r Registry) FuncGauge(name string, function func() float64) metrics.FuncGauge {
	metricName := r.newMetricName(name)
	key := registryutil.BuildRegistryKey(metricName, r.tags)
	s := FuncGauge{function: function}
	if _, loaded := r.metrics.LoadOrStore(key, s); loaded {
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r *Registry) IntGauge(name string) metrics.IntGauge {
	s := &IntGauge{
		Name:  r.newMetricName(name),
		Tags:  r.tags,
		Value: new(atomic.Int64),
	}

	key := registryutil.BuildRegistryKey(s.Name, r.tags)
	if val, loaded := r.metrics.LoadOrStore(key, s); loaded {
		if r.allowLoadRegisteredMetrics {
			return val.(*IntGauge)
		}
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r *Registry) FuncIntGauge(name string, function func() int64) metrics.FuncIntGauge {
	metricName := r.newMetricName(name)
	key := registryutil.BuildRegistryKey(metricName, r.tags)
	s := FuncIntGauge{function: function}
	if _, loaded := r.metrics.LoadOrStore(key, s); loaded {
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r Registry) Timer(name string) metrics.Timer {
	s := &Timer{
		Name:  r.newMetricName(name),
		Tags:  r.tags,
		Value: new(atomic.Duration),
	}

	key := registryutil.BuildRegistryKey(s.Name, r.tags)
	if val, loaded := r.metrics.LoadOrStore(key, s); loaded {
		if r.allowLoadRegisteredMetrics {
			return val.(*Timer)
		}
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r Registry) Histogram(name string, buckets metrics.Buckets) metrics.Histogram {
	s := &Histogram{
		Name:         r.newMetricName(name),
		Tags:         r.tags,
		BucketBounds: metricsutil.BucketsBounds(buckets),
		BucketValues: make([]int64, buckets.Size()),
		InfValue:     new(atomic.Int64),
	}

	key := registryutil.BuildRegistryKey(s.Name, r.tags)
	if val, loaded := r.metrics.LoadOrStore(key, s); loaded {
		if r.allowLoadRegisteredMetrics {
			return val.(*Histogram)
		}
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r Registry) DurationHistogram(name string, buckets metrics.DurationBuckets) metrics.Timer {
	s := &Histogram{
		Name:         r.newMetricName(name),
		Tags:         r.tags,
		BucketBounds: metricsutil.DurationBucketsBounds(buckets),
		BucketValues: make([]int64, buckets.Size()),
		InfValue:     new(atomic.Int64),
	}

	key := registryutil.BuildRegistryKey(s.Name, r.tags)
	if val, loaded := r.metrics.LoadOrStore(key, s); loaded {
		if r.allowLoadRegisteredMetrics {
			return val.(*Histogram)
		}
		panic("metric with key " + key + " already registered")
	}
	return s
}

func (r *Registry) newSubregistry(prefix string, tags map[string]string) *Registry {
	registryKey := registryutil.BuildRegistryKey(prefix, tags)

	r.m.Lock()
	defer r.m.Unlock()

	if existing, ok := r.subregistries[registryKey]; ok {
		return existing
	}

	subregistry := &Registry{
		separator:                  r.separator,
		prefix:                     prefix,
		tags:                       tags,
		allowLoadRegisteredMetrics: r.allowLoadRegisteredMetrics,

		subregistries: r.subregistries,
		m:             r.m,

		metrics: r.metrics,
	}

	r.subregistries[registryKey] = subregistry
	return subregistry
}

func (r *Registry) newMetricName(name string) string {
	return registryutil.BuildFQName(r.separator, r.prefix, name)
}