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

Fix backoff / network client interfaces to implement Stringer #7882

Merged
merged 1 commit into from
Aug 6, 2018

Conversation

andrewvc
Copy link
Contributor

@andrewvc andrewvc commented Aug 6, 2018

Merging https://github.com/elastic/beats/pull/6404/files
Caused 512611a to break.

I assume the earlier commit wasn't rebased before merge. This fixes up the interface for the Redis
backoff client to implement Stringer

@ph
Copy link
Contributor

ph commented Aug 6, 2018

Good finding @andrewvc, it appears to be the stringer interface but actually, the String() method was added to output.Client, @urso This looks like we break the interface in a minor release? I am not sure how strict we are on theses interface.

I did a quick look at the other output, console/kafka and they implement the stringer interface.

@@ -112,3 +113,7 @@ func (b *backoffClient) resetFail() {
b.reason = failNone
b.backoff.Reset()
}

func (b *backoffClient) String() string {
return fmt.Sprintf("BackoffClient: client: %s, reason: %v, backoff: %s", b.client, b.reason, b.backoff)
Copy link

Choose a reason for hiding this comment

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

The backoffClient here is specific to redis. Let's just return b.client.String(). The string is used to identify a client type + endpoint on error. The actual error message message contains the reason. Printing additional state would be more helpful in form of debug messages when the next connection attempt starts/fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@urso as a go noob I'm a little confused by this. In most languages the canonical String()/toString() function is generally for debugging. Usually if you need a specific formatting of an object you have that logic at a higher level.

Are we sure we want to make String()'s implementation tightly coupled to this specific error message?

It seems to me like we should just not have made this stringer-like interface, and had the print function traverse the object graph itself and format things the specific way it would like them to be formatted.

Copy link

Choose a reason for hiding this comment

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

We mostly use String to print and identify an object. In go we also have alternative formatting supporting using the fmt package. When using any fmt.X methods, it checks if your type implements fmt.Formatter. This adds additional formatting capabilities by passing the format pattern to the Formatter. When using fmt.XPrintf, one normally uses:

  • %v: default representation
  • %+v: which asks for more details
  • %#v: which is interpreted by fmt and will add the struct name + print each field by name.

If fmt.Formatter is not implemented, %v will fallback to String() string (fmt.Stringer interface), and %#v will fallback to GoString() string (fmt.GoStringer interface).

See package documentation of fmt.

@ph ph mentioned this pull request Aug 6, 2018
@andrewvc
Copy link
Contributor Author

andrewvc commented Aug 6, 2018

I've done the quick fix @urso recommended in the outdated comment. I think we should merge sooner rather than later since this is breaking all builds, but the usage of specific String() formatting for user-friendly errors is weird. Usually String() in most langs is a debuggable string with more specific context and an accounting of all important object fields.

@urso urso merged commit 551b2a9 into elastic:master Aug 6, 2018
urso pushed a commit to urso/beats that referenced this pull request Aug 29, 2018
urso pushed a commit to urso/beats that referenced this pull request Oct 19, 2018
urso pushed a commit that referenced this pull request Oct 22, 2018
* Log reconnect attempts (#6404)

* Log reconnect attempts (#5715)

* Add identifiers to connections

(cherry picked from commit 06dea8b)

* Add String() to redis backoff client to correctly impl. NetworkClient (#7882)

(cherry picked from commit 551b2a9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants