-
Notifications
You must be signed in to change notification settings - Fork 451
/
config.go
299 lines (261 loc) · 9.6 KB
/
config.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2017 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 instrument
import (
"errors"
"fmt"
"io"
"net"
"net/http"
"time"
prom "github.com/m3db/prometheus_client_golang/prometheus"
"github.com/uber-go/tally"
"github.com/uber-go/tally/m3"
"github.com/uber-go/tally/multi"
"github.com/uber-go/tally/prometheus"
"go.uber.org/zap"
)
var errNoReporterConfigured = errors.New("no reporter configured")
// ScopeConfiguration configures a metric scope.
type ScopeConfiguration struct {
// Prefix of metrics in this scope.
Prefix string `yaml:"prefix"`
// Metrics reporting interval.
ReportingInterval time.Duration `yaml:"reportingInterval"`
// Common tags shared by metrics reported.
CommonTags map[string]string `yaml:"tags"`
}
// MetricsConfiguration configures options for emitting metrics.
type MetricsConfiguration struct {
// Root scope configuration.
RootScope *ScopeConfiguration `yaml:"scope"`
// M3 reporter configuration.
M3Reporter *m3.Configuration `yaml:"m3"`
// Prometheus reporter configuration.
PrometheusReporter *PrometheusConfiguration `yaml:"prometheus"`
// Metrics sampling rate.
SamplingRate float64 `yaml:"samplingRate" validate:"nonzero,min=0.0,max=1.0"`
// Extended metrics type.
ExtendedMetrics *ExtendedMetricsType `yaml:"extended"`
// Metric sanitization type.
Sanitization *MetricSanitizationType `yaml:"sanitization"`
}
// NewRootScope creates a new tally.Scope based on a tally.CachedStatsReporter
// based on the the the config.
func (mc *MetricsConfiguration) NewRootScope() (tally.Scope, io.Closer, error) {
opts := NewRootScopeAndReportersOptions{}
scope, closer, _, err := mc.NewRootScopeAndReporters(opts)
return scope, closer, err
}
// MetricsConfigurationReporters is the reporters constructed.
type MetricsConfigurationReporters struct {
AllReporters []tally.CachedStatsReporter
M3Reporter *MetricsConfigurationM3Reporter
PrometheusReporter *MetricsConfigurationPrometheusReporter
}
// MetricsConfigurationM3Reporter is the M3 reporter if constructed.
type MetricsConfigurationM3Reporter struct {
Reporter m3.Reporter
}
// MetricsConfigurationPrometheusReporter is the Prometheus reporter if constructed.
type MetricsConfigurationPrometheusReporter struct {
Reporter prometheus.Reporter
Registry *prom.Registry
}
// NewRootScopeAndReportersOptions is a set of options.
type NewRootScopeAndReportersOptions struct {
PrometheusHandlerListener net.Listener
PrometheusDefaultServeMux *http.ServeMux
PrometheusExternalRegistries []PrometheusExternalRegistry
PrometheusOnError func(e error)
// CommonLabels will be appended to every metric gathered.
CommonLabels map[string]string
}
type metricsClosers struct {
// serverCloser is responsible for closing the http server handling /metrics
// if one was started up as a part of reporter creation.
serverCloser io.Closer
// reporterClose is responsible for closing the underlying tally.Reporter
// responsible for reporting metrics for all registered scopes.
reporterCloser io.Closer
}
func (m metricsClosers) Close() error {
if err := m.reporterCloser.Close(); err != nil {
return err
}
if m.serverCloser != nil {
return m.serverCloser.Close()
}
return nil
}
// NewRootScopeAndReporters creates a new tally.Scope based on a tally.CachedStatsReporter
// based on the the the config along with the reporters used.
func (mc *MetricsConfiguration) NewRootScopeAndReporters(
opts NewRootScopeAndReportersOptions,
) (
tally.Scope,
io.Closer,
MetricsConfigurationReporters,
error,
) {
var (
result MetricsConfigurationReporters
closers metricsClosers
)
if mc.M3Reporter != nil {
r, err := mc.M3Reporter.NewReporter()
if err != nil {
return nil, nil, MetricsConfigurationReporters{}, err
}
result.AllReporters = append(result.AllReporters, r)
result.M3Reporter = &MetricsConfigurationM3Reporter{
Reporter: r,
}
}
if mc.PrometheusReporter != nil {
// Set a default on error method for sane handling when registering metrics
// results in an error with the Prometheus reporter.
onError := func(e error) {
logger := NewOptions().Logger()
logger.Error("register metrics error", zap.Error(e))
}
if opts.PrometheusOnError != nil {
onError = opts.PrometheusOnError
}
// Override the default registry with an empty one that does not have the default
// registered collectors (Go and Process). The M3 reporters will emit the Go metrics
// and the Process metrics are reported by both the M3 process reporter and a
// modified Prometheus process collector, which reports everything except the
// number of open FDs.
//
// Collecting the number of F.Ds for a process that has many of them can take a long
// time and be very CPU intensive, especially the default Prometheus collector
// implementation which is less optimized than the M3 implementation.
//
// TODO: Emit the Prometheus process stats from our own process reporter so we
// get the same stats regardless of the reporter used. See issue:
// https://github.com/m3db/m3/issues/1649
registry := prom.NewRegistry()
if err := registry.Register(NewPrometheusProcessCollector(ProcessCollectorOpts{
DisableOpenFDs: true,
})); err != nil {
return nil, nil, MetricsConfigurationReporters{}, fmt.Errorf("could not create process collector: %v", err)
}
opts := PrometheusConfigurationOptions{
Registry: registry,
ExternalRegistries: opts.PrometheusExternalRegistries,
HandlerListener: opts.PrometheusHandlerListener,
DefaultServeMux: opts.PrometheusDefaultServeMux,
OnError: onError,
CommonLabels: opts.CommonLabels,
}
// Use default instrument package default histogram buckets if not set.
if len(mc.PrometheusReporter.DefaultHistogramBuckets) == 0 {
for _, v := range DefaultHistogramTimerHistogramBuckets().AsValues() {
bucket := prometheus.HistogramObjective{
Upper: v,
}
mc.PrometheusReporter.DefaultHistogramBuckets =
append(mc.PrometheusReporter.DefaultHistogramBuckets, bucket)
}
}
if len(mc.PrometheusReporter.DefaultSummaryObjectives) == 0 {
for k, v := range DefaultSummaryQuantileObjectives() {
q := prometheus.SummaryObjective{
Percentile: k,
AllowedError: v,
}
mc.PrometheusReporter.DefaultSummaryObjectives =
append(mc.PrometheusReporter.DefaultSummaryObjectives, q)
}
}
r, srvCloser, err := mc.PrometheusReporter.NewReporter(opts)
if err != nil {
return nil, nil, MetricsConfigurationReporters{}, err
}
closers.serverCloser = srvCloser
result.AllReporters = append(result.AllReporters, r)
result.PrometheusReporter = &MetricsConfigurationPrometheusReporter{
Reporter: r,
Registry: registry,
}
}
if len(result.AllReporters) == 0 {
return nil, nil, MetricsConfigurationReporters{}, errNoReporterConfigured
}
var r tally.CachedStatsReporter
if len(result.AllReporters) == 1 {
r = result.AllReporters[0]
} else {
r = multi.NewMultiCachedReporter(result.AllReporters...)
}
scope, closer := mc.NewRootScopeReporter(r)
closers.reporterCloser = closer
return scope, closers, result, nil
}
// NewRootScopeReporter creates a new tally.Scope based on a given tally.CachedStatsReporter
// and given root scope config. In most cases NewRootScope should be used, but for cases such
// as hooking into the reporter to manually flush it.
func (mc *MetricsConfiguration) NewRootScopeReporter(
r tally.CachedStatsReporter,
) (tally.Scope, io.Closer) {
var (
prefix string
tags map[string]string
)
if mc.RootScope != nil {
if mc.RootScope.Prefix != "" {
prefix = mc.RootScope.Prefix
}
if mc.RootScope.CommonTags != nil {
tags = mc.RootScope.CommonTags
}
}
var sanitizeOpts *tally.SanitizeOptions
if mc.Sanitization != nil {
sanitizeOpts = mc.Sanitization.NewOptions()
}
scopeOpts := tally.ScopeOptions{
Tags: tags,
Prefix: prefix,
CachedReporter: r,
SanitizeOptions: sanitizeOpts,
}
reportInterval := mc.ReportInterval()
scope, closer := tally.NewRootScope(scopeOpts, reportInterval)
if mc.ExtendedMetrics != nil {
StartReportingExtendedMetrics(scope, reportInterval, *mc.ExtendedMetrics)
}
return scope, closer
}
// SampleRate returns the metrics sampling rate.
func (mc *MetricsConfiguration) SampleRate() float64 {
if mc.SamplingRate > 0.0 && mc.SamplingRate <= 1.0 {
return mc.SamplingRate
}
return defaultSamplingRate
}
// ReportInterval returns the metrics reporting interval.
func (mc *MetricsConfiguration) ReportInterval() time.Duration {
if mc.RootScope != nil && mc.RootScope.ReportingInterval != 0 {
return mc.RootScope.ReportingInterval
}
return defaultReportingInterval
}