Skip to content

Commit c547da2

Browse files
committed
fix ErrorCallbackFunc
1 parent 9d3c481 commit c547da2

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

examples/exemplars/main.go

+48
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,54 @@
1414
// A simple example of how to record a latency metric with exemplars, using a fictional id
1515
// as a prometheus label.
1616

17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"github.com/prometheus/client_golang/prometheus/collectors"
23+
"github.com/prometheus/client_golang/prometheus/promhttp"
24+
"log"
25+
"math/rand"
26+
"net/http"
27+
"time"
28+
29+
"github.com/prometheus/client_golang/prometheus"
30+
"github.com/prometheus/client_golang/prometheus/graphite"
31+
"go.uber.org/zap"
32+
)
33+
34+
func main() {
35+
logger, _ := zap.NewProduction()
36+
defer logger.Sync()
37+
38+
ctx := context.Background()
39+
ctx, cancel := context.WithCancel(ctx)
40+
defer cancel()
41+
42+
c := &Config{
43+
URL: "graphite.example.org:3099",
44+
Gatherer: prometheus.DefaultGatherer,
45+
Prefix: "prefix",
46+
Interval: 5 * time.Second,
47+
Timeout: 2 * time.Second,
48+
ErrorHandling: testCase.errorHandling,
49+
ErrorCallbackFunc: func(err error) { if err != nil { logger.Error("run", zap.Error(err)); cancel() } },
50+
}
51+
52+
53+
b, err := graphite.NewBridge(c)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
b.Run(ctx)
59+
}
60+
61+
62+
63+
64+
1765
package main
1866

1967
import (

prometheus/graphite/bridge.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ const (
5050

5151
// Abort the push to Graphite upon the first error encountered.
5252
AbortOnError
53-
54-
// Execute callback function on error.
55-
CallbackOnError
5653
)
5754

5855
// Config defines the Graphite bridge config.
@@ -84,7 +81,7 @@ type Config struct {
8481
ErrorHandling HandlerErrorHandling
8582

8683
// ErrorCallbackFunc is a callback function that can be executed when error is occurred
87-
ErrorCallbackFunc CallbackFunc
84+
ErrorCallbackFunc ErrorCallbackFunc
8885
}
8986

9087
// Bridge pushes metrics to the configured Graphite server.
@@ -96,7 +93,7 @@ type Bridge struct {
9693
timeout time.Duration
9794

9895
errorHandling HandlerErrorHandling
99-
errorCallbackFunc CallbackFunc
96+
errorCallbackFunc ErrorCallbackFunc
10097
logger Logger
10198

10299
g prometheus.Gatherer
@@ -109,8 +106,8 @@ type Logger interface {
109106
Println(v ...interface{})
110107
}
111108

112-
// CallbackFunc is a special type for callback functions
113-
type CallbackFunc func(error)
109+
// ErrorCallbackFunc is a special type for callback functions
110+
type ErrorCallbackFunc func(error)
114111

115112
// NewBridge returns a pointer to a new Bridge struct.
116113
func NewBridge(c *Config) (*Bridge, error) {
@@ -179,17 +176,16 @@ func (b *Bridge) Run(ctx context.Context) {
179176
// Push pushes Prometheus metrics to the configured Graphite server.
180177
func (b *Bridge) Push() error {
181178
err := b.push()
179+
if b.errorCallbackFunc != nil {
180+
b.errorCallbackFunc(err)
181+
}
182182
switch b.errorHandling {
183183
case AbortOnError:
184184
return err
185185
case ContinueOnError:
186186
if b.logger != nil {
187187
b.logger.Println("continue on error:", err)
188188
}
189-
case CallbackOnError:
190-
if b.errorCallbackFunc != nil {
191-
b.errorCallbackFunc(err)
192-
}
193189
}
194190
return nil
195191
}

prometheus/graphite/bridge_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,11 @@ func TestErrorHandling(t *testing.T) {
477477
{
478478
errorHandling: ContinueOnError,
479479
receivedError: nil,
480-
interceptedError: nil,
480+
interceptedError: &net.OpError{},
481481
},
482482
{
483483
errorHandling: AbortOnError,
484484
receivedError: &net.OpError{},
485-
interceptedError: nil,
486-
},
487-
{
488-
errorHandling: CallbackOnError,
489-
receivedError: nil,
490485
interceptedError: &net.OpError{},
491486
},
492487
}

0 commit comments

Comments
 (0)