-
Notifications
You must be signed in to change notification settings - Fork 702
/
Copy pathoptions.go
143 lines (121 loc) · 4.36 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
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package prometheus
import (
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
// FromError returns a grpc status. If the error code is neither a valid grpc status nor a context error, codes.Unknown
// will be set.
func FromError(err error) *status.Status {
s, ok := status.FromError(err)
// Mirror what the grpc server itself does, i.e. also convert context errors to status
if !ok {
s = status.FromContextError(err)
}
return s
}
// A CounterOption lets you add options to Counter metrics using With* funcs.
type CounterOption func(*prometheus.CounterOpts)
type counterOptions []CounterOption
func (co counterOptions) apply(o prometheus.CounterOpts) prometheus.CounterOpts {
for _, f := range co {
f(&o)
}
return o
}
// WithConstLabels allows you to add ConstLabels to Counter metrics.
func WithConstLabels(labels prometheus.Labels) CounterOption {
return func(o *prometheus.CounterOpts) {
o.ConstLabels = labels
}
}
// WithSubsystem allows you to add a Subsystem to Counter metrics.
func WithSubsystem(subsystem string) CounterOption {
return func(o *prometheus.CounterOpts) {
o.Subsystem = subsystem
}
}
// WithNamespace allows you to add a Namespace to Counter metrics.
func WithNamespace(namespace string) CounterOption {
return func(o *prometheus.CounterOpts) {
o.Namespace = namespace
}
}
// A HistogramOption lets you add options to Histogram metrics using With*
// funcs.
type HistogramOption func(*prometheus.HistogramOpts)
type histogramOptions []HistogramOption
func (ho histogramOptions) apply(o prometheus.HistogramOpts) prometheus.HistogramOpts {
for _, f := range ho {
f(&o)
}
return o
}
// WithHistogramBuckets allows you to specify custom bucket ranges for histograms if EnableHandlingTimeHistogram is on.
func WithHistogramBuckets(buckets []float64) HistogramOption {
return func(o *prometheus.HistogramOpts) { o.Buckets = buckets }
}
// WithHistogramOpts allows you to specify HistogramOpts but makes sure the correct name and label is used.
// This function is helpful when specifying more than just the buckets, like using NativeHistograms.
func WithHistogramOpts(opts *prometheus.HistogramOpts) HistogramOption {
// TODO: This isn't ideal either if new fields are added to prometheus.HistogramOpts.
// Maybe we can change the interface to accept abitrary HistogramOpts and
// only make sure to overwrite the necessary fields (name, labels).
return func(o *prometheus.HistogramOpts) {
o.Buckets = opts.Buckets
o.NativeHistogramBucketFactor = opts.NativeHistogramBucketFactor
o.NativeHistogramZeroThreshold = opts.NativeHistogramZeroThreshold
o.NativeHistogramMaxBucketNumber = opts.NativeHistogramMaxBucketNumber
o.NativeHistogramMinResetDuration = opts.NativeHistogramMinResetDuration
o.NativeHistogramMaxZeroThreshold = opts.NativeHistogramMaxZeroThreshold
}
}
// WithHistogramConstLabels allows you to add custom ConstLabels to
// histograms metrics.
func WithHistogramConstLabels(labels prometheus.Labels) HistogramOption {
return func(o *prometheus.HistogramOpts) {
o.ConstLabels = labels
}
}
// WithHistogramSubsystem allows you to add a Subsystem to histograms metrics.
func WithHistogramSubsystem(subsystem string) HistogramOption {
return func(o *prometheus.HistogramOpts) {
o.Subsystem = subsystem
}
}
// WithHistogramNamespace allows you to add a Namespace to histograms metrics.
func WithHistogramNamespace(namespace string) HistogramOption {
return func(o *prometheus.HistogramOpts) {
o.Namespace = namespace
}
}
func typeFromMethodInfo(mInfo *grpc.MethodInfo) grpcType {
if !mInfo.IsClientStream && !mInfo.IsServerStream {
return Unary
}
if mInfo.IsClientStream && !mInfo.IsServerStream {
return ClientStream
}
if !mInfo.IsClientStream && mInfo.IsServerStream {
return ServerStream
}
return BidiStream
}
// An Option lets you add options to prometheus interceptors using With* funcs.
type Option func(*config)
type config struct {
exemplarFn exemplarFromCtxFn
}
func (c *config) apply(opts []Option) {
for _, o := range opts {
o(c)
}
}
// WithExemplarFromContext sets function that will be used to deduce exemplar for all counter and histogram metrics.
func WithExemplarFromContext(exemplarFn exemplarFromCtxFn) Option {
return func(o *config) {
o.exemplarFn = exemplarFn
}
}