Skip to content

Commit a33abc7

Browse files
committed
Allow custom (non-global) OpenTelemetry instance propagation
Propagates a custom OpenTelemetry instance through the datasource, connection and statement(s) wrappers. The OpenTelemetryDriver uses an OpenTelemetry instance provided by the GlobalOpenTelemetry.get() static method.
1 parent 79a644d commit a33abc7

File tree

13 files changed

+175
-144
lines changed

13 files changed

+175
-144
lines changed

instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/datasource/DataSourceInstrumentation.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
package io.opentelemetry.javaagent.instrumentation.jdbc.datasource;
77

8-
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceSingletons.instrumenter;
8+
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceInstrumenterFactory.createInstrumenter;
99
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
1010
import static net.bytebuddy.matcher.ElementMatchers.named;
1111

12+
import io.opentelemetry.api.GlobalOpenTelemetry;
1213
import io.opentelemetry.context.Context;
1314
import io.opentelemetry.context.Scope;
15+
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1416
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
1517
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1618
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
@@ -46,7 +48,8 @@ public static void start(
4648
return;
4749
}
4850

49-
context = instrumenter().start(parentContext, ds);
51+
Instrumenter<DataSource, Void> instrumenter = createInstrumenter(GlobalOpenTelemetry.get());
52+
context = instrumenter.start(parentContext, ds);
5053
scope = context.makeCurrent();
5154
}
5255

@@ -60,7 +63,8 @@ public static void stopSpan(
6063
return;
6164
}
6265
scope.close();
63-
instrumenter().end(context, ds, null, throwable);
66+
Instrumenter<DataSource, Void> instrumenter = createInstrumenter(GlobalOpenTelemetry.get());
67+
instrumenter.end(context, ds, null, throwable);
6468
}
6569
}
6670
}

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/OpenTelemetryDriver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020

2121
package io.opentelemetry.instrumentation.jdbc;
2222

23-
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcSingletons.INSTRUMENTATION_NAME;
23+
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcInstrumenterFactory.INSTRUMENTATION_NAME;
2424

25+
import io.opentelemetry.api.GlobalOpenTelemetry;
2526
import io.opentelemetry.instrumentation.api.internal.EmbeddedInstrumentationProperties;
2627
import io.opentelemetry.instrumentation.jdbc.internal.JdbcConnectionUrlParser;
2728
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection;
@@ -211,7 +212,7 @@ public Connection connect(String url, Properties info) throws SQLException {
211212

212213
DbInfo dbInfo = JdbcConnectionUrlParser.parse(realUrl, info);
213214

214-
return new OpenTelemetryConnection(connection, dbInfo);
215+
return new OpenTelemetryConnection(connection, dbInfo, GlobalOpenTelemetry.get());
215216
}
216217

217218
@Override

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/OpenTelemetryDataSource.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
package io.opentelemetry.instrumentation.jdbc.datasource;
2222

23-
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceSingletons.instrumenter;
23+
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceInstrumenterFactory.createInstrumenter;
2424
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcUtils.computeDbInfo;
2525

26+
import io.opentelemetry.api.OpenTelemetry;
2627
import io.opentelemetry.api.trace.Span;
2728
import io.opentelemetry.context.Context;
2829
import io.opentelemetry.context.Scope;
30+
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
2931
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection;
3032
import io.opentelemetry.instrumentation.jdbc.internal.ThrowingSupplier;
3133
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo;
@@ -40,29 +42,34 @@
4042
public class OpenTelemetryDataSource implements DataSource, AutoCloseable {
4143

4244
private final DataSource delegate;
45+
private final OpenTelemetry openTelemetry;
46+
private final Instrumenter<DataSource, Void> instrumenter;
4347

4448
/**
4549
* Create a OpenTelemetry DataSource wrapping another DataSource. This constructor is primarily
4650
* used by dependency injection frameworks.
4751
*
4852
* @param delegate the DataSource to wrap
53+
* @param openTelemetry the OpenTelemetry instance to setup for
4954
*/
50-
public OpenTelemetryDataSource(DataSource delegate) {
55+
public OpenTelemetryDataSource(DataSource delegate, OpenTelemetry openTelemetry) {
5156
this.delegate = delegate;
57+
this.openTelemetry = openTelemetry;
58+
this.instrumenter = createInstrumenter(this.openTelemetry);
5259
}
5360

5461
@Override
5562
public Connection getConnection() throws SQLException {
5663
Connection connection = wrapCall(delegate::getConnection);
5764
DbInfo dbInfo = computeDbInfo(connection);
58-
return new OpenTelemetryConnection(connection, dbInfo);
65+
return new OpenTelemetryConnection(connection, dbInfo, openTelemetry);
5966
}
6067

6168
@Override
6269
public Connection getConnection(String username, String password) throws SQLException {
6370
Connection connection = wrapCall(() -> delegate.getConnection(username, password));
6471
DbInfo dbInfo = computeDbInfo(connection);
65-
return new OpenTelemetryConnection(connection, dbInfo);
72+
return new OpenTelemetryConnection(connection, dbInfo, openTelemetry);
6673
}
6774

6875
@Override
@@ -116,15 +123,15 @@ private <T, E extends SQLException> T wrapCall(ThrowingSupplier<T, E> callable)
116123
return callable.call();
117124
}
118125

119-
Context context = instrumenter().start(parentContext, delegate);
126+
Context context = this.instrumenter.start(parentContext, delegate);
120127
T result;
121128
try (Scope ignored = context.makeCurrent()) {
122129
result = callable.call();
123130
} catch (Throwable t) {
124-
instrumenter().end(context, delegate, null, t);
131+
this.instrumenter.end(context, delegate, null, t);
125132
throw t;
126133
}
127-
instrumenter().end(context, delegate, null, null);
134+
this.instrumenter.end(context, delegate, null, null);
128135
return result;
129136
}
130137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.jdbc.internal;
7+
8+
import io.opentelemetry.api.OpenTelemetry;
9+
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
10+
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor;
11+
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeSpanNameExtractor;
12+
import javax.sql.DataSource;
13+
14+
/**
15+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
16+
* any time.
17+
*/
18+
public final class DataSourceInstrumenterFactory {
19+
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.jdbc";
20+
private static final DataSourceCodeAttributesGetter codeAttributesGetter =
21+
new DataSourceCodeAttributesGetter();
22+
23+
public static Instrumenter<DataSource, Void> createInstrumenter(OpenTelemetry openTelemetry) {
24+
return Instrumenter.<DataSource, Void>builder(
25+
openTelemetry, INSTRUMENTATION_NAME, CodeSpanNameExtractor.create(codeAttributesGetter))
26+
.addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter))
27+
.buildInstrumenter();
28+
}
29+
30+
private DataSourceInstrumenterFactory() {}
31+
}

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/DataSourceSingletons.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.jdbc.internal;
7+
8+
import io.opentelemetry.api.OpenTelemetry;
9+
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
10+
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
11+
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientSpanNameExtractor;
12+
import io.opentelemetry.instrumentation.api.instrumenter.db.SqlClientAttributesExtractor;
13+
import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
14+
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
15+
16+
/**
17+
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
18+
* any time.
19+
*/
20+
public final class JdbcInstrumenterFactory {
21+
public static final String INSTRUMENTATION_NAME = "io.opentelemetry.jdbc";
22+
private static final JdbcAttributesGetter dbAttributesGetter = new JdbcAttributesGetter();
23+
private static final JdbcNetAttributesGetter netAttributesGetter = new JdbcNetAttributesGetter();
24+
25+
public static Instrumenter<DbRequest, Void> createInstrumenter() {
26+
return createInstrumenter(null);
27+
}
28+
29+
public static Instrumenter<DbRequest, Void> createInstrumenter(OpenTelemetry openTelemetry) {
30+
return Instrumenter.<DbRequest, Void>builder(
31+
openTelemetry,
32+
INSTRUMENTATION_NAME,
33+
DbClientSpanNameExtractor.create(dbAttributesGetter))
34+
.addAttributesExtractor(
35+
SqlClientAttributesExtractor.builder(dbAttributesGetter)
36+
.setStatementSanitizationEnabled(
37+
ConfigPropertiesUtil.getBoolean(
38+
"otel.instrumentation.common.db-statement-sanitizer.enabled", true))
39+
.build())
40+
.addAttributesExtractor(NetClientAttributesExtractor.create(netAttributesGetter))
41+
.buildInstrumenter(SpanKindExtractor.alwaysClient());
42+
}
43+
44+
private JdbcInstrumenterFactory() {}
45+
}

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcSingletons.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/OpenTelemetryCallableStatement.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package io.opentelemetry.instrumentation.jdbc.internal;
2222

23+
import io.opentelemetry.api.OpenTelemetry;
2324
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo;
2425
import java.io.InputStream;
2526
import java.io.Reader;
@@ -47,8 +48,9 @@
4748
public class OpenTelemetryCallableStatement<S extends CallableStatement>
4849
extends OpenTelemetryPreparedStatement<S> implements CallableStatement {
4950

50-
public OpenTelemetryCallableStatement(S delegate, DbInfo dbInfo, String query) {
51-
super(delegate, dbInfo, query);
51+
public OpenTelemetryCallableStatement(
52+
S delegate, DbInfo dbInfo, String query, OpenTelemetry openTelemetry) {
53+
super(delegate, dbInfo, query, openTelemetry);
5254
}
5355

5456
@Override

0 commit comments

Comments
 (0)