|
1 | 1 | package io.quarkus.opentelemetry.deployment;
|
2 | 2 |
|
| 3 | +import static io.quarkus.opentelemetry.runtime.OpenTelemetryRecorder.OPEN_TELEMETRY_DRIVER; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
3 | 6 | import java.util.List;
|
4 | 7 | import java.util.Optional;
|
5 | 8 | import java.util.Set;
|
6 | 9 | import java.util.stream.Collectors;
|
7 | 10 |
|
| 11 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 12 | +import org.eclipse.microprofile.config.ConfigValue; |
8 | 13 | import org.jboss.jandex.AnnotationInstance;
|
9 | 14 | import org.jboss.jandex.AnnotationTarget;
|
10 | 15 | import org.jboss.jandex.AnnotationValue;
|
|
14 | 19 | import io.opentelemetry.instrumentation.annotations.SpanAttribute;
|
15 | 20 | import io.opentelemetry.instrumentation.annotations.WithSpan;
|
16 | 21 | import io.opentelemetry.sdk.trace.SdkTracerProvider;
|
| 22 | +import io.quarkus.agroal.spi.JdbcDataSourceBuildItem; |
| 23 | +import io.quarkus.agroal.spi.JdbcDriverBuildItem; |
17 | 24 | import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
|
18 | 25 | import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
|
19 | 26 | import io.quarkus.arc.deployment.InterceptorBindingRegistrarBuildItem;
|
20 | 27 | import io.quarkus.arc.processor.AnnotationsTransformer;
|
21 | 28 | import io.quarkus.arc.processor.InterceptorBindingRegistrar;
|
| 29 | +import io.quarkus.datasource.common.runtime.DataSourceUtil; |
| 30 | +import io.quarkus.datasource.common.runtime.DatabaseKind; |
| 31 | +import io.quarkus.deployment.Capabilities; |
| 32 | +import io.quarkus.deployment.Capability; |
22 | 33 | import io.quarkus.deployment.annotations.BuildProducer;
|
23 | 34 | import io.quarkus.deployment.annotations.BuildStep;
|
24 | 35 | import io.quarkus.deployment.annotations.BuildSteps;
|
@@ -155,4 +166,57 @@ void createOpenTelemetry(
|
155 | 166 | void storeVertxOnContextStorage(OpenTelemetryRecorder recorder, CoreVertxBuildItem vertx) {
|
156 | 167 | recorder.storeVertxOnContextStorage(vertx.getVertx());
|
157 | 168 | }
|
| 169 | + |
| 170 | + @BuildStep |
| 171 | + void collectAllJdbcDataSourcesUsingOTelDriver(BuildProducer<OpenTelemetryDriverJdbcDataSourcesBuildItem> resultProducer, |
| 172 | + List<JdbcDataSourceBuildItem> jdbcDataSources) { |
| 173 | + final List<JdbcDataSourceBuildItem> result = new ArrayList<>(); |
| 174 | + for (JdbcDataSourceBuildItem dataSource : jdbcDataSources) { |
| 175 | + // if the datasource is explicitly configured to use the OTel driver... |
| 176 | + if (dataSourceUsesOTelJdbcDriver(dataSource.getName())) { |
| 177 | + result.add(dataSource); |
| 178 | + } |
| 179 | + } |
| 180 | + if (!result.isEmpty()) { |
| 181 | + resultProducer.produce(new OpenTelemetryDriverJdbcDataSourcesBuildItem(result)); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private static boolean dataSourceUsesOTelJdbcDriver(String dataSourceName) { |
| 186 | + List<String> driverPropertyKeys = DataSourceUtil.dataSourcePropertyKeys(dataSourceName, "jdbc.driver"); |
| 187 | + for (String driverPropertyKey : driverPropertyKeys) { |
| 188 | + ConfigValue explicitlyConfiguredDriverValue = ConfigProvider.getConfig().getConfigValue(driverPropertyKey); |
| 189 | + if (explicitlyConfiguredDriverValue.getValue() != null) { |
| 190 | + return explicitlyConfiguredDriverValue.getValue().equals(OPEN_TELEMETRY_DRIVER); |
| 191 | + } |
| 192 | + } |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * 'OracleDriver' register itself as driver in static initialization block, however we don't want to |
| 198 | + * force runtime initialization for compatibility reasons, for more information please check: |
| 199 | + * io.quarkus.jdbc.oracle.deployment.OracleMetadataOverrides#runtimeInitializeDriver |
| 200 | + */ |
| 201 | + @BuildStep |
| 202 | + @Record(ExecutionTime.RUNTIME_INIT) |
| 203 | + void registerOracleDriver(Optional<OpenTelemetryDriverJdbcDataSourcesBuildItem> otJdbcDataSourcesBuildItem, |
| 204 | + List<JdbcDriverBuildItem> driverBuildItems, Capabilities capabilities, OpenTelemetryRecorder recorder) { |
| 205 | + // check if there are data sources using OT driver and jdbc-oracle extension is present |
| 206 | + if (otJdbcDataSourcesBuildItem.isPresent() && capabilities.isPresent(Capability.JDBC_ORACLE)) { |
| 207 | + for (JdbcDataSourceBuildItem jdbcDataSource : otJdbcDataSourcesBuildItem.get().jdbcDataSources) { |
| 208 | + if (jdbcDataSource.getDbKind().equals(DatabaseKind.ORACLE)) { |
| 209 | + // now we know there is Oracle JDBC datasource |
| 210 | + // let's find Oracle driver |
| 211 | + for (JdbcDriverBuildItem driverBuildItem : driverBuildItems) { |
| 212 | + if (DatabaseKind.ORACLE.equals(driverBuildItem.getDbKind())) { |
| 213 | + recorder.registerJdbcDriver(driverBuildItem.getDriverClass()); |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + break; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
158 | 222 | }
|
0 commit comments