diff --git a/datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/DataSourceObservationListener.java b/datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/DataSourceObservationListener.java index 9a6b1d6..9725099 100644 --- a/datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/DataSourceObservationListener.java +++ b/datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/DataSourceObservationListener.java @@ -246,9 +246,14 @@ private void handleGetConnectionAfter(MethodExecutionContext executionContext) { Connection connection = (Connection) executionContext.getResult(); Observation.Scope scopeToUse = executionContext.getCustomValue(Observation.Scope.class.getName(), Observation.Scope.class); + ConnectionContext connectionContext = executionContext.getCustomValue(ConnectionContext.class.getName(), + ConnectionContext.class); if (connection == null) { - // Handle closing the observation due to an error from getConnection(). + // Logic when getConnection failed. + // Populate the context with available info. (see gh-44) + connectionContext.setDataSource(dataSource); + // Handle closing the observation. // For normal case, observation is stopped when connection is closed. // see "handleConnectionClose()". if (scopeToUse != null) { @@ -273,8 +278,6 @@ private void handleGetConnectionAfter(MethodExecutionContext executionContext) { String connectionId = connectionInfo.getConnectionId(); this.connectionAttributesManager.put(connectionId, connectionAttributes); - ConnectionContext connectionContext = executionContext.getCustomValue(ConnectionContext.class.getName(), - ConnectionContext.class); populateFromConnectionAttributes(connectionContext, connectionId); // When "getConnection" was successful, a connection is acquired. diff --git a/datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceObservationListenerTests.java b/datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceObservationListenerTests.java index 3cc8295..accfd4b 100644 --- a/datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceObservationListenerTests.java +++ b/datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceObservationListenerTests.java @@ -331,6 +331,13 @@ void connection() throws Exception { @Test void getConnectionFailure() throws Exception { this.registry.observationConfig().observationHandler(new ConnectionTracingObservationHandler(this.tracer)); + // gh-44: make sure datasource is available in observation filter + this.registry.observationConfig().observationFilter((context) -> { + DataSource ds = ((DataSourceBaseContext) context).getDataSource(); + assertThat(ds).as("datasource needs to be available in observation filter").isNotNull(); + return context; + }); + Method getConnection = DataSource.class.getMethod("getConnection"); RuntimeException exception = new RuntimeException();