newrelic / newrelic-java-agent

The New Relic Java agent
Apache License 2.0
200 stars 143 forks source link

Investigate inaccuracies with r2dbc-postgresql #1558

Open meiao opened 11 months ago

meiao commented 11 months ago

Description

When a connection is created, it makes some queries on which the instrumentation starts a segment but fails to end it.

This causes the transaction to last longer until the segment times out, making the time measurements inaccurate.

All versions of the instrumentation are affected by this.

workato-integration[bot] commented 11 months ago

https://new-relic.atlassian.net/browse/NR-172086

meiao commented 7 months ago

When a new connection is created, a query (not requested by the code) is executed. But for some reason, the doFinally in the request wrapper is not being executed:

    public static Flux<PostgresqlResult> wrapRequest(Flux<PostgresqlResult> request, ...) {
        //...
                Segment segment = transaction.startSegment("execute");
                return request
                        .doOnSubscribe(reportExecution(sql, connectionConfiguration, segment))
                        .doFinally((type) -> segment.end());
    }

https://github.com/newrelic/newrelic-java-agent/blob/624ff4ce636159556dfd8a0695a51fa4055752bc/instrumentation/r2dbc-postgresql-0.9.0/src/main/java/io/r2dbc/postgresql/R2dbcUtils.java#L26

This causes every transaction with a new connection to be held until the segment times out. Also since the segment was not ended, the whole time of the segment shows up in the transaction.

When a transaction uses a connection created outside of it, for instance as part of a connection pool, then this inaccuracy will not happen.