Skip to content

Commit

Permalink
OpenTelemetry JDBC instrumentation - register Oracle driver manually
Browse files Browse the repository at this point in the history
fixes: #28915
  • Loading branch information
michalvavrik committed Nov 3, 2022
1 parent da74776 commit 81bd209
Show file tree
Hide file tree
Showing 25 changed files with 892 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@ public interface Capability {
String KAFKA = QUARKUS_PREFIX + "kafka";

String SMALLRYE_REACTIVE_MESSAGING = QUARKUS_PREFIX + "smallrye.reactive.messaging";
String JDBC_ORACLE = QUARKUS_PREFIX + "jdbc.oracle";
}
5 changes: 5 additions & 0 deletions extensions/jdbc/jdbc-oracle/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-maven-plugin</artifactId>
<configuration>
<capabilities>
<provides>io.quarkus.jdbc.oracle</provides>
</capabilities>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,17 @@ void createOpenTelemetry(
void storeVertxOnContextStorage(OpenTelemetryRecorder recorder, CoreVertxBuildItem vertx) {
recorder.storeVertxOnContextStorage(vertx.getVertx());
}

/**
* 'OracleDriver' register itself as driver in static initialization block, however we don't want to
* force runtime initialization for compatibility reasons, for more information please check:
* io.quarkus.jdbc.oracle.deployment.OracleMetadataOverrides#runtimeInitializeDriver
*/
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void registerOracleDriver(Capabilities capabilities, OpenTelemetryRecorder recorder) {
if (capabilities.isPresent(Capability.JDBC_ORACLE)) {
recorder.registerOracleDriver();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package io.quarkus.opentelemetry.runtime;

import java.lang.reflect.InvocationTargetException;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.function.Supplier;

import org.jboss.logging.Logger;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.ContextStorage;
Expand All @@ -16,6 +22,8 @@
@Recorder
public class OpenTelemetryRecorder {

private static final Logger LOG = Logger.getLogger(OpenTelemetryRecorder.class);

/* STATIC INIT */
public void resetGlobalOpenTelemetryForDevMode() {
GlobalOpenTelemetry.resetForTest();
Expand Down Expand Up @@ -46,4 +54,25 @@ public void eagerlyCreateContextStorage() {
public void storeVertxOnContextStorage(Supplier<Vertx> vertx) {
QuarkusContextStorage.vertx = vertx.get();
}

public void registerOracleDriver() {
try {
var constructors = Class
.forName("oracle.jdbc.driver.OracleDriver", true, Thread.currentThread().getContextClassLoader())
.getConstructors();
if (constructors.length == 1) {
// register OracleDriver
DriverManager.registerDriver((Driver) constructors[0].newInstance());
} else {
// we need default constructor, ATM there is just one
LOG.warn(
"Class 'oracle.jdbc.driver.OracleDriver' has more than one constructor and won't be registered as driver."
+
" JDBC instrumentation might not work properly in native mode.");
}
} catch (SQLException | InvocationTargetException | InstantiationException | IllegalAccessException
| ClassNotFoundException e) {
LOG.warn("Failed to register Oracle driver. JDBC instrumentation might not work properly in native mode.");
}
}
}
16 changes: 16 additions & 0 deletions integration-tests/opentelemetry-jdbc-instrumentation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# OpenTelemetry JDBC instrumentation example

## Running the tests


To run the tests in a standard JVM with an Oracle database started as a Docker container, you can run the following command:

```
mvn verify -Dtest-containers
```

To also test as a native image, add `-Dnative`:

```
mvn verify -Dtest-containers -Dnative
```
Loading

0 comments on commit 81bd209

Please sign in to comment.