Skip to content

Commit 3573b9b

Browse files
committed
Create unique types for service/scope/metric indexes
Surprisingly easy to change all these actually, I should've done this a long time ago. Unsurprisingly we were *almost* perfect in our use, but not completely.
1 parent 505ecb5 commit 3573b9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+196
-186
lines changed

common/archiver/s3store/historyArchiver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s *historyArchiverSuite) SetupTest() {
9999
s.Assertions = require.New(s.T())
100100
s.container = &archiver.HistoryBootstrapContainer{
101101
Logger: testlogger.New(s.T()),
102-
MetricsClient: metrics.NewClient(scope, metrics.HistoryArchiverScope),
102+
MetricsClient: metrics.NewClient(scope, metrics.History), // clear misuse
103103
}
104104
}
105105

common/archiver/s3store/visibilityArchiver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (s *visibilityArchiverSuite) SetupSuite() {
124124

125125
s.container = &archiver.VisibilityBootstrapContainer{
126126
Logger: testlogger.New(s.T()),
127-
MetricsClient: metrics.NewClient(scope, metrics.VisibilityArchiverScope),
127+
MetricsClient: metrics.NewClient(scope, metrics.History),
128128
}
129129
s.setupVisibilityDirectory()
130130
}

common/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ type GetCacheItemSizeFunc func(interface{}) uint64
156156
// DomainMetricsScopeCache represents a interface for mapping domainID and scopeIdx to metricsScope
157157
type DomainMetricsScopeCache interface {
158158
// Get retrieves metrics scope for a domainID and scopeIdx
159-
Get(domainID string, scopeIdx int) (metrics.Scope, bool)
159+
Get(domainID string, scopeIdx metrics.ScopeIdx) (metrics.Scope, bool)
160160
// Put adds metrics scope for a domainID and scopeIdx
161-
Put(domainID string, scopeIdx int, metricsScope metrics.Scope)
161+
Put(domainID string, scopeIdx metrics.ScopeIdx, metricsScope metrics.Scope)
162162

163163
common.Daemon
164164
}

common/cache/interface_mock.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/cache/metricsScopeCache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
const flushBufferedMetricsScopeDuration = 10 * time.Second
3636

3737
type (
38-
metricsScopeMap map[string]map[int]metrics.Scope
38+
metricsScopeMap map[string]map[metrics.ScopeIdx]metrics.Scope
3939

4040
buffer struct {
4141
sync.RWMutex
@@ -81,7 +81,7 @@ func (c *domainMetricsScopeCache) flushBufferedMetricsScope(flushDuration time.D
8181
data := c.cache.Load().(metricsScopeMap)
8282
// Copy everything over after atomic load
8383
for key, val := range data {
84-
scopeMap[key] = map[int]metrics.Scope{}
84+
scopeMap[key] = map[metrics.ScopeIdx]metrics.Scope{}
8585
for k, v := range val {
8686
scopeMap[key][k] = v
8787
}
@@ -90,7 +90,7 @@ func (c *domainMetricsScopeCache) flushBufferedMetricsScope(flushDuration time.D
9090
// Copy from buffered array
9191
for key, val := range c.buffer.bufferMap {
9292
if _, ok := scopeMap[key]; !ok {
93-
scopeMap[key] = map[int]metrics.Scope{}
93+
scopeMap[key] = map[metrics.ScopeIdx]metrics.Scope{}
9494
}
9595
for k, v := range val {
9696
scopeMap[key][k] = v
@@ -109,7 +109,7 @@ func (c *domainMetricsScopeCache) flushBufferedMetricsScope(flushDuration time.D
109109
}
110110

111111
// Get retrieves scope for domainID and scopeIdx
112-
func (c *domainMetricsScopeCache) Get(domainID string, scopeIdx int) (metrics.Scope, bool) {
112+
func (c *domainMetricsScopeCache) Get(domainID string, scopeIdx metrics.ScopeIdx) (metrics.Scope, bool) {
113113
data := c.cache.Load().(metricsScopeMap)
114114

115115
if data == nil {
@@ -126,12 +126,12 @@ func (c *domainMetricsScopeCache) Get(domainID string, scopeIdx int) (metrics.Sc
126126
}
127127

128128
// Put puts map of domainID and scopeIdx to metricsScope
129-
func (c *domainMetricsScopeCache) Put(domainID string, scopeIdx int, scope metrics.Scope) {
129+
func (c *domainMetricsScopeCache) Put(domainID string, scopeIdx metrics.ScopeIdx, scope metrics.Scope) {
130130
c.buffer.Lock()
131131
defer c.buffer.Unlock()
132132

133133
if c.buffer.bufferMap[domainID] == nil {
134-
c.buffer.bufferMap[domainID] = map[int]metrics.Scope{}
134+
c.buffer.bufferMap[domainID] = map[metrics.ScopeIdx]metrics.Scope{}
135135
}
136136
c.buffer.bufferMap[domainID][scopeIdx] = scope
137137
}

common/cache/metricsScopeCache_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *domainMetricsCacheSuite) TestGetMetricsScope() {
6666
var found bool
6767

6868
tests := []struct {
69-
scopeID int
69+
scopeID metrics.ScopeIdx
7070
domainID string
7171
}{
7272
{1, "A"},
@@ -104,7 +104,7 @@ func (s *domainMetricsCacheSuite) TestGetMetricsScopeMultipleFlushLoop() {
104104
var found bool
105105

106106
tests := []struct {
107-
scopeID int
107+
scopeID metrics.ScopeIdx
108108
domainID string
109109
}{
110110
{1, "A"},
@@ -174,14 +174,14 @@ func (s *domainMetricsCacheSuite) TestConcurrentMetricsScopeAccess() {
174174
for i := 0; i < 1000; i++ {
175175
wg.Add(1)
176176
// concurrent get and put
177-
go func(scopeIdx int) {
177+
go func(scopeIdx metrics.ScopeIdx) {
178178
defer wg.Done()
179179

180180
<-ch
181181

182182
s.metricsCache.Get("test_domain", scopeIdx)
183-
s.metricsCache.Put("test_domain", scopeIdx, s.metricsClient.Scope(scopeIdx%metrics.NumServices))
184-
}(i)
183+
s.metricsCache.Put("test_domain", scopeIdx, s.metricsClient.Scope(metrics.ScopeIdx(int(scopeIdx)%int(metrics.NumServices))))
184+
}(metrics.ScopeIdx(i))
185185
}
186186

187187
close(ch)
@@ -190,8 +190,8 @@ func (s *domainMetricsCacheSuite) TestConcurrentMetricsScopeAccess() {
190190
time.Sleep(120 * time.Millisecond)
191191

192192
for i := 0; i < 1000; i++ {
193-
metricsScope, found = s.metricsCache.Get("test_domain", i)
194-
testMetricsScope = s.metricsClient.Scope(i % metrics.NumServices)
193+
metricsScope, found = s.metricsCache.Get("test_domain", metrics.ScopeIdx(i))
194+
testMetricsScope = s.metricsClient.Scope(metrics.ScopeIdx(i % int(metrics.NumServices)))
195195

196196
s.Equal(true, found)
197197
s.Equal(testMetricsScope, metricsScope)

common/metrics/client.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
type ClientImpl struct {
3131
// parentReporter is the parent scope for the metrics
3232
parentScope tally.Scope
33-
childScopes map[int]tally.Scope
34-
metricDefs map[int]metricDefinition
33+
childScopes map[ScopeIdx]tally.Scope
34+
metricDefs map[MetricIdx]metricDefinition
3535
serviceIdx ServiceIdx
3636
}
3737

@@ -43,7 +43,7 @@ func NewClient(scope tally.Scope, serviceIdx ServiceIdx) Client {
4343
totalScopes := len(ScopeDefs[Common]) + len(ScopeDefs[serviceIdx])
4444
metricsClient := &ClientImpl{
4545
parentScope: scope,
46-
childScopes: make(map[int]tally.Scope, totalScopes),
46+
childScopes: make(map[ScopeIdx]tally.Scope, totalScopes),
4747
metricDefs: getMetricDefs(serviceIdx),
4848
serviceIdx: serviceIdx,
4949
}
@@ -69,60 +69,60 @@ func NewClient(scope tally.Scope, serviceIdx ServiceIdx) Client {
6969

7070
// IncCounter increments one for a counter and emits
7171
// to metrics backend
72-
func (m *ClientImpl) IncCounter(scopeIdx int, counterIdx int) {
72+
func (m *ClientImpl) IncCounter(scope ScopeIdx, counterIdx MetricIdx) {
7373
name := string(m.metricDefs[counterIdx].metricName)
74-
m.childScopes[scopeIdx].Counter(name).Inc(1)
74+
m.childScopes[scope].Counter(name).Inc(1)
7575
}
7676

7777
// AddCounter adds delta to the counter and
7878
// emits to the metrics backend
79-
func (m *ClientImpl) AddCounter(scopeIdx int, counterIdx int, delta int64) {
79+
func (m *ClientImpl) AddCounter(scope ScopeIdx, counterIdx MetricIdx, delta int64) {
8080
name := string(m.metricDefs[counterIdx].metricName)
81-
m.childScopes[scopeIdx].Counter(name).Inc(delta)
81+
m.childScopes[scope].Counter(name).Inc(delta)
8282
}
8383

8484
// StartTimer starts a timer for the given
8585
// metric name
86-
func (m *ClientImpl) StartTimer(scopeIdx int, timerIdx int) tally.Stopwatch {
86+
func (m *ClientImpl) StartTimer(scope ScopeIdx, timerIdx MetricIdx) tally.Stopwatch {
8787
name := string(m.metricDefs[timerIdx].metricName)
88-
return m.childScopes[scopeIdx].Timer(name).Start()
88+
return m.childScopes[scope].Timer(name).Start()
8989
}
9090

9191
// RecordTimer record and emit a timer for the given
9292
// metric name
93-
func (m *ClientImpl) RecordTimer(scopeIdx int, timerIdx int, d time.Duration) {
93+
func (m *ClientImpl) RecordTimer(scope ScopeIdx, timerIdx MetricIdx, d time.Duration) {
9494
name := string(m.metricDefs[timerIdx].metricName)
95-
m.childScopes[scopeIdx].Timer(name).Record(d)
95+
m.childScopes[scope].Timer(name).Record(d)
9696
}
9797

9898
// RecordHistogramDuration record and emit a duration
99-
func (m *ClientImpl) RecordHistogramDuration(scopeIdx int, timerIdx int, d time.Duration) {
99+
func (m *ClientImpl) RecordHistogramDuration(scope ScopeIdx, timerIdx MetricIdx, d time.Duration) {
100100
name := string(m.metricDefs[timerIdx].metricName)
101-
m.childScopes[scopeIdx].Histogram(name, m.getBuckets(timerIdx)).RecordDuration(d)
101+
m.childScopes[scope].Histogram(name, m.getBuckets(timerIdx)).RecordDuration(d)
102102
}
103103

104104
// UpdateGauge reports Gauge type metric
105-
func (m *ClientImpl) UpdateGauge(scopeIdx int, gaugeIdx int, value float64) {
105+
func (m *ClientImpl) UpdateGauge(scopeIdx ScopeIdx, gaugeIdx MetricIdx, value float64) {
106106
name := string(m.metricDefs[gaugeIdx].metricName)
107107
m.childScopes[scopeIdx].Gauge(name).Update(value)
108108
}
109109

110110
// Scope return a new internal metrics scope that can be used to add additional
111111
// information to the metrics emitted
112-
func (m *ClientImpl) Scope(scopeIdx int, tags ...Tag) Scope {
113-
scope := m.childScopes[scopeIdx]
114-
return newMetricsScope(scope, scope, m.metricDefs, false).Tagged(tags...)
112+
func (m *ClientImpl) Scope(scope ScopeIdx, tags ...Tag) Scope {
113+
sc := m.childScopes[scope]
114+
return newMetricsScope(sc, sc, m.metricDefs, false).Tagged(tags...)
115115
}
116116

117-
func (m *ClientImpl) getBuckets(id int) tally.Buckets {
117+
func (m *ClientImpl) getBuckets(id MetricIdx) tally.Buckets {
118118
if m.metricDefs[id].buckets != nil {
119119
return m.metricDefs[id].buckets
120120
}
121121
return tally.DefaultBuckets
122122
}
123123

124-
func getMetricDefs(serviceIdx ServiceIdx) map[int]metricDefinition {
125-
defs := make(map[int]metricDefinition)
124+
func getMetricDefs(serviceIdx ServiceIdx) map[MetricIdx]metricDefinition {
125+
defs := make(map[MetricIdx]metricDefinition)
126126
for idx, def := range MetricDefs[Common] {
127127
defs[idx] = def
128128
}

common/metrics/defs.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type (
5151

5252
// ServiceIdx is an index that uniquely identifies the service
5353
ServiceIdx int
54+
55+
// ScopeIdx is an index that uniquely identifies an operation, which is required to form a new metrics scope
56+
ScopeIdx int
57+
58+
// MetricIdx is an index that uniquely identifies the metric definition
59+
MetricIdx int
5460
)
5561

5662
func (s scopeDefinition) GetOperationString() string {
@@ -67,12 +73,13 @@ const (
6773

6874
// Service names for all services that emit metrics.
6975
const (
70-
Common = iota
76+
Common ServiceIdx = iota
7177
Frontend
7278
History
7379
Matching
7480
Worker
7581
ShardDistributor
82+
7683
NumServices
7784
)
7885

@@ -143,7 +150,7 @@ const (
143150
// -- Common Operation scopes --
144151

145152
// PersistenceCreateShardScope tracks CreateShard calls made by service to persistence layer
146-
PersistenceCreateShardScope = iota
153+
PersistenceCreateShardScope ScopeIdx = iota
147154
// PersistenceGetShardScope tracks GetShard calls made by service to persistence layer
148155
PersistenceGetShardScope
149156
// PersistenceUpdateShardScope tracks UpdateShard calls made by service to persistence layer
@@ -1466,7 +1473,7 @@ const (
14661473
)
14671474

14681475
// ScopeDefs record the scopes for all services
1469-
var ScopeDefs = map[ServiceIdx]map[int]scopeDefinition{
1476+
var ScopeDefs = map[ServiceIdx]map[ScopeIdx]scopeDefinition{
14701477
// common scope Names
14711478
Common: {
14721479
PersistenceCreateShardScope: {operation: "CreateShard"},
@@ -2141,7 +2148,7 @@ var ScopeDefs = map[ServiceIdx]map[int]scopeDefinition{
21412148

21422149
// Common Metrics enum
21432150
const (
2144-
CadenceRequests = iota
2151+
CadenceRequests MetricIdx = iota
21452152
CadenceFailures
21462153
CadenceLatency
21472154
CadenceErrBadRequestCounter
@@ -2916,7 +2923,7 @@ const (
29162923
)
29172924

29182925
// MetricDefs record the metrics for all services
2919-
var MetricDefs = map[ServiceIdx]map[int]metricDefinition{
2926+
var MetricDefs = map[ServiceIdx]map[MetricIdx]metricDefinition{
29202927
Common: {
29212928
CadenceRequests: {metricName: "cadence_requests", metricType: Counter},
29222929
CadenceFailures: {metricName: "cadence_errors", metricType: Counter},

common/metrics/defs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestMetricDefs(t *testing.T) {
134134
// Duplicate indexes with the same operation name are technically fine, but there doesn't seem to be any benefit in allowing it,
135135
// and it trivially ensures that all values have only one operation name.
136136
func TestOperationIndexesAreUnique(t *testing.T) {
137-
seen := make(map[int]bool)
137+
seen := make(map[ScopeIdx]bool)
138138
for serviceIdx, serviceOps := range ScopeDefs {
139139
for idx := range serviceOps {
140140
if seen[idx] {
@@ -158,7 +158,7 @@ func TestOperationIndexesAreUnique(t *testing.T) {
158158
func TestMetricsAreUnique(t *testing.T) {
159159
// Duplicate indexes is arguably fine, but there doesn't seem to be any benefit in allowing it.
160160
t.Run("indexes", func(t *testing.T) {
161-
seen := make(map[int]bool)
161+
seen := make(map[MetricIdx]bool)
162162
for _, serviceMetrics := range MetricDefs {
163163
for idx := range serviceMetrics {
164164
if seen[idx] {

common/metrics/interfaces.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,43 @@ type (
3030
// Client is the interface used to report metrics tally.
3131
Client interface {
3232
// IncCounter increments a counter metric
33-
IncCounter(scope int, counter int)
33+
IncCounter(scope ScopeIdx, counter MetricIdx)
3434
// AddCounter adds delta to the counter metric
35-
AddCounter(scope int, counter int, delta int64)
35+
AddCounter(scope ScopeIdx, counter MetricIdx, delta int64)
3636
// StartTimer starts a timer for the given
3737
// metric name. Time will be recorded when stopwatch is stopped.
38-
StartTimer(scope int, timer int) tally.Stopwatch
38+
StartTimer(scope ScopeIdx, timer MetricIdx) tally.Stopwatch
3939
// RecordTimer starts a timer for the given
4040
// metric name
41-
RecordTimer(scope int, timer int, d time.Duration)
41+
RecordTimer(scope ScopeIdx, timer MetricIdx, d time.Duration)
4242
// RecordHistogramDuration records a histogram duration value for the given
4343
// metric name
44-
RecordHistogramDuration(scope int, timer int, d time.Duration)
44+
RecordHistogramDuration(scope ScopeIdx, timer MetricIdx, d time.Duration)
4545
// UpdateGauge reports Gauge type absolute value metric
46-
UpdateGauge(scope int, gauge int, value float64)
46+
UpdateGauge(scope ScopeIdx, gauge MetricIdx, value float64)
4747
// Scope return an internal scope that can be used to add additional
4848
// information to metrics
49-
Scope(scope int, tags ...Tag) Scope
49+
Scope(scope ScopeIdx, tags ...Tag) Scope
5050
}
5151

5252
// Scope is an interface for metrics
5353
Scope interface {
5454
// IncCounter increments a counter metric
55-
IncCounter(counter int)
55+
IncCounter(counter MetricIdx)
5656
// AddCounter adds delta to the counter metric
57-
AddCounter(counter int, delta int64)
57+
AddCounter(counter MetricIdx, delta int64)
5858
// StartTimer starts a timer for the given metric name.
5959
// Time will be recorded when stopwatch is stopped.
60-
StartTimer(timer int) Stopwatch
60+
StartTimer(timer MetricIdx) Stopwatch
6161
// RecordTimer starts a timer for the given metric name
62-
RecordTimer(timer int, d time.Duration)
62+
RecordTimer(timer MetricIdx, d time.Duration)
6363
// RecordHistogramDuration records a histogram duration value for the given
6464
// metric name
65-
RecordHistogramDuration(timer int, d time.Duration)
65+
RecordHistogramDuration(timer MetricIdx, d time.Duration)
6666
// RecordHistogramValue records a histogram value for the given metric name
67-
RecordHistogramValue(timer int, value float64)
67+
RecordHistogramValue(timer MetricIdx, value float64)
6868
// UpdateGauge reports Gauge type absolute value metric
69-
UpdateGauge(gauge int, value float64)
69+
UpdateGauge(gauge MetricIdx, value float64)
7070
// Tagged return an internal scope that can be used to add additional
7171
// information to metrics
7272
Tagged(tags ...Tag) Scope

0 commit comments

Comments
 (0)