Skip to content

Commit

Permalink
fix ErrorCallbackFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
ifireice committed Jun 21, 2023
1 parent 9d3c481 commit c547da2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
48 changes: 48 additions & 0 deletions examples/exemplars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
18 changes: 7 additions & 11 deletions prometheus/graphite/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -96,7 +93,7 @@ type Bridge struct {
timeout time.Duration

errorHandling HandlerErrorHandling
errorCallbackFunc CallbackFunc
errorCallbackFunc ErrorCallbackFunc
logger Logger

g prometheus.Gatherer
Expand All @@ -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) {
Expand Down Expand Up @@ -179,17 +176,16 @@ 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
case ContinueOnError:
if b.logger != nil {
b.logger.Println("continue on error:", err)
}
case CallbackOnError:
if b.errorCallbackFunc != nil {
b.errorCallbackFunc(err)
}
}
return nil
}
Expand Down
7 changes: 1 addition & 6 deletions prometheus/graphite/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
}
Expand Down

0 comments on commit c547da2

Please sign in to comment.