Guest User

Untitled

a guest
Aug 31st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. public class CustomTracingStatementInterceptor implements StatementInterceptorV2 {
  2.  
  3. private final ThreadLocal<Span> currentSpanInScope = new ThreadLocal<>();
  4.  
  5. @Override
  6. @Nullable
  7. public ResultSetInternalMethods preProcess(String sql, Statement interceptedStatement,
  8. Connection connection) {
  9. final Tracer tracer = Tracing.currentTracer();
  10. if (tracer == null) {
  11. return null;
  12. }
  13. final Span span = tracer.nextSpan();
  14. if (span.isNoop()) {
  15. return null;
  16. }
  17.  
  18. currentSpanInScope.set(span);
  19. // When running a prepared statement, sql will be null and we must fetch the sql from the statement itself
  20. if (interceptedStatement instanceof PreparedStatement) {
  21. sql = ((PreparedStatement) interceptedStatement).getPreparedSql();
  22. }
  23. final int spaceIndex = sql.indexOf(' '); // Allow span names of single-word statements like COMMIT
  24. span.kind(Span.Kind.CLIENT).name(spaceIndex == -1 ? sql : sql.substring(0, spaceIndex));
  25. span.tag("sql.query", sql);
  26. parseServerIpAndPort(connection, span);
  27. span.start();
  28. return null;
  29. }
  30.  
  31. @Override
  32. @Nullable
  33. public ResultSetInternalMethods postProcess(String sql, Statement interceptedStatement,
  34. ResultSetInternalMethods originalResultSet,
  35. Connection connection, int warningCount,
  36. boolean noIndexUsed, boolean noGoodIndexUsed,
  37. SQLException statementException) {
  38. final Span span = currentSpanInScope.get();
  39. currentSpanInScope.remove();
  40. if (span == null || span.isNoop()) {
  41. return null;
  42. }
  43.  
  44. if (statementException != null) {
  45. span.tag("error", Integer.toString(statementException.getErrorCode()));
  46. }
  47. span.finish();
  48. return null;
  49. }
  50.  
  51. /**
  52. * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the
  53. * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent.
  54. */
  55. static void parseServerIpAndPort(Connection connection, Span span) {
  56. try {
  57. final URI url = URI.create(connection.getMetaData().getURL().substring(5)); // strip "jdbc:"
  58. String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName");
  59. if (remoteServiceName == null || remoteServiceName.isEmpty()) {
  60. final String databaseName = connection.getCatalog();
  61. if (databaseName != null && !databaseName.isEmpty()) {
  62. remoteServiceName = "mysql-" + databaseName;
  63. } else {
  64. remoteServiceName = "mysql";
  65. }
  66. }
  67. span.remoteServiceName(remoteServiceName);
  68. final String host = connection.getHost();
  69. if (host != null) {
  70. span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort());
  71. }
  72. } catch (Exception e) {
  73. // remote address is optional
  74. }
  75. }
  76.  
  77. @Override
  78. public boolean executeTopLevelOnly() {
  79. return true; // True means that we don't get notified about queries that other interceptors issue
  80. }
  81.  
  82. @Override
  83. public void init(Connection conn, Properties props) {
  84. // Don't care
  85. }
  86.  
  87. @Override
  88. public void destroy() {
  89. // Don't care
  90. }
  91. }
Add Comment
Please, Sign In to add comment