diff --git a/examples/exemplars/main.go b/examples/exemplars/main.go index 798c2df..b5fd3de 100644 --- a/examples/exemplars/main.go +++ b/examples/exemplars/main.go @@ -14,6 +14,54 @@ // A simple example of how to record a latency metric with exemplars, using a fictional id // as a prometheus label. +package main + +import ( + "context" + "fmt" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promhttp" + "log" + "math/rand" + "net/http" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/graphite" + "go.uber.org/zap" +) + +func main() { + logger, _ := zap.NewProduction() + defer logger.Sync() + + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + c := &Config{ + URL: "graphite.example.org:3099", + Gatherer: prometheus.DefaultGatherer, + Prefix: "prefix", + Interval: 5 * time.Second, + Timeout: 2 * time.Second, + ErrorHandling: testCase.errorHandling, + ErrorCallbackFunc: func(err error) { if err != nil { logger.Error("run", zap.Error(err)); cancel() } }, + } + + + b, err := graphite.NewBridge(c) + if err != nil { + t.Fatal(err) + } + + b.Run(ctx) +} + + + + + package main import ( diff --git a/prometheus/graphite/bridge.go b/prometheus/graphite/bridge.go index 5d7d35c..f0693c6 100644 --- a/prometheus/graphite/bridge.go +++ b/prometheus/graphite/bridge.go @@ -50,9 +50,6 @@ const ( // Abort the push to Graphite upon the first error encountered. AbortOnError - - // Execute callback function on error. - CallbackOnError ) // Config defines the Graphite bridge config. @@ -84,7 +81,7 @@ type Config struct { ErrorHandling HandlerErrorHandling // ErrorCallbackFunc is a callback function that can be executed when error is occurred - ErrorCallbackFunc CallbackFunc + ErrorCallbackFunc ErrorCallbackFunc } // Bridge pushes metrics to the configured Graphite server. @@ -96,7 +93,7 @@ type Bridge struct { timeout time.Duration errorHandling HandlerErrorHandling - errorCallbackFunc CallbackFunc + errorCallbackFunc ErrorCallbackFunc logger Logger g prometheus.Gatherer @@ -109,8 +106,8 @@ type Logger interface { Println(v ...interface{}) } -// CallbackFunc is a special type for callback functions -type CallbackFunc func(error) +// ErrorCallbackFunc is a special type for callback functions +type ErrorCallbackFunc func(error) // NewBridge returns a pointer to a new Bridge struct. func NewBridge(c *Config) (*Bridge, error) { @@ -179,6 +176,9 @@ func (b *Bridge) Run(ctx context.Context) { // Push pushes Prometheus metrics to the configured Graphite server. func (b *Bridge) Push() error { err := b.push() + if b.errorCallbackFunc != nil { + b.errorCallbackFunc(err) + } switch b.errorHandling { case AbortOnError: return err @@ -186,10 +186,6 @@ func (b *Bridge) Push() error { if b.logger != nil { b.logger.Println("continue on error:", err) } - case CallbackOnError: - if b.errorCallbackFunc != nil { - b.errorCallbackFunc(err) - } } return nil } diff --git a/prometheus/graphite/bridge_test.go b/prometheus/graphite/bridge_test.go index 9c83cd4..01f0079 100644 --- a/prometheus/graphite/bridge_test.go +++ b/prometheus/graphite/bridge_test.go @@ -477,16 +477,11 @@ func TestErrorHandling(t *testing.T) { { errorHandling: ContinueOnError, receivedError: nil, - interceptedError: nil, + interceptedError: &net.OpError{}, }, { errorHandling: AbortOnError, receivedError: &net.OpError{}, - interceptedError: nil, - }, - { - errorHandling: CallbackOnError, - receivedError: nil, interceptedError: &net.OpError{}, }, }