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

Oracle R2DBC Instrumentation #829

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
25 changes: 25 additions & 0 deletions instrumentation/oracle-r2dbc/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
dependencies {
implementation(project(":agent-bridge"))
implementation(project(":agent-bridge-datastore"))
implementation("com.oracle.database.r2dbc:oracle-r2dbc:0.4.0")
testImplementation("com.h2database:h2:1.4.200")
}

jar {
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.oracle-r2dbc' }
}

verifyInstrumentation {
passesOnly 'com.oracle.database.r2dbc:oracle-r2dbc:[0.0.0,)'
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

site {
title 'Oracle R2DBC'
type 'Datastore'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package oracle.r2dbc.impl;

import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

import java.sql.Connection;

@Weave(type = MatchType.ExactClass, originalName = "oracle.r2dbc.impl.OracleStatementImpl")
final class OracleStatementImpl_Instrumentation {
private final String sql = Weaver.callOriginal();

private final Connection jdbcConnection = Weaver.callOriginal();

public Publisher<OracleResultImpl> execute() {
Flux<OracleResultImpl> request = Weaver.callOriginal();
if(request != null && jdbcConnection != null) {
return R2dbcUtils.wrapRequest(request, sql, jdbcConnection);
}
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package oracle.r2dbc.impl;

import com.newrelic.agent.bridge.NoOpTransaction;
import com.newrelic.agent.bridge.datastore.DatastoreInstanceDetection;
import com.newrelic.agent.bridge.datastore.DatastoreVendor;
import com.newrelic.agent.bridge.datastore.JdbcHelper;
import com.newrelic.agent.bridge.datastore.OperationAndTableName;
import com.newrelic.agent.bridge.datastore.R2dbcObfuscator;
import com.newrelic.agent.bridge.datastore.R2dbcOperation;
import com.newrelic.api.agent.DatastoreParameters;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Segment;
import com.newrelic.api.agent.Transaction;
import org.reactivestreams.Subscription;
import reactor.core.publisher.Flux;

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

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

private static Consumer<Subscription> reportExecution(String sql, Connection jdbcConnection, Segment segment) {
return (subscription) -> {
OperationAndTableName sqlOperation = R2dbcOperation.extractFrom(sql);
InetSocketAddress socketAddress = DatastoreInstanceDetection.getAddressForConnection(jdbcConnection);
String cachedIdentifier = JdbcHelper.getCachedIdentifierForConnection(jdbcConnection);
String identifier = cachedIdentifier != null ? cachedIdentifier : JdbcHelper.parseAndCacheInMemoryIdentifier(jdbcConnection);
if (sqlOperation != null && socketAddress != null && identifier != null) {
segment.reportAsExternal(DatastoreParameters
.product(DatastoreVendor.Oracle.name())
.collection(sqlOperation.getTableName())
.operation(sqlOperation.getOperation())
.instance(socketAddress.getHostName(), socketAddress.getPort())
.databaseName(identifier)
.slowQuery(sql, R2dbcObfuscator.QUERY_CONVERTER)
.build());
}
};
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ include 'instrumentation:okhttp-4.0.0'
include 'instrumentation:okhttp-4.4.0'
include 'instrumentation:openjpa-1.0.0'
include 'instrumentation:openejb-3.0'
include 'instrumentation:oracle-r2dbc'
include 'instrumentation:play-2.3'
include 'instrumentation:play-2.4'
include 'instrumentation:play-2.5'
Expand Down