-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions.go
105 lines (90 loc) · 2.64 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
/*
* Copyright 2021 CloudWeGo Authors
*
* 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 prometheus
import (
"net/http"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
)
var defaultBuckets = []float64{5000, 10000, 25000, 50000, 100000, 250000, 500000, 1000000}
// Option opts for monitor prometheus
type Option interface {
apply(cfg *config)
}
type option func(cfg *config)
func (fn option) apply(cfg *config) {
fn(cfg)
}
type config struct {
buckets []float64
enableGoCollector bool
runtimeMetricRules []collectors.GoRuntimeMetricsRule
registry *prom.Registry
serveMux *http.ServeMux
disableServer bool
}
func defaultConfig() *config {
return &config{
buckets: defaultBuckets,
enableGoCollector: false,
runtimeMetricRules: []collectors.GoRuntimeMetricsRule{},
registry: prom.NewRegistry(),
serveMux: http.DefaultServeMux,
disableServer: false,
}
}
// WithEnableGoCollector enable go collector
func WithEnableGoCollector(enable bool) Option {
return option(func(cfg *config) {
cfg.enableGoCollector = enable
})
}
// WithGoCollectorRule define your custom go collector rule
func WithGoCollectorRule(rules ...collectors.GoRuntimeMetricsRule) Option {
return option(func(cfg *config) {
cfg.runtimeMetricRules = rules
})
}
// WithHistogramBuckets define your custom histogram buckets base on your biz
func WithHistogramBuckets(buckets []float64) Option {
return option(func(cfg *config) {
if len(buckets) > 0 {
cfg.buckets = buckets
}
})
}
// WithRegistry define your custom registry
func WithRegistry(registry *prom.Registry) Option {
return option(func(cfg *config) {
if registry != nil {
cfg.registry = registry
}
})
}
// WithServeMux define your custom serve mux
func WithServeMux(serveMux *http.ServeMux) Option {
return option(func(cfg *config) {
if serveMux != nil {
cfg.serveMux = serveMux
}
})
}
// WithDisableServer disable prometheus server
func WithDisableServer(disable bool) Option {
return option(func(cfg *config) {
cfg.disableServer = disable
})
}