-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
134 lines (117 loc) · 3.52 KB
/
factory.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
// Copyright (c) 2022 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expvar
import (
"sort"
kexpvar "github.com/go-kit/kit/metrics/expvar"
"github.com/jaegertracing/jaeger/pkg/metrics"
)
// NewFactory creates a new metrics factory using go-kit expvar package.
// buckets is the number of buckets to be used in histograms.
// Custom buckets passed via options are not supported.
func NewFactory(buckets int) metrics.Factory {
return &factory{
buckets: buckets,
scope: "",
scopeSep: ".",
tagsSep: ".",
tagKVSep: "_",
cache: newCache(),
}
}
type factory struct {
buckets int
scope string
tags map[string]string
scopeSep string
tagsSep string
tagKVSep string
cache *cache
}
var _ metrics.Factory = (*factory)(nil)
func (f *factory) subScope(name string) string {
if f.scope == "" {
return name
}
if name == "" {
return f.scope
}
return f.scope + f.scopeSep + name
}
func (f *factory) mergeTags(tags map[string]string) map[string]string {
ret := make(map[string]string, len(f.tags)+len(tags))
for k, v := range f.tags {
ret[k] = v
}
for k, v := range tags {
ret[k] = v
}
return ret
}
func (f *factory) getKey(name string, tags map[string]string) string {
fullName := f.subScope(name)
fullTags := f.mergeTags(tags)
return makeKey(fullName, fullTags, f.tagsSep, f.tagKVSep)
}
// getKey converts name+tags into a single string of the form
// "name|tag1=value1|...|tagN=valueN", where tag names are
// sorted alphabetically.
func makeKey(name string, tags map[string]string, tagsSep string, tagKVSep string) string {
keys := make([]string, 0, len(tags))
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
key := name
for _, k := range keys {
key = key + tagsSep + k + tagKVSep + tags[k]
}
return key
}
func (f *factory) Counter(options metrics.Options) metrics.Counter {
key := f.getKey(options.Name, options.Tags)
return f.cache.getOrSetCounter(key, func() metrics.Counter {
return NewCounter(kexpvar.NewCounter(key))
})
}
func (f *factory) Gauge(options metrics.Options) metrics.Gauge {
key := f.getKey(options.Name, options.Tags)
return f.cache.getOrSetGauge(key, func() metrics.Gauge {
return NewGauge(kexpvar.NewGauge(key))
})
}
func (f *factory) Timer(options metrics.TimerOptions) metrics.Timer {
key := f.getKey(options.Name, options.Tags)
return f.cache.getOrSetTimer(key, func() metrics.Timer {
return NewTimer(kexpvar.NewHistogram(key, f.buckets))
})
}
func (f *factory) Histogram(options metrics.HistogramOptions) metrics.Histogram {
key := f.getKey(options.Name, options.Tags)
return f.cache.getOrSetHistogram(key, func() metrics.Histogram {
return NewHistogram(kexpvar.NewHistogram(key, f.buckets))
})
}
func (f *factory) Namespace(options metrics.NSOptions) metrics.Factory {
return &factory{
buckets: f.buckets,
scope: f.subScope(options.Name),
tags: f.mergeTags(options.Tags),
scopeSep: f.scopeSep,
tagsSep: f.tagsSep,
tagKVSep: f.tagKVSep,
cache: f.cache,
}
}