-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.go
162 lines (133 loc) · 4.62 KB
/
options.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
155
156
157
158
159
160
161
162
package promgrpc
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
)
// ShareableOption is a simple wrapper for shareable method.
// It makes it possible to distinguish options reserved for direct usage, from those that are applicable on a set of objects.
type ShareableOption interface {
shareable()
}
type statsHandlerOptions struct {
// stats.ConnTagInfo carries no information about whether it is an incoming or outgoing connection.
// Use IsClient method if available.
client bool
handleRPCLabelFn HandleRPCLabelFunc
tagRPCLabelFn TagRPCLabelFunc
}
// StatsHandlerOption configures a stats handler behaviour.
type StatsHandlerOption interface {
apply(*statsHandlerOptions)
}
type funcStatsHandlerOption struct {
f func(*statsHandlerOptions)
}
func (o *funcStatsHandlerOption) apply(in *statsHandlerOptions) {
o.f(in)
}
func newFuncStatsHandlerOption(f func(*statsHandlerOptions)) *funcStatsHandlerOption {
return &funcStatsHandlerOption{
f: f,
}
}
// ShareableStatsHandlerOption is StatsHandlerOption extended with shareable capability.
type ShareableStatsHandlerOption interface {
ShareableOption
StatsHandlerOption
}
// StatsHandlerWithHandleRPCLabelsFunc allows to inject custom HandleRPCLabelFunc to a stats handler.
// It is not shareable because there little to no chance that all stats handlers need the same set of labels.
func StatsHandlerWithHandleRPCLabelsFunc(fn HandleRPCLabelFunc) StatsHandlerOption {
return newFuncStatsHandlerOption(func(o *statsHandlerOptions) {
o.handleRPCLabelFn = fn
})
}
// StatsHandlerWithTagRPCLabelsFunc allows to inject custom TagRPCLabelFunc to a stats handler.
// It is not shareable because of performance reasons.
// If all stats handlers require the same set of additional labels, it is better to implement a custom coordinator
// (e.g. by embedding StatsHandler) with self-defined TagRPC method.
// That way, it is guaranteed that new tagging execute only once and default implementation be overridden.
func StatsHandlerWithTagRPCLabelsFunc(fn TagRPCLabelFunc) StatsHandlerOption {
return newFuncStatsHandlerOption(func(o *statsHandlerOptions) {
o.tagRPCLabelFn = fn
})
}
type collectorOptions struct {
namespace string
userAgent string
constLabels prometheus.Labels
}
// CollectorOption configures a collector.
type CollectorOption interface {
apply(*collectorOptions)
}
type funcCollectorOption struct {
f func(*collectorOptions)
}
func (o *funcCollectorOption) apply(in *collectorOptions) {
o.f(in)
}
func newFuncCollectorOption(f func(*collectorOptions)) *funcCollectorOption {
return &funcCollectorOption{
f: f,
}
}
// ShareableCollectorOption is CollectorOption extended with shareable capability.
type ShareableCollectorOption interface {
ShareableOption
CollectorOption
}
type funcShareableCollectorOption struct {
funcCollectorOption
}
func (o *funcShareableCollectorOption) shareable() {}
func newFuncShareableCollectorOption(f func(*collectorOptions)) *funcShareableCollectorOption {
return &funcShareableCollectorOption{
funcCollectorOption: funcCollectorOption{f: f},
}
}
// CollectorWithNamespace returns a ShareableCollectorOption which sets namespace of a collector.
func CollectorWithNamespace(namespace string) ShareableCollectorOption {
return newFuncShareableCollectorOption(func(o *collectorOptions) {
o.namespace = namespace
})
}
// CollectorWithUserAgent ...
func CollectorWithUserAgent(name, version string) ShareableCollectorOption {
return newFuncShareableCollectorOption(func(o *collectorOptions) {
o.userAgent = fmt.Sprintf("grpc/%s %s/%s", grpc.Version, name, version)
})
}
// CollectorWithConstLabels returns a ShareableCollectorOption which adds a set of constant labels to a collector.
func CollectorWithConstLabels(constLabels prometheus.Labels) ShareableCollectorOption {
return newFuncShareableCollectorOption(func(o *collectorOptions) {
o.constLabels = constLabels
})
}
func applyCollectorOptions(prototype prometheus.Opts, opts ...CollectorOption) prometheus.Opts {
var options collectorOptions
for _, opt := range opts {
opt.apply(&options)
}
if options.namespace != "" {
prototype.Namespace = options.namespace
}
if options.constLabels != nil {
prototype.ConstLabels = options.constLabels
}
return prototype
}
func applyHistogramOptions(prototype prometheus.HistogramOpts, opts ...CollectorOption) prometheus.HistogramOpts {
var options collectorOptions
for _, opt := range opts {
opt.apply(&options)
}
if options.namespace != "" {
prototype.Namespace = options.namespace
}
if options.constLabels != nil {
prototype.ConstLabels = options.constLabels
}
return prototype
}