Skip to content

Commit

Permalink
feat: Added backend metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Feb 16, 2023
1 parent e2a564f commit ff10404
Show file tree
Hide file tree
Showing 8 changed files with 1,450 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
public class RedisRowSetCache implements RowSetCache {

private static final String METER_PREFIX = "cache.";
private static final String METER_GETS = METER_PREFIX + "gets";
private static final String METER_PUTS = METER_PREFIX + "puts";
private static final String METER_RESULT_TAG = "result";
private static final String METER_MISS_TAG = "miss";
private static final String METER_HIT_TAG = "hit";
private static final String METER_GET = METER_PREFIX + "get";
private static final String METER_PUT = METER_PREFIX + "put";
private static final String TAG_RESULT = "result";
private static final String TAG_MISS = "miss";
private static final String TAG_HIT = "hit";

private final StatefulRedisConnection<String, RowSet> connection;
private final String prefix;
Expand All @@ -32,10 +32,10 @@ public RedisRowSetCache(StatefulRedisModulesConnection<String, RowSet> connectio
LettuceAssert.notNull(meterRegistry, "Meter registry must not be null");
this.connection = connection;
this.prefix = prefix;
this.getTimer = meterRegistry.timer(METER_GETS);
this.putTimer = meterRegistry.timer(METER_PUTS);
this.missCounter = meterRegistry.counter(METER_GETS, METER_RESULT_TAG, METER_MISS_TAG);
this.hitCounter = meterRegistry.counter(METER_GETS, METER_RESULT_TAG, METER_HIT_TAG);
this.getTimer = Timer.builder(METER_GET).publishPercentiles(0.9, 0.99).register(meterRegistry);
this.putTimer = Timer.builder(METER_PUT).publishPercentiles(0.9, 0.99).register(meterRegistry);
this.missCounter = Counter.builder(METER_GET).tag(TAG_RESULT, TAG_MISS).register(meterRegistry);
this.hitCounter = Counter.builder(METER_GET).tag(TAG_RESULT, TAG_HIT).register(meterRegistry);
}

private String key(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected String key(Query query) {

@Override
public ResultSet executeQuery() throws SQLException {
return executeQuery(sql, ((PreparedStatement) statement)::executeQuery);
return executeQuery(sql, s -> ((PreparedStatement) statement).executeQuery());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@

public class SmartStatement implements Statement {

private static final String QUERY_TIMER_ID = "queries";
private static final String METER_QUERY = "query";
private static final String METER_DB = "db";

private final SmartConnection connection;
protected final Statement statement;
private final Timer queryTimer;
private final Timer dbTimer;
private long executeDuration = 0;
private Query query;
private RowSet rowSet;

public SmartStatement(SmartConnection connection, Statement statement) {
this.connection = connection;
this.statement = statement;
this.queryTimer = connection.getMeterRegistry().timer(QUERY_TIMER_ID);
this.queryTimer = Timer.builder(METER_QUERY).publishPercentiles(0.9, 0.99)
.register(connection.getMeterRegistry());
this.dbTimer = Timer.builder(METER_DB).publishPercentiles(0.9, 0.99).register(connection.getMeterRegistry());
}

@Override
Expand All @@ -47,27 +51,27 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {

@Override
public ResultSet executeQuery(String sql) throws SQLException {
return executeQuery(sql, () -> statement.executeQuery(sql));
return executeQuery(sql, statement::executeQuery);
}

protected String key(Query query) {
return query.getKey();
}

protected ResultSet executeQuery(String sql, ResultSetProvider execution) throws SQLException {
protected ResultSet executeQuery(String sql, QueryExecutable executeQuery) throws SQLException {
try {
return queryTimer.recordCallable(() -> {
Query query = connection.getQuery(sql);
connection.getRuleSession().fire(query);
String key = key(query);
if (query.isCaching()) {
Query sqlQuery = connection.getQuery(sql);
connection.getRuleSession().fire(sqlQuery);
String key = key(sqlQuery);
if (sqlQuery.isCaching()) {
RowSet cachedRowSet = connection.getRowSetCache().get(key);
if (cachedRowSet != null) {
return cachedRowSet;
}
}
ResultSet resultSet = execution.get();
return cacheResultSet(key, query, resultSet);
ResultSet resultSet = dbTimer.recordCallable(() -> executeQuery.executeQuery(sql));
return cacheResultSet(key, sqlQuery, resultSet);
});
} catch (SQLException e) {
throw e;
Expand Down Expand Up @@ -95,9 +99,9 @@ private ResultSet cacheResultSet(String key, Query query, ResultSet resultSet) t
return resultSet;
}

protected interface ResultSetProvider {
protected interface QueryExecutable {

ResultSet get() throws SQLException;
ResultSet executeQuery(String sql) throws SQLException;

}

Expand All @@ -106,12 +110,12 @@ protected interface Executable {
boolean execute() throws SQLException;
}

protected boolean execute(String sql, Executable execution) throws SQLException {
protected boolean execute(String sql, Executable executable) throws SQLException {
long startTime = System.nanoTime();
try {
query = connection.getQuery(sql);
connection.getRuleSession().fire(query);
return execute(query.getKey(), query, execution::execute);
return execute(query.getKey(), query, executable::execute);
} finally {
executeDuration += System.nanoTime() - startTime;
}
Expand All @@ -124,8 +128,14 @@ private boolean execute(String key, Query query, Executable executable) throws S
return true;
}
}
return executable.execute();

try {
return dbTimer.recordCallable(executable::execute);
} catch (SQLException e) {
throw e;
} catch (Exception e) {
// Should not happen but rethrow anyway
throw new SQLException(e);
}
}

@Override
Expand Down
Loading

0 comments on commit ff10404

Please sign in to comment.