Last active
February 4, 2022 18:42
-
-
Save jonatan-ivanov/407064be2270b648674c013434ed1e45 to your computer and use it in GitHub Desktop.
Trick for updating a gauge less frequently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from Jon Schneider | |
public class GaugeUpdateTrick { | |
public static void main(String[] args) { | |
AtomicLong lastCalculatedTime = new AtomicLong(0); | |
AtomicLong gaugeValue = new AtomicLong(0); | |
Gauge.builder("expensive", () -> { | |
long curr = System.nanoTime(); | |
return gaugeValue.updateAndGet(old -> | |
lastCalculatedTime.updateAndGet(ts -> curr - ts > 10_000 ? curr : ts) == curr ? 0L /* new */ : old); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment