Skip to content

Instantly share code, notes, and snippets.

@jonatan-ivanov
Last active February 4, 2022 18:42
Show Gist options
  • Save jonatan-ivanov/407064be2270b648674c013434ed1e45 to your computer and use it in GitHub Desktop.
Save jonatan-ivanov/407064be2270b648674c013434ed1e45 to your computer and use it in GitHub Desktop.
Trick for updating a gauge less frequently
// 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