question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

support Autocloseable with Timer

See original GitHub issue

I’m migrating from the prometheus API and changing the Timer code is quite cumbersome. In Prometheus, I could do the following:

public void myTimedMethod() throws CheckedException {
    try(timer.startTimer()) {
        methodThatThrowsCheckedException();
    }
}

However, Micrometer’s Timer objects are not Autocloseable. The methods I see in Timer, record() and recordCallable(), take lambda functions, but lambda functions do not allow throwing checked exceptions, leading to lots of annoying boilerplate (I’m following section 3.1 from this Baeldung article). recordCallable() explicitly declares the caller as throwing Exception, which is also not good because now the code I write has to handle the general Exception instead of my specific exception class.

Because I really prefer the autocloseable usage, I ended up writing a little wrapper class:

public static class TimerCloser implements AutoCloseable {
    private final Timer timer;
    private final Timer.Sample sample;

    public TimerCloser(Timer t) {
        this.timer = t;
        this.sample = Timer.start();
    }

    public void close(){
        sample.stop(timer);
    }
}

And I use it like this:

try(TimerCloser closer = new TimerCloser(myTimer)) {...

It would be much easier for me if the I could call myTimer.start() and that would return an autocloseable class similar to Timer.Sample, and then the close() method would call Timer.Sample.stop(). We can’t just change Timer.Sample to be autocloseable, unfortunately, because it contains no reference to the timer metric to write the results to.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
RichMacDonaldcommented, May 9, 2020

GC ramifications of many short-lived Sample objects

I’ve since become more educated, specifically from https://vimeo.com/181925278 and testing with JITWatch. If your AutoCloseableObject is final and stays simple, the VM will not even allocate an extra object, so there is literally no GC penalty at all. The impact is the same as if the AutoCloseableObject fields were defined as local fields in your method.

0reactions
jkschneidercommented, May 10, 2020

all the fields in the Timer.Sample will become local variables in the caller method

So fascinating, I’ve never heard this! Thanks for taking the time to explain. I guess let’s figure out a way to get this done?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the best way to release resources after java.util.Timer?
I can easily solve that by removing "implements AutoCloseable" from the ProcessQueues class (and removing the @Override annotation). But then I ...
Read more >
AutoCloseable (Java Platform SE 8 ) - Oracle Help Center
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try -with-resources statement.
Read more >
The AutoCloseable Interface. A Bit of Background | by Priya Patil
An Object that implements the AutoCloseable interface holds on to a resource until it is done using it within a try block and...
Read more >
try with resources and timers... - Google Groups
In our code, we wrap Timer usage with our own wrapper and have made that object's returned TimerContext-wrapper implement AutoCloseable, ...
Read more >
Closing Java Streams with AutoCloseable - Mike my bytes
Unfortunately, this change (probably due to backward compatibility reasons) did not introduce any compile-time checks, so such resource leaks ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found