From 43f72a3e3540dd30757c4e5034741695cf741804 Mon Sep 17 00:00:00 2001 From: idawda Date: Wed, 24 Jul 2024 19:39:55 +0530 Subject: [PATCH] ExternalConnection reporting for R2DBC driver --- .../h2/client/SessionClient_Instrumentation.java | 3 +++ .../MariadbConnectionFactory_Instrumentation.java | 10 +++++++++- .../client/ReactorNettyClient_Instrumentation.java | 2 ++ .../r2dbc/mysql/client/Client_Instrumentation.java | 3 +++ .../impl/OracleConnectionImpl_Instrumentation.java | 13 +++++++++++++ .../client/ReactorNettyClient_Instrumentation.java | 3 +++ 6 files changed, 33 insertions(+), 1 deletion(-) diff --git a/instrumentation-security/r2dbc-h2/src/main/java/io/r2dbc/h2/client/SessionClient_Instrumentation.java b/instrumentation-security/r2dbc-h2/src/main/java/io/r2dbc/h2/client/SessionClient_Instrumentation.java index fcd7c7ae9..435b27868 100644 --- a/instrumentation-security/r2dbc-h2/src/main/java/io/r2dbc/h2/client/SessionClient_Instrumentation.java +++ b/instrumentation-security/r2dbc-h2/src/main/java/io/r2dbc/h2/client/SessionClient_Instrumentation.java @@ -1,6 +1,7 @@ package io.r2dbc.h2.client; import com.newrelic.api.agent.security.NewRelicSecurity; +import com.newrelic.api.agent.security.schema.ExternalConnectionType; import com.newrelic.api.agent.security.schema.R2DBCVendor; import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; @@ -8,9 +9,11 @@ @Weave(type = MatchType.ExactClass, originalName = "io.r2dbc.h2.client.SessionClient") public class SessionClient_Instrumentation { + public SessionClient_Instrumentation(ConnectionInfo connectionInfo, boolean shutdownDatabaseOnClose) { if (NewRelicSecurity.isHookProcessingActive() && !NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) { NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(R2DBCVendor.META_CONST_R2DBC_VENDOR, R2DBCVendor.H2); } + NewRelicSecurity.getAgent().recordExternalConnection(null, -1, connectionInfo.getURL(), null, ExternalConnectionType.DATABASE_CONNECTION.name(), "R2DBC-H2"); } } diff --git a/instrumentation-security/r2dbc-mariadb/src/main/java/org/mariadb/r2dbc/MariadbConnectionFactory_Instrumentation.java b/instrumentation-security/r2dbc-mariadb/src/main/java/org/mariadb/r2dbc/MariadbConnectionFactory_Instrumentation.java index f215eeefd..43968f252 100644 --- a/instrumentation-security/r2dbc-mariadb/src/main/java/org/mariadb/r2dbc/MariadbConnectionFactory_Instrumentation.java +++ b/instrumentation-security/r2dbc-mariadb/src/main/java/org/mariadb/r2dbc/MariadbConnectionFactory_Instrumentation.java @@ -1,6 +1,7 @@ package org.mariadb.r2dbc; import com.newrelic.api.agent.security.NewRelicSecurity; +import com.newrelic.api.agent.security.schema.ExternalConnectionType; import com.newrelic.api.agent.security.schema.R2DBCVendor; import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; @@ -8,12 +9,19 @@ import org.mariadb.r2dbc.api.MariadbConnection; import reactor.core.publisher.Mono; -@Weave(type = MatchType.Interface, originalName = "org.mariadb.r2dbc.MariadbConnectionFactory") +import java.net.SocketAddress; + +@Weave(type = MatchType.ExactClass, originalName = "org.mariadb.r2dbc.MariadbConnectionFactory") public class MariadbConnectionFactory_Instrumentation { + + private final SocketAddress endpoint = Weaver.callOriginal(); + private final MariadbConnectionConfiguration configuration = Weaver.callOriginal(); + public Mono create() { if (NewRelicSecurity.isHookProcessingActive() && !NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) { NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(R2DBCVendor.META_CONST_R2DBC_VENDOR, R2DBCVendor.MARIA_DB); } + NewRelicSecurity.getAgent().recordExternalConnection(configuration.getHost(), configuration.getPort(), endpoint.toString(), null, ExternalConnectionType.DATABASE_CONNECTION.name(), "R2DBC-MARIADB"); return Weaver.callOriginal(); } } diff --git a/instrumentation-security/r2dbc-mssql/src/main/java/io/r2dbc/mssql/client/ReactorNettyClient_Instrumentation.java b/instrumentation-security/r2dbc-mssql/src/main/java/io/r2dbc/mssql/client/ReactorNettyClient_Instrumentation.java index 0f4f29a01..570dce6f2 100644 --- a/instrumentation-security/r2dbc-mssql/src/main/java/io/r2dbc/mssql/client/ReactorNettyClient_Instrumentation.java +++ b/instrumentation-security/r2dbc-mssql/src/main/java/io/r2dbc/mssql/client/ReactorNettyClient_Instrumentation.java @@ -1,6 +1,7 @@ package io.r2dbc.mssql.client; import com.newrelic.api.agent.security.NewRelicSecurity; +import com.newrelic.api.agent.security.schema.ExternalConnectionType; import com.newrelic.api.agent.security.schema.R2DBCVendor; import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; @@ -15,6 +16,7 @@ public static Mono connect(ClientConfiguration configuration if (NewRelicSecurity.isHookProcessingActive() && !NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) { NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(R2DBCVendor.META_CONST_R2DBC_VENDOR, R2DBCVendor.MSSQL); } + NewRelicSecurity.getAgent().recordExternalConnection(configuration.getHost(), configuration.getPort(), null, null, ExternalConnectionType.DATABASE_CONNECTION.name(), "R2DBC-MSSQL"); return Weaver.callOriginal(); } } diff --git a/instrumentation-security/r2dbc-mysql/src/main/java/dev/miku/r2dbc/mysql/client/Client_Instrumentation.java b/instrumentation-security/r2dbc-mysql/src/main/java/dev/miku/r2dbc/mysql/client/Client_Instrumentation.java index cb0a8737c..8dd946823 100644 --- a/instrumentation-security/r2dbc-mysql/src/main/java/dev/miku/r2dbc/mysql/client/Client_Instrumentation.java +++ b/instrumentation-security/r2dbc-mysql/src/main/java/dev/miku/r2dbc/mysql/client/Client_Instrumentation.java @@ -1,6 +1,7 @@ package dev.miku.r2dbc.mysql.client; import com.newrelic.api.agent.security.NewRelicSecurity; +import com.newrelic.api.agent.security.schema.ExternalConnectionType; import com.newrelic.api.agent.security.schema.R2DBCVendor; import com.newrelic.api.agent.weaver.MatchType; import com.newrelic.api.agent.weaver.Weave; @@ -15,11 +16,13 @@ @Weave(type = MatchType.Interface, originalName = "dev.miku.r2dbc.mysql.client.Client") public class Client_Instrumentation { + public static Mono connect( MySqlSslConfiguration ssl, SocketAddress address, boolean tcpKeepAlive, boolean tcpNoDelay, ConnectionContext context, @Nullable Duration connectTimeout) { if (NewRelicSecurity.isHookProcessingActive() && !NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) { NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(R2DBCVendor.META_CONST_R2DBC_VENDOR, R2DBCVendor.MYSQL); } + NewRelicSecurity.getAgent().recordExternalConnection(null, -1, address.toString(), null, ExternalConnectionType.DATABASE_CONNECTION.name(), "R2DBC-MYSQL"); return Weaver.callOriginal(); } } diff --git a/instrumentation-security/r2dbc-oracle/src/main/java/oracle/r2dbc/impl/OracleConnectionImpl_Instrumentation.java b/instrumentation-security/r2dbc-oracle/src/main/java/oracle/r2dbc/impl/OracleConnectionImpl_Instrumentation.java index aaab9038b..fc4b2e017 100644 --- a/instrumentation-security/r2dbc-oracle/src/main/java/oracle/r2dbc/impl/OracleConnectionImpl_Instrumentation.java +++ b/instrumentation-security/r2dbc-oracle/src/main/java/oracle/r2dbc/impl/OracleConnectionImpl_Instrumentation.java @@ -1,16 +1,29 @@ package oracle.r2dbc.impl; import com.newrelic.api.agent.security.NewRelicSecurity; +import com.newrelic.api.agent.security.schema.ExternalConnectionType; import com.newrelic.api.agent.security.schema.R2DBCVendor; +import com.newrelic.api.agent.security.utils.logging.LogLevel; import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.WeaveAllConstructors; +import com.newrelic.api.agent.weaver.Weaver; + +import java.sql.SQLException; @Weave(originalName = "oracle.r2dbc.impl.OracleConnectionImpl") final class OracleConnectionImpl_Instrumentation { + + private final java.sql.Connection jdbcConnection = Weaver.callOriginal(); + @WeaveAllConstructors OracleConnectionImpl_Instrumentation() { if (NewRelicSecurity.isHookProcessingActive() && !NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) { NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(R2DBCVendor.META_CONST_R2DBC_VENDOR, R2DBCVendor.ORACLE); } + try { + NewRelicSecurity.getAgent().recordExternalConnection(null, -1, jdbcConnection.getMetaData().getURL(), null, ExternalConnectionType.DATABASE_CONNECTION.name(), "R2DBC-ORACLE"); + } catch (SQLException e) { + NewRelicSecurity.getAgent().log(LogLevel.WARNING, String.format("Instrumentation library: %s, Error while detecting external connection : %s", "R2DBC-ORACLE", e.getMessage()), e, this.getClass().getName()); + } } } diff --git a/instrumentation-security/r2dbc-postgresql/src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient_Instrumentation.java b/instrumentation-security/r2dbc-postgresql/src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient_Instrumentation.java index 0cffbafab..39d01bc50 100644 --- a/instrumentation-security/r2dbc-postgresql/src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient_Instrumentation.java +++ b/instrumentation-security/r2dbc-postgresql/src/main/java/io/r2dbc/postgresql/client/ReactorNettyClient_Instrumentation.java @@ -1,6 +1,7 @@ package io.r2dbc.postgresql.client; import com.newrelic.api.agent.security.NewRelicSecurity; +import com.newrelic.api.agent.security.schema.ExternalConnectionType; import com.newrelic.api.agent.security.schema.R2DBCVendor; import com.newrelic.api.agent.weaver.Weave; import com.newrelic.api.agent.weaver.Weaver; @@ -10,10 +11,12 @@ @Weave(originalName = "io.r2dbc.postgresql.client.ReactorNettyClient") public class ReactorNettyClient_Instrumentation { + public static Mono connect(SocketAddress socketAddress, ConnectionSettings settings){ if (NewRelicSecurity.isHookProcessingActive() && !NewRelicSecurity.getAgent().getSecurityMetaData().getRequest().isEmpty()) { NewRelicSecurity.getAgent().getSecurityMetaData().addCustomAttribute(R2DBCVendor.META_CONST_R2DBC_VENDOR, R2DBCVendor.POSTGRES); } + NewRelicSecurity.getAgent().recordExternalConnection(null, -1, socketAddress.toString(), null, ExternalConnectionType.DATABASE_CONNECTION.name(), "R2DBC-POSTGRESQL"); return Weaver.callOriginal(); } }