-
Notifications
You must be signed in to change notification settings - Fork 867
/
tally_metrics_handler.go
154 lines (130 loc) · 4.56 KB
/
tally_metrics_handler.go
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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"time"
"github.com/uber-go/tally/v4"
"go.temporal.io/server/common/log"
)
var sanitizer = tally.NewSanitizer(tally.SanitizeOptions{
NameCharacters: tally.ValidCharacters{Ranges: tally.AlphanumericRange, Characters: tally.UnderscoreCharacters},
KeyCharacters: tally.ValidCharacters{Ranges: tally.AlphanumericRange, Characters: tally.UnderscoreCharacters},
ValueCharacters: tally.ValidCharacters{Ranges: tally.AlphanumericRange, Characters: tally.UnderscoreCharacters},
ReplacementCharacter: '_',
})
type (
excludeTags map[string]map[string]struct{}
tallyMetricsHandler struct {
scope tally.Scope
perUnitBuckets map[MetricUnit]tally.Buckets
excludeTags excludeTags
}
)
var _ Handler = (*tallyMetricsHandler)(nil)
func NewTallyMetricsHandler(cfg ClientConfig, scope tally.Scope) *tallyMetricsHandler {
perUnitBuckets := make(map[MetricUnit]tally.Buckets)
for unit, boundariesList := range cfg.PerUnitHistogramBoundaries {
perUnitBuckets[MetricUnit(unit)] = tally.ValueBuckets(boundariesList)
}
return &tallyMetricsHandler{
scope: scope,
perUnitBuckets: perUnitBuckets,
excludeTags: configExcludeTags(cfg),
}
}
// WithTags creates a new MetricProvder with provided []Tag
// Tags are merged with registered Tags from the source MetricsHandler
func (tmh *tallyMetricsHandler) WithTags(tags ...Tag) Handler {
return &tallyMetricsHandler{
scope: tmh.scope.Tagged(tagsToMap(tags, tmh.excludeTags)),
perUnitBuckets: tmh.perUnitBuckets,
excludeTags: tmh.excludeTags,
}
}
// Counter obtains a counter for the given name.
func (tmh *tallyMetricsHandler) Counter(counter string) CounterIface {
return CounterFunc(func(i int64, t ...Tag) {
scope := tmh.scope
if len(t) > 0 {
scope = tmh.scope.Tagged(tagsToMap(t, tmh.excludeTags))
}
scope.Counter(counter).Inc(i)
})
}
// Gauge obtains a gauge for the given name.
func (tmh *tallyMetricsHandler) Gauge(gauge string) GaugeIface {
return GaugeFunc(func(f float64, t ...Tag) {
scope := tmh.scope
if len(t) > 0 {
scope = tmh.scope.Tagged(tagsToMap(t, tmh.excludeTags))
}
scope.Gauge(gauge).Update(f)
})
}
// Timer obtains a timer for the given name.
func (tmh *tallyMetricsHandler) Timer(timer string) TimerIface {
return TimerFunc(func(d time.Duration, t ...Tag) {
scope := tmh.scope
if len(t) > 0 {
scope = tmh.scope.Tagged(tagsToMap(t, tmh.excludeTags))
}
scope.Timer(timer).Record(d)
})
}
// Histogram obtains a histogram for the given name.
func (tmh *tallyMetricsHandler) Histogram(histogram string, unit MetricUnit) HistogramIface {
return HistogramFunc(func(i int64, t ...Tag) {
scope := tmh.scope
if len(t) > 0 {
scope = tmh.scope.Tagged(tagsToMap(t, tmh.excludeTags))
}
scope.Histogram(histogram, tmh.perUnitBuckets[unit]).RecordValue(float64(i))
})
}
func (*tallyMetricsHandler) Stop(log.Logger) {}
func (*tallyMetricsHandler) Close() error {
return nil
}
func (tmh *tallyMetricsHandler) StartBatch(_ string) BatchHandler {
return tmh
}
func tagsToMap(t1 []Tag, e excludeTags) map[string]string {
if len(t1) == 0 {
return nil
}
m := make(map[string]string, len(t1))
convert := func(tag Tag) {
if vals, ok := e[tag.Key()]; ok {
if _, ok := vals[tag.Value()]; !ok {
m[tag.Key()] = tagExcludedValue
return
}
}
m[tag.Key()] = tag.Value()
}
for i := range t1 {
convert(t1[i])
}
return m
}