@@ -38,11 +38,22 @@ const (
38
38
millisecondsPerSecond = 1000
39
39
)
40
40
41
- // ErrorHandler is a function that handles errors
42
- type ErrorHandler func (err error )
41
+ // HandlerErrorHandling defines how a Handler serving metrics will handle
42
+ // errors.
43
+ type HandlerErrorHandling int
43
44
44
- // DefaultErrorHandler skips received errors
45
- var DefaultErrorHandler = func (err error ) {}
45
+ // These constants cause handlers serving metrics to behave as described if
46
+ // errors are encountered.
47
+ const (
48
+ // Ignore errors and try to push as many metrics to Graphite as possible.
49
+ ContinueOnError HandlerErrorHandling = iota
50
+
51
+ // Abort the push to Graphite upon the first error encountered.
52
+ AbortOnError
53
+
54
+ // Execute callback function on error.
55
+ CallbackOnError
56
+ )
46
57
47
58
// Config defines the Graphite bridge config.
48
59
type Config struct {
@@ -64,8 +75,16 @@ type Config struct {
64
75
// The Gatherer to use for metrics. Defaults to prometheus.DefaultGatherer.
65
76
Gatherer prometheus.Gatherer
66
77
67
- // ErrorHandler defines how errors are handled.
68
- ErrorHandler ErrorHandler
78
+ // The logger that messages are written to. Defaults to no logging.
79
+ Logger Logger
80
+
81
+ // ErrorHandling defines how errors are handled. Note that errors are
82
+ // logged regardless of the configured ErrorHandling provided Logger
83
+ // is not nil.
84
+ ErrorHandling HandlerErrorHandling
85
+
86
+ // ErrorCallbackFunc is a callback function that can be executed when error is occurred
87
+ ErrorCallbackFunc CallbackFunc
69
88
}
70
89
71
90
// Bridge pushes metrics to the configured Graphite server.
@@ -76,11 +95,23 @@ type Bridge struct {
76
95
interval time.Duration
77
96
timeout time.Duration
78
97
79
- errorHandler ErrorHandler
98
+ errorHandling HandlerErrorHandling
99
+ errorCallbackFunc CallbackFunc
100
+ logger Logger
80
101
81
102
g prometheus.Gatherer
82
103
}
83
104
105
+ // Logger is the minimal interface Bridge needs for logging. Note that
106
+ // log.Logger from the standard library implements this interface, and it is
107
+ // easy to implement by custom loggers, if they don't do so already anyway.
108
+ type Logger interface {
109
+ Println (v ... interface {})
110
+ }
111
+
112
+ // CallbackFunc is a special type for callback functions
113
+ type CallbackFunc func (error )
114
+
84
115
// NewBridge returns a pointer to a new Bridge struct.
85
116
func NewBridge (c * Config ) (* Bridge , error ) {
86
117
b := & Bridge {}
@@ -98,6 +129,10 @@ func NewBridge(c *Config) (*Bridge, error) {
98
129
b .g = c .Gatherer
99
130
}
100
131
132
+ if c .Logger != nil {
133
+ b .logger = c .Logger
134
+ }
135
+
101
136
if c .Prefix != "" {
102
137
b .prefix = c .Prefix
103
138
}
@@ -115,7 +150,11 @@ func NewBridge(c *Config) (*Bridge, error) {
115
150
b .timeout = c .Timeout
116
151
}
117
152
118
- b .errorHandler = c .ErrorHandler
153
+ b .errorHandling = c .ErrorHandling
154
+
155
+ if c .ErrorCallbackFunc != nil {
156
+ b .errorCallbackFunc = c .ErrorCallbackFunc
157
+ }
119
158
120
159
return b , nil
121
160
}
@@ -128,7 +167,9 @@ func (b *Bridge) Run(ctx context.Context) {
128
167
for {
129
168
select {
130
169
case <- ticker .C :
131
- b .errorHandler (b .Push ())
170
+ if err := b .Push (); err != nil && b .logger != nil {
171
+ b .logger .Println ("error pushing to Graphite:" , err )
172
+ }
132
173
case <- ctx .Done ():
133
174
return
134
175
}
@@ -137,11 +178,27 @@ func (b *Bridge) Run(ctx context.Context) {
137
178
138
179
// Push pushes Prometheus metrics to the configured Graphite server.
139
180
func (b * Bridge ) Push () error {
181
+ err := b .push ()
182
+ switch b .errorHandling {
183
+ case AbortOnError :
184
+ return err
185
+ case ContinueOnError :
186
+ if b .logger != nil {
187
+ b .logger .Println ("continue on error:" , err )
188
+ }
189
+ case CallbackOnError :
190
+ if b .errorCallbackFunc != nil {
191
+ b .errorCallbackFunc (err )
192
+ }
193
+ }
194
+ return nil
195
+ }
196
+
197
+ func (b * Bridge ) push () error {
140
198
mfs , err := b .g .Gather ()
141
199
if err != nil {
142
200
return err
143
201
}
144
-
145
202
if len (mfs ) == 0 {
146
203
return nil
147
204
}
0 commit comments