jdbc-observations / datasource-micrometer

Micrometer Observation API instrumentation and Spring Boot 3 Auto-Configuration for JDBC APIs
Apache License 2.0
57 stars 9 forks source link

The list of queries is missing in QueryContext supplied to micrometer's ObservationPredicate #18

Closed dmurat closed 1 year ago

dmurat commented 1 year ago

When the micrometer ObservationPredicate is configured, supplied QueryContext does not contain the list of queries.

Related discussion: https://github.com/jdbc-observations/datasource-micrometer/issues/17

For example, the following test will not pass at the moment:

    @Test
    void queryPredicateGetsExpectedData() throws Exception{
        ObservationPredicate testQueryObservationPredicate = (s, observationContext) -> {
            if (observationContext instanceof QueryContext) {
                QueryContext queryContext = (QueryContext) observationContext;
                List<String> queries = queryContext.getQueries();

                assertThat(queries).isNotEmpty();
                assertThat(queries.get(0)).isEqualTo("SELECT 1");
                return true;
            }
            return true;
        };

        this.registry.observationConfig().observationHandler(new DefaultTracingObservationHandler(this.tracer));
        this.registry.observationConfig().observationPredicate(testQueryObservationPredicate);
        DataSourceObservationListener listener = new DataSourceObservationListener(this.registry);

        Method execute = Statement.class.getMethod("execute", String.class);

        QueryInfo queryInfo = new QueryInfo();
        queryInfo.setQuery("SELECT 1");

        ExecutionInfo executionInfo = new ExecutionInfo();
        executionInfo.setConnectionId("id-1");
        executionInfo.setDataSourceName("myDS");
        executionInfo.setMethod(execute);
        List<QueryInfo> queryInfos = Collections.singletonList(queryInfo);

        listener.beforeQuery(executionInfo, queryInfos);
    }

I will try to create appropriate PR.

dmurat commented 1 year ago

PR is here: https://github.com/jdbc-observations/datasource-micrometer/pull/19

dmurat commented 9 months ago

Hi,

Is it possible to get the 1.0.3 release published? I would very much like to have a fix for this bug. Tnx.

ttddyy commented 9 months ago

@dmurat oh, sorry to kept you so long for getting this change in a release. I just released 1.0.3 which includes this fix.

dmurat commented 9 months ago

No problem, and thank you. You are very kind.