Skip to content

Conversation

@yarosla
Copy link
Contributor

@yarosla yarosla commented Apr 7, 2018

@brian-brazil

Another usability enhancement allowing instead of:

    String v;
    try (Summary.Timer timer = summary.startTimer()) {
      v = someMethod();
    }

to write:

    String v = summary.time(this::someMethod);

This is especially useful when programming (reactive) streams.

Trying to make metric code as unobtrusive as possible.

BTW Micrometer already does that.

}

/**
* Executes callable code (i.e. a Java 8 Lambda) and observes a duration of how long it took to run.
Copy link
Contributor

Choose a reason for hiding this comment

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

e.g.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no example in existing time() method, but I can certainly add one here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant that i.e. is incorrect as it can be any Callable, not just a Java 8 Lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Then on old method this should also be corrected, as I copied text from there.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I must have missed that back when that was added.

* @param timeable Code that is being timed
* @return Result returned by callable.
*/
public <E> E time(Callable<E> timeable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Gauge also has a time() method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add that too.

try {
return timeable.call();
} catch (Exception e) {
throw new RuntimeException(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you wrapping this exception rather than just letting it propagate naturally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cause in most cases there will be no checked exceptions within that Callable.

And I would not make time() method throw checked exception, as this will require try-catch around it, which makes usage in streams quite painful.

Supplier class would be much more appropriate for this method but there is only a Callable alternative in Java 6.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay

@yarosla
Copy link
Contributor Author

yarosla commented Apr 14, 2018

I've fixed all the comments. Don't know how to deal with this DCO thing.

@brian-brazil
Copy link
Contributor

The easiest thing to do is to squash the commits with rebase, and make sure the remaining commit has the signoff.

@yarosla
Copy link
Contributor Author

yarosla commented Apr 21, 2018

I've created new PR #378 from squashed branch.

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.

2 participants