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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

ifireice
Copy link

@ifireice ifireice commented Apr 30, 2023

Removed Logger interface and make error handling more flexible.

For example, we could use our own logger implementation and have more control over the execution flow

package main

import (
	"context"
	"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)
}

@ifireice ifireice force-pushed the main branch 4 times, most recently from 675545c to 252ec74 Compare May 1, 2023 10:29
@bwplotka
Copy link
Member

bwplotka commented May 3, 2023

I think this is fair improvement, but can we break compatibility there? I don't think so ): cc @beorn7 ?

@beorn7
Copy link
Member

beorn7 commented May 3, 2023

Yeah, this is a breaking change that can only happen in v2. (And even then, all users of "the old way" might not be happy that they have to change things.)

Could you add the more flexible handling in a backwards compatible way?

@ifireice
Copy link
Author

Made error interception native to existing implementation

Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Amazing! It looks now much better, suggested one thing, let me know what you think 🤗

@@ -102,6 +109,9 @@ type Logger interface {
Println(v ...interface{})
}

// CallbackFunc is a special type for callback functions
type CallbackFunc func(error)
Copy link
Member

Choose a reason for hiding this comment

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

If Callback is another option after Abort or Continue, it has to control abort/continue in callback response for full flexibility:

Also let's call it "ErrorCallbackFunc" to be precise?

Suggested change
type CallbackFunc func(error)
type ErrorCallbackFunc func(error) (abort bool)

Alternative is to perhaps remove Callback Handler enum option and just rely on callback function to be filled AND respect continue/abort enum. The former idea gives more flexiblity though. WDYT?

Signed-off-by: ifireice <ifireice@gmail.com>

Signed-off-by: Darya Melentsova <ifireice@gmail.com>
Signed-off-by: Darya Melentsova <ifireice@gmail.com>
Signed-off-by: Darya Melentsova <ifireice@gmail.com>
Signed-off-by: Darya Melentsova <ifireice@gmail.com>
Signed-off-by: Darya Melentsova <ifireice@gmail.com>
Signed-off-by: Darya Melentsova <ifireice@gmail.com>
@stale
Copy link

stale bot commented Sep 17, 2023

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Sep 17, 2023
Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Thanks! Sorry for lag and sounds like some unwanted changes on exemplars got in? Do you mind removing them, checking my comments and rebasing? I think it's good to go otherwise!

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

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

👍🏽

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?

@stale stale bot removed stale labels Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants