Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomTracingStatementInterceptor implements StatementInterceptorV2 {
- private final ThreadLocal<Span> currentSpanInScope = new ThreadLocal<>();
- @Override
- @Nullable
- public ResultSetInternalMethods preProcess(String sql, Statement interceptedStatement,
- Connection connection) {
- final Tracer tracer = Tracing.currentTracer();
- if (tracer == null) {
- return null;
- }
- final Span span = tracer.nextSpan();
- if (span.isNoop()) {
- return null;
- }
- currentSpanInScope.set(span);
- // When running a prepared statement, sql will be null and we must fetch the sql from the statement itself
- if (interceptedStatement instanceof PreparedStatement) {
- sql = ((PreparedStatement) interceptedStatement).getPreparedSql();
- }
- final int spaceIndex = sql.indexOf(' '); // Allow span names of single-word statements like COMMIT
- span.kind(Span.Kind.CLIENT).name(spaceIndex == -1 ? sql : sql.substring(0, spaceIndex));
- span.tag("sql.query", sql);
- parseServerIpAndPort(connection, span);
- span.start();
- return null;
- }
- @Override
- @Nullable
- public ResultSetInternalMethods postProcess(String sql, Statement interceptedStatement,
- ResultSetInternalMethods originalResultSet,
- Connection connection, int warningCount,
- boolean noIndexUsed, boolean noGoodIndexUsed,
- SQLException statementException) {
- final Span span = currentSpanInScope.get();
- currentSpanInScope.remove();
- if (span == null || span.isNoop()) {
- return null;
- }
- if (statementException != null) {
- span.tag("error", Integer.toString(statementException.getErrorCode()));
- }
- span.finish();
- return null;
- }
- /**
- * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the
- * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent.
- */
- static void parseServerIpAndPort(Connection connection, Span span) {
- try {
- final URI url = URI.create(connection.getMetaData().getURL().substring(5)); // strip "jdbc:"
- String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName");
- if (remoteServiceName == null || remoteServiceName.isEmpty()) {
- final String databaseName = connection.getCatalog();
- if (databaseName != null && !databaseName.isEmpty()) {
- remoteServiceName = "mysql-" + databaseName;
- } else {
- remoteServiceName = "mysql";
- }
- }
- span.remoteServiceName(remoteServiceName);
- final String host = connection.getHost();
- if (host != null) {
- span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort());
- }
- } catch (Exception e) {
- // remote address is optional
- }
- }
- @Override
- public boolean executeTopLevelOnly() {
- return true; // True means that we don't get notified about queries that other interceptors issue
- }
- @Override
- public void init(Connection conn, Properties props) {
- // Don't care
- }
- @Override
- public void destroy() {
- // Don't care
- }
- }
Add Comment
Please, Sign In to add comment