Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instrumentation for r2dbc-postgresql-1.0.0 #1413

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a test that ran without instrumentation. Not sure what was the intent of it.
Removing because it took 8 seconds.

This file was deleted.

4 changes: 2 additions & 2 deletions instrumentation/r2dbc-postgresql-0.9.2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jar {
}

verifyInstrumentation {
passesOnly 'org.postgresql:r2dbc-postgresql:[0.9.2,1.0.0.RELEASE)'
excludeRegex(".*(M1|M2|RC).*")
passesOnly 'org.postgresql:r2dbc-postgresql:[0.9.2,)'
exclude 'org.postgresql:r2dbc-postgresql:1.0.0.RC1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something was odd with this release.
Removed the regex so if there is an incompatibility with any preview of 2.0.0 we will be notified then, instead of when the final version is released.

}

site {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class PostgresqlStatement_Instrumentation {
public Flux<PostgresqlResult> execute() {
Flux<PostgresqlResult> request = Weaver.callOriginal();
if(request != null && parsedSql != null && resources != null) {
return R2dbcUtils.wrapRequest(request, parsedSql.getSql(), resources.getConfiguration());
return R2dbcUtils.wrapRequest(request, parsedSql.getSql(), resources.getClient(), resources.getConfiguration());
}
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,61 @@
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Segment;
import com.newrelic.api.agent.Transaction;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.api.PostgresqlResult;
import io.r2dbc.postgresql.client.Client;
import io.r2dbc.postgresql.client.ReactorNettyClient_Instrumentation;
import org.reactivestreams.Subscription;
import reactor.core.publisher.Flux;
import reactor.netty.Connection;
import reactor.util.annotation.Nullable;

import java.net.InetSocketAddress;
import java.util.function.Consumer;

public class R2dbcUtils {
public static Flux<PostgresqlResult> wrapRequest(Flux<PostgresqlResult> request, String sql, PostgresqlConnectionConfiguration connectionConfiguration) {
public static Flux<PostgresqlResult> wrapRequest(Flux<PostgresqlResult> request, String sql, Client client, PostgresqlConnectionConfiguration connectionConfiguration) {
if(request != null) {
Transaction transaction = NewRelic.getAgent().getTransaction();
if(transaction != null && !(transaction instanceof NoOpTransaction)) {
Segment segment = transaction.startSegment("execute");
return request
.doOnSubscribe(reportExecution(sql, connectionConfiguration, segment))
.doOnSubscribe(reportExecution(sql, client, connectionConfiguration, segment))
.doFinally((type) -> segment.end());
}
}
return request;
}

private static Consumer<Subscription> reportExecution(String sql, PostgresqlConnectionConfiguration connectionConfiguration, Segment segment) {
private static Consumer<Subscription> reportExecution(String sql, Client client, PostgresqlConnectionConfiguration connectionConfiguration, Segment segment) {
return (subscription) -> {
OperationAndTableName sqlOperation = R2dbcOperation.extractFrom(sql);
if (sqlOperation != null) {
InetSocketAddress socketAddress = extractSocketAddress(client);
if (sqlOperation != null && socketAddress != null) {
segment.reportAsExternal(DatastoreParameters
.product(DatastoreVendor.Postgres.name())
.collection(sqlOperation.getTableName())
.operation(sqlOperation.getOperation())
.instance(connectionConfiguration.getHost(), connectionConfiguration.getPort())
.instance(socketAddress.getHostName(), socketAddress.getPort())
.databaseName(connectionConfiguration.getDatabase())
.slowQuery(sql, R2dbcObfuscator.POSTGRES_QUERY_CONVERTER)
.build());
}
};
}

public static @Nullable InetSocketAddress extractSocketAddress(Client client) {
try {
if(client instanceof ReactorNettyClient_Instrumentation) {
ReactorNettyClient_Instrumentation instrumentedClient = (ReactorNettyClient_Instrumentation) client;
Connection clientConnection = instrumentedClient.clientConnection;
if(clientConnection.channel().remoteAddress() != null && clientConnection.channel().remoteAddress() instanceof InetSocketAddress) {
return (InetSocketAddress) clientConnection.channel().remoteAddress();
}
}
return null;
} catch(Exception exception) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.NewField;
import com.newrelic.api.agent.weaver.Weave;
import io.r2dbc.postgresql.client.ConnectionSettings;
import reactor.netty.Connection;

import java.util.Optional;
import java.util.TimeZone;

@Weave(type = MatchType.ExactClass, originalName = "io.r2dbc.postgresql.client.ReactorNettyClient")
public class ReactorNettyClient_Instrumentation {
public abstract class ReactorNettyClient_Instrumentation {
@NewField
public final Connection clientConnection;

private ReactorNettyClient_Instrumentation(Connection connection, ConnectionSettings settings) {
this.clientConnection = connection;
}

}

This file was deleted.

20 changes: 0 additions & 20 deletions instrumentation/r2dbc-postgresql-1.0.0/build.gradle

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading