Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphite: refactor error handling #1263

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we can't have another package main here 🤔


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
47 changes: 34 additions & 13 deletions prometheus/graphite/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type Config struct {
// logged regardless of the configured ErrorHandling provided Logger
// is not nil.
ErrorHandling HandlerErrorHandling

// ErrorCallbackFunc is a callback function that can be executed when error is occurred
ErrorCallbackFunc ErrorCallbackFunc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

}

// Bridge pushes metrics to the configured Graphite server.
Expand All @@ -89,8 +92,9 @@ type Bridge struct {
interval time.Duration
timeout time.Duration

errorHandling HandlerErrorHandling
logger Logger
errorHandling HandlerErrorHandling
errorCallbackFunc ErrorCallbackFunc
logger Logger

g prometheus.Gatherer
}
Expand All @@ -102,6 +106,9 @@ type Logger interface {
Println(v ...interface{})
}

// 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) {
b := &Bridge{}
Expand Down Expand Up @@ -142,6 +149,10 @@ func NewBridge(c *Config) (*Bridge, error) {

b.errorHandling = c.ErrorHandling

if c.ErrorCallbackFunc != nil {
b.errorCallbackFunc = c.ErrorCallbackFunc
}

return b, nil
}

Expand All @@ -164,19 +175,29 @@ func (b *Bridge) Run(ctx context.Context) {

// Push pushes Prometheus metrics to the configured Graphite server.
func (b *Bridge) Push() error {
mfs, err := b.g.Gather()
if err != nil || len(mfs) == 0 {
switch b.errorHandling {
case AbortOnError:
return err
case ContinueOnError:
if b.logger != nil {
b.logger.Println("continue on error:", err)
}
default:
panic("unrecognized error handling value")
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be safe to leave that panic in, especially if we would add validation to NewBridge for it, WDYT?

}
}
return nil
}

func (b *Bridge) push() error {
mfs, err := b.g.Gather()
if err != nil {
return err
}
if len(mfs) == 0 {
return nil
}

conn, err := net.DialTimeout("tcp", b.url, b.timeout)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions prometheus/graphite/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,43 @@ func ExampleBridge() {
// Start pushing metrics to Graphite in the Run() loop.
b.Run(ctx)
}

func TestErrorHandling(t *testing.T) {
var testCases = []struct {
errorHandling HandlerErrorHandling
receivedError error
interceptedError error
}{
{
errorHandling: ContinueOnError,
receivedError: nil,
interceptedError: &net.OpError{},
},
{
errorHandling: AbortOnError,
receivedError: &net.OpError{},
interceptedError: &net.OpError{},
},
}

for _, testCase := range testCases {
var interceptedError error
c := &Config{
URL: "localhost",
ErrorHandling: testCase.errorHandling,
ErrorCallbackFunc: func(err error) { interceptedError = err },
}
b, err := NewBridge(c)
if err != nil {
t.Fatal(err)
}

receivedError := b.Push()
if reflect.TypeOf(receivedError) != reflect.TypeOf(testCase.receivedError) {
t.Errorf("expected to receive: %T, received: %T", testCase.receivedError, receivedError)
}
if reflect.TypeOf(interceptedError) != reflect.TypeOf(testCase.interceptedError) {
t.Errorf("expected to intercept: %T, intercepted: %T", testCase.interceptedError, interceptedError)
}
}
}