Skip to content

Commit

Permalink
refactor: avoid repeating meter ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Jun 30, 2022
1 parent 15ee6c4 commit 7900a43
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ abstract class AbstractResultSetCache implements ResultSetCache {

private static final Logger log = Logger.getLogger(AbstractResultSetCache.class.getName());

private static final String METER_PREFIX = "metrics.cache.";
private static final String METER_GETS = METER_PREFIX + "gets";
private static final String METER_PUTS = METER_PREFIX + "puts";

private final Timer getTimer;
private final Timer putTimer;
private final Counter missCounter;
private final Counter hitCounter;

protected AbstractResultSetCache(MeterRegistry meterRegistry) {
this.getTimer = Timer.builder("metrics.cache.gets").register(meterRegistry);
this.putTimer = Timer.builder("metrics.cache.puts").register(meterRegistry);
this.missCounter = Counter.builder("metrics.cache.gets").tag("result", "miss").register(meterRegistry);
this.hitCounter = Counter.builder("metrics.cache.gets").tag("result", "hit").register(meterRegistry);
this.getTimer = Timer.builder(METER_GETS).register(meterRegistry);
this.putTimer = Timer.builder(METER_PUTS).register(meterRegistry);
this.missCounter = Counter.builder(METER_GETS).tag("result", "miss").register(meterRegistry);
this.hitCounter = Counter.builder(METER_GETS).tag("result", "hit").register(meterRegistry);
}

@Override
Expand All @@ -31,7 +35,7 @@ public Optional<ResultSet> get(String key) {
try {
resultSet = getTimer.recordCallable(() -> doGet(key));
} catch (Exception e) {
log.log(Level.SEVERE, "Could not retrieve key " + key, e);
log.log(Level.SEVERE, String.format("Could not retrieve key %s", key), e);
return Optional.empty();
}
if (resultSet == null) {
Expand All @@ -52,7 +56,7 @@ public void put(String key, long ttl, ResultSet resultSet) {
try {
putTimer.recordCallable(() -> doPut(key, ttl, resultSet));
} catch (Exception e) {
log.log(Level.SEVERE, "Could not store key " + key, e);
log.log(Level.SEVERE, String.format("Could not store key %s", key), e);
}
}

Expand Down

0 comments on commit 7900a43

Please sign in to comment.