fix ErrorCallbackFunc

Signed-off-by: Darya Melentsova <ifireice@gmail.com>
This commit is contained in:
Darya Melentsova 2023-06-21 08:49:59 +03:00
parent 8bc96219a0
commit b801f06e31
3 changed files with 56 additions and 17 deletions

View File

@ -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 (

View File

@ -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
}

View File

@ -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{},
},
}