public Connection monitorGetConnection(Connection connection, Object dataSource, long duration) throws SQLException { if (active && dataSource instanceof DataSource && !(connection instanceof P6Proxy)) { return ConnectionWrapper.wrap(connection, jdbcEventListener, ConnectionInformation.fromDataSource((DataSource) dataSource, connection, duration)); } else { return connection; } }
@Override public void onConnectionWrapped(ConnectionInformation connectionInformation) { final Metric2Registry metricRegistry = corePlugin.getMetricRegistry(); // at the moment stagemonitor only supports monitoring connections initiated via a DataSource if (connectionInformation.getDataSource() instanceof DataSource && corePlugin.isInitialized()) { DataSource dataSource = (DataSource) connectionInformation.getDataSource(); ensureUrlExistsForDataSource(dataSource, connectionInformation.getConnection()); MetaData metaData = dataSourceUrlMap.get(dataSource); metricRegistry.timer(getConnectionTemplate.build(metaData.serviceName)).update(connectionInformation.getTimeToGetConnectionNs(), TimeUnit.NANOSECONDS); final Span span = TracingPlugin.getCurrentSpan(); final Long connectionWrappedCountSum = incrementAndGetContextValue(span, CONNECTION_WRAPPED_COUNT_ATTRIBUTE, 1L); span.setTag("jdbc_get_connection_count", connectionWrappedCountSum); final double timeToGetConnectionMs = connectionInformation.getTimeToGetConnectionNs() / MILLISECOND_IN_NANOS; final Double connectionWaitTimeMsSum = incrementAndGetContextValue(span, TIME_TO_GET_CONNECTION_MS_ATTRIBUTE, timeToGetConnectionMs); span.setTag("jdbc_connection_wait_time_ms", connectionWaitTimeMsSum); } }
/** * Creates a new {@link ConnectionInformation} instance for a {@link Connection} which will be obtained via a * {@link PooledConnection} * * @param pooledConnection the {@link PooledConnection} which created the {@link #connection} * @return a new {@link ConnectionInformation} instance */ public static ConnectionInformation fromPooledConnection(PooledConnection pooledConnection) { final ConnectionInformation connectionInformation = new ConnectionInformation(); connectionInformation.pooledConnection = pooledConnection; return connectionInformation; }
final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = ((DataSource) realDataSource).getConnection(); connectionInformation.setConnection(conn); if (conn.getMetaData() != null) { connectionInformation.setUrl(conn.getMetaData().getURL()); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e;
@Override public Connection getConnection(String username, String password) throws SQLException { if (realDataSource == null) { bindDataSource(); } final long start = System.nanoTime(); if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = ((DataSource) realDataSource).getConnection(username, password); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
final ConnectionInformation connectionInformation = ConnectionInformation.fromDriver(passThru); connectionInformation.setUrl(url); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = passThru.connect(extractRealUrl(url), properties); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e;
@Test public void testResultSetInformation() throws Exception { final ConnectionInformation connectionInformation = connection.getConnectionInformation(); assertSame(connection.getDelegate(), connectionInformation.getConnection()); assertNotNull(connectionInformation.getDriver()); assertNotEquals(P6SpyDriver.class, connectionInformation.getDriver().getClass()); assertTrue(connectionInformation.getTimeToGetConnectionNs() > 0); } }
@Override public Connection getConnection() throws SQLException { final long start = System.nanoTime(); final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromPooledConnection(passthru); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = passthru.getConnection(); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
/** * Uses {@link ThreadLocalSpan} as there's no attribute namespace shared between callbacks, but * all callbacks happen on the same thread. * * <p>Uses {@link ThreadLocalSpan#CURRENT_TRACER} and this interceptor initializes before tracing. */ @Override public void onBeforeAnyExecute(StatementInformation info) { String sql = includeParameterValues ? info.getSqlWithValues() : info.getSql(); // don't start a span unless there is SQL as we cannot choose a relevant name without it if (sql == null || sql.isEmpty()) return; // Gets the next span (and places it in scope) so code between here and postProcess can read it Span span = ThreadLocalSpan.CURRENT_TRACER.next(); if (span == null || span.isNoop()) return; span.kind(Span.Kind.CLIENT).name(sql.substring(0, sql.indexOf(' '))); span.tag("sql.query", sql); parseServerIpAndPort(info.getConnectionInformation().getConnection(), span); span.start(); }
@Override public void onAfterAnyExecute(StatementInformation statementInformation, long timeElapsedNanos, SQLException e) { final Scope activeScope = tracingPlugin.getTracer().scopeManager().active(); if (activeScope != null) { final Span span = activeScope.span(); if (statementInformation.getConnectionInformation().getDataSource() instanceof DataSource && jdbcPlugin.isCollectSql()) { MetaData metaData = dataSourceUrlMap.get(statementInformation.getConnectionInformation().getDataSource()); Tags.PEER_SERVICE.set(span, metaData.serviceName); span.setTag("db.type", metaData.productName); span.setTag("db.user", metaData.userName); if (StringUtils.isNotEmpty(statementInformation.getSql())) { String sql = getSql(statementInformation.getSql(), statementInformation.getSqlWithValues()); Profiler.addIOCall(sql, timeElapsedNanos); span.setTag(AbstractExternalRequest.EXTERNAL_REQUEST_METHOD, getMethod(sql)); span.setTag(DB_STATEMENT, sql); } } tracingPlugin.getRequestMonitor().monitorStop(); } }
@Override public void onBeforeRollback(ConnectionInformation connectionInformation) { if (P6OutageOptions.getActiveInstance().getOutageDetection()) { P6OutageDetector.INSTANCE.registerInvocation(this, System.nanoTime(), "rollback", "", "", connectionInformation.getUrl()); } }
@Override public void onConnectionWrapped(ConnectionInformation connectionInformation) { onConnectionWrappedCalled = true; assertEquals(42, connectionInformation.getTimeToGetConnectionNs()); } }, //
protected void logElapsed(Loggable loggable, long timeElapsedNanos, Category category, SQLException e) { P6LogQuery.logElapsed(loggable.getConnectionInformation().getConnectionId(), timeElapsedNanos, category, loggable); } }
final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = ((DataSource) realDataSource).getConnection(); connectionInformation.setConnection(conn); if (conn.getMetaData() != null) { connectionInformation.setUrl(conn.getMetaData().getURL()); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e;
@Override public Connection getConnection(String username, String password) throws SQLException { if (realDataSource == null) { bindDataSource(); } final long start = System.nanoTime(); if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = ((DataSource) realDataSource).getConnection(username, password); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
final ConnectionInformation connectionInformation = ConnectionInformation.fromDriver(passThru); connectionInformation.setUrl(url); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = passThru.connect(extractRealUrl(url), properties); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e;
@Override public Connection getConnection() throws SQLException { final long start = System.nanoTime(); final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromPooledConnection(passthru); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = passthru.getConnection(); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
@Override public boolean matches(ConnectionInformation connectionInformation) { return connectionInformation.getConnection() == null; } });
@Override public void onBeforeGetConnection(ConnectionInformation connectionInformation) { String dataSourceName = dataSourceNameResolver.resolveDataSourceName(connectionInformation.getDataSource()); strategy.beforeGetConnection(connectionInformation, dataSourceName); }
@Override public void onBeforeCommit(ConnectionInformation connectionInformation) { if (P6OutageOptions.getActiveInstance().getOutageDetection()) { P6OutageDetector.INSTANCE.registerInvocation(this, System.nanoTime(), "commit", "", "", connectionInformation.getUrl()); } }