fix ErrorCallbackFunc
Signed-off-by: Darya Melentsova <ifireice@gmail.com>
This commit is contained in:
parent
8bc96219a0
commit
b801f06e31
|
@ -14,6 +14,54 @@
|
||||||
// A simple example of how to record a latency metric with exemplars, using a fictional id
|
// A simple example of how to record a latency metric with exemplars, using a fictional id
|
||||||
// as a prometheus label.
|
// 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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -50,9 +50,6 @@ const (
|
||||||
|
|
||||||
// Abort the push to Graphite upon the first error encountered.
|
// Abort the push to Graphite upon the first error encountered.
|
||||||
AbortOnError
|
AbortOnError
|
||||||
|
|
||||||
// Execute callback function on error.
|
|
||||||
CallbackOnError
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config defines the Graphite bridge config.
|
// Config defines the Graphite bridge config.
|
||||||
|
@ -84,7 +81,7 @@ type Config struct {
|
||||||
ErrorHandling HandlerErrorHandling
|
ErrorHandling HandlerErrorHandling
|
||||||
|
|
||||||
// ErrorCallbackFunc is a callback function that can be executed when error is occurred
|
// 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.
|
// Bridge pushes metrics to the configured Graphite server.
|
||||||
|
@ -96,7 +93,7 @@ type Bridge struct {
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
|
||||||
errorHandling HandlerErrorHandling
|
errorHandling HandlerErrorHandling
|
||||||
errorCallbackFunc CallbackFunc
|
errorCallbackFunc ErrorCallbackFunc
|
||||||
logger Logger
|
logger Logger
|
||||||
|
|
||||||
g prometheus.Gatherer
|
g prometheus.Gatherer
|
||||||
|
@ -109,8 +106,8 @@ type Logger interface {
|
||||||
Println(v ...interface{})
|
Println(v ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallbackFunc is a special type for callback functions
|
// ErrorCallbackFunc is a special type for callback functions
|
||||||
type CallbackFunc func(error)
|
type ErrorCallbackFunc func(error)
|
||||||
|
|
||||||
// NewBridge returns a pointer to a new Bridge struct.
|
// NewBridge returns a pointer to a new Bridge struct.
|
||||||
func NewBridge(c *Config) (*Bridge, error) {
|
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.
|
// Push pushes Prometheus metrics to the configured Graphite server.
|
||||||
func (b *Bridge) Push() error {
|
func (b *Bridge) Push() error {
|
||||||
err := b.push()
|
err := b.push()
|
||||||
|
if b.errorCallbackFunc != nil {
|
||||||
|
b.errorCallbackFunc(err)
|
||||||
|
}
|
||||||
switch b.errorHandling {
|
switch b.errorHandling {
|
||||||
case AbortOnError:
|
case AbortOnError:
|
||||||
return err
|
return err
|
||||||
|
@ -186,10 +186,6 @@ func (b *Bridge) Push() error {
|
||||||
if b.logger != nil {
|
if b.logger != nil {
|
||||||
b.logger.Println("continue on error:", err)
|
b.logger.Println("continue on error:", err)
|
||||||
}
|
}
|
||||||
case CallbackOnError:
|
|
||||||
if b.errorCallbackFunc != nil {
|
|
||||||
b.errorCallbackFunc(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -477,16 +477,11 @@ func TestErrorHandling(t *testing.T) {
|
||||||
{
|
{
|
||||||
errorHandling: ContinueOnError,
|
errorHandling: ContinueOnError,
|
||||||
receivedError: nil,
|
receivedError: nil,
|
||||||
interceptedError: nil,
|
interceptedError: &net.OpError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
errorHandling: AbortOnError,
|
errorHandling: AbortOnError,
|
||||||
receivedError: &net.OpError{},
|
receivedError: &net.OpError{},
|
||||||
interceptedError: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
errorHandling: CallbackOnError,
|
|
||||||
receivedError: nil,
|
|
||||||
interceptedError: &net.OpError{},
|
interceptedError: &net.OpError{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue