Profiling / recording query timing data with QueryInterceptor
Posted by: Johnathan Crawford
Date: October 08, 2019 12:48AM

Hey all,

I am working on rewriting our statement interceptor that logs executed statements into circular memory buffer against MySQL Connector 8.x.

I would need to know the creation time of the query object when passed into the postProcess method or be able to inject some information via a context object available in a pre* method.

Currently I am using a ThreadLocal object to store a unix timestamp however this feels like a hack.

There don't seem to be a great deal of examples on how to use QueryInterceptor , is there something I have overlooked?

Cheers,

Johno

private final ThreadLocal<Long> timeLocal = new ThreadLocal<>();

@Override
public <T extends Resultset> T preProcess(Supplier<String> supplier, Query query) {
timeLocal.set(TimeSource.now());
return null; // don't allow result set substitution
}

@Override
public <T extends Resultset> T postProcess(Supplier<String> supplier, Query query, T t, ServerSession serverSession) {
final Long timeMillis = timeLocal.get();
if (timeMillis == null) {
return null;
}
timeLocal.remove();
final String sql = supplier.get();
if (sql == null || sql.startsWith("SET autocommit=")) {
return null; // don't allow result set substitution
}
getLogCollectorForSchema(schema).addEntry(sql, (TimeSource.now() - timeMillis));
return null; // don't allow result set substitution
}

Options: ReplyQuote




Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.