Skip to content

Commit 4426423

Browse files
Add Hikari Pool Name tag (#7672)
* Update deprecated MSSQLServerContainer initialization to fix setupSpec error * add HikariPoolName instrumentation and test for it * Add seperate HikariDataSource instrumentation, broken muzzle dependency add * Remove hikariDataSource instrumentation from generic DataSource instrumentation file * add muzzle dependency * revert line breaks and changes to unrelated files * Add not working rework of hikaripoolname instrumentation with DBInfo and JDBCDecorator tag setting * Add working version of intrumentation that passes statement span test * Push draft not working code - prepared statement tests dont pass * unwrap the connection before looking in the instrumentation context * Refactor tag name to remove hikari specific mention, make generic * Refactor tag name to remove hikari specific mention, make generic * clean up print statements and commented out code * fix tests in jdbc suite to check for pool name tag when testing with hikari * Add null check before 'get' * bump dependency lock * remove non-functional new module that was previously added by mistake --------- Co-authored-by: Andrea Marziali <[email protected]>
1 parent a49a0a1 commit 4426423

File tree

8 files changed

+319
-149
lines changed

8 files changed

+319
-149
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/jdbc/DBInfo.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class DBInfo {
1515
private final Integer port;
1616
private final String warehouse;
1717
private final String schema;
18+
private String poolName;
1819

1920
DBInfo(
2021
String type,
@@ -27,7 +28,8 @@ public class DBInfo {
2728
String host,
2829
Integer port,
2930
String warehouse,
30-
String schema) {
31+
String schema,
32+
String poolName) {
3133
this.type = type;
3234
this.subtype = subtype;
3335
this.fullPropagationSupport = fullPropagationSupport;
@@ -39,6 +41,7 @@ public class DBInfo {
3941
this.port = port;
4042
this.warehouse = warehouse;
4143
this.schema = schema;
44+
this.poolName = poolName;
4245
}
4346

4447
public static class Builder {
@@ -55,6 +58,7 @@ public static class Builder {
5558
private String schema;
5659
private String host;
5760
private Integer port;
61+
private String poolName;
5862

5963
Builder() {}
6064

@@ -69,7 +73,8 @@ public static class Builder {
6973
String host,
7074
Integer port,
7175
String warehouse,
72-
String schema) {
76+
String schema,
77+
String poolName) {
7378
this.type = type;
7479
this.subtype = subtype;
7580
this.fullPropagationSupport = fullPropagationSupport;
@@ -81,6 +86,7 @@ public static class Builder {
8186
this.port = port;
8287
this.warehouse = warehouse;
8388
this.schema = schema;
89+
this.poolName = poolName;
8490
}
8591

8692
public Builder type(String type) {
@@ -141,6 +147,11 @@ public Builder port(Integer port) {
141147
return this;
142148
}
143149

150+
public Builder poolName(String poolName) {
151+
this.poolName = poolName;
152+
return this;
153+
}
154+
144155
public DBInfo build() {
145156
return new DBInfo(
146157
type,
@@ -153,7 +164,8 @@ public DBInfo build() {
153164
host,
154165
port,
155166
warehouse,
156-
schema);
167+
schema,
168+
poolName);
157169
}
158170
}
159171

@@ -201,6 +213,14 @@ public String getSchema() {
201213
return schema;
202214
}
203215

216+
public String getPoolName() {
217+
return poolName;
218+
}
219+
220+
public void setPoolName(String poolname) {
221+
this.poolName = poolname;
222+
}
223+
204224
public Builder toBuilder() {
205225
return new Builder(
206226
type,
@@ -213,7 +233,8 @@ public Builder toBuilder() {
213233
host,
214234
port,
215235
warehouse,
216-
schema);
236+
schema,
237+
poolName);
217238
}
218239

219240
@Override
@@ -231,7 +252,8 @@ public boolean equals(Object o) {
231252
&& Objects.equals(host, dbInfo.host)
232253
&& Objects.equals(port, dbInfo.port)
233254
&& Objects.equals(warehouse, dbInfo.warehouse)
234-
&& Objects.equals(schema, dbInfo.schema);
255+
&& Objects.equals(schema, dbInfo.schema)
256+
&& Objects.equals(poolName, dbInfo.poolName);
235257
}
236258

237259
@Override

dd-java-agent/instrumentation/jdbc/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ muzzle {
77
pass {
88
coreJdk()
99
extraDependency 'com.ibm.db2:jcc:11.1.4.4'
10+
extraDependency 'com.zaxxer:HikariCP:2.4.0'
1011
}
1112
}
1213

@@ -19,6 +20,7 @@ addTestSuiteForDir('latestDepTest', 'test')
1920
addTestSuiteExtendingForDir('latestDepJava11Test', 'latestDepTest', 'test')
2021

2122
dependencies {
23+
compileOnly group: 'com.zaxxer', name: 'HikariCP', version: '2.4.0'
2224
testImplementation(testFixtures(project(':dd-java-agent:agent-iast')))
2325

2426
// jdbc unit testing

dd-java-agent/instrumentation/jdbc/gradle.lockfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ com.squareup.okhttp3:logging-interceptor:3.12.12=jmhRuntimeClasspath,latestDepJa
5656
com.squareup.okhttp3:okhttp:3.12.12=jmhRuntimeClasspath,latestDepJava11TestCompileClasspath,latestDepJava11TestRuntimeClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,oldH2TestCompileClasspath,oldH2TestRuntimeClasspath,oldPostgresTestCompileClasspath,oldPostgresTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
5757
com.squareup.okio:okio:1.17.5=compileClasspath,csiCompileClasspath,instrumentPluginClasspath,jmhCompileClasspath,jmhRuntimeClasspath,latestDepJava11TestCompileClasspath,latestDepJava11TestRuntimeClasspath,latestDepTestCompileClasspath,latestDepTestRuntimeClasspath,muzzleTooling,oldH2TestCompileClasspath,oldH2TestRuntimeClasspath,oldPostgresTestCompileClasspath,oldPostgresTestRuntimeClasspath,runtimeClasspath,testCompileClasspath,testFixturesRuntimeClasspath,testRuntimeClasspath
5858
com.thoughtworks.qdox:qdox:1.12.1=jmhRuntimeClasspath,latestDepJava11TestRuntimeClasspath,latestDepTestRuntimeClasspath,oldH2TestRuntimeClasspath,oldPostgresTestRuntimeClasspath,testRuntimeClasspath
59-
com.zaxxer:HikariCP:2.4.0=jmhRuntimeClasspath,oldH2TestCompileClasspath,oldH2TestRuntimeClasspath,oldPostgresTestCompileClasspath,oldPostgresTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
59+
com.zaxxer:HikariCP:2.4.0=compileClasspath,csiCompileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,oldH2TestCompileClasspath,oldH2TestRuntimeClasspath,oldPostgresTestCompileClasspath,oldPostgresTestRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
6060
com.zaxxer:HikariCP:4.0.3=latestDepTestCompileClasspath,latestDepTestRuntimeClasspath
6161
com.zaxxer:HikariCP:6.0.0=latestDepJava11TestCompileClasspath,latestDepJava11TestRuntimeClasspath
6262
commons-codec:commons-codec:1.15=spotbugs
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package datadog.trace.instrumentation.jdbc;
2+
3+
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
4+
import static java.util.Collections.singletonMap;
5+
6+
import com.google.auto.service.AutoService;
7+
import com.zaxxer.hikari.HikariDataSource;
8+
import datadog.trace.agent.tooling.Instrumenter;
9+
import datadog.trace.agent.tooling.InstrumenterModule;
10+
import datadog.trace.bootstrap.InstrumentationContext;
11+
import datadog.trace.bootstrap.instrumentation.jdbc.DBInfo;
12+
import java.sql.Connection;
13+
import java.util.Map;
14+
import net.bytebuddy.asm.Advice;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
@AutoService(InstrumenterModule.class)
19+
public final class HikariDataSourceInstrumentation extends InstrumenterModule.Tracing
20+
implements Instrumenter.ForSingleType {
21+
22+
private static final Logger log = LoggerFactory.getLogger(HikariDataSourceInstrumentation.class);
23+
24+
public HikariDataSourceInstrumentation() {
25+
super("jdbc-datasource");
26+
}
27+
28+
@Override
29+
public String instrumentedType() {
30+
return "com.zaxxer.hikari.HikariDataSource";
31+
}
32+
33+
@Override
34+
public Map<String, String> contextStore() {
35+
return singletonMap("java.sql.Connection", DBInfo.class.getName());
36+
}
37+
38+
@Override
39+
public void methodAdvice(MethodTransformer transformer) {
40+
transformer.applyAdvice(
41+
named("getConnection"),
42+
HikariDataSourceInstrumentation.class.getName() + "$HikariGetConnectionAdvice");
43+
}
44+
45+
public static class HikariGetConnectionAdvice {
46+
@Advice.OnMethodExit(suppress = Throwable.class)
47+
public static void start(
48+
@Advice.This final HikariDataSource ds, @Advice.Return Connection con) {
49+
if (con == null) {
50+
// Exception was probably thrown.
51+
return;
52+
}
53+
// connection pools wraps connection with their own type (ProxyConnection in this case)
54+
// jdbc drivers have a standard way to ask for the unwrapped instance (calling unwrap).
55+
// we need the unwrapped version in order to be able to lookup the instrumentation context
56+
// since we stored dbInfo for that instance and not for the wrapped one
57+
Connection unwrapped = con;
58+
try {
59+
if (con.isWrapperFor(Connection.class)) {
60+
unwrapped = con.unwrap(Connection.class);
61+
}
62+
} catch (Throwable t) {
63+
return;
64+
}
65+
String hikariPoolname = ds.getPoolName();
66+
if (unwrapped == null) {
67+
return;
68+
}
69+
DBInfo dbInfo = InstrumentationContext.get(Connection.class, DBInfo.class).get(unwrapped);
70+
if (dbInfo == null) {
71+
return;
72+
}
73+
dbInfo.setPoolName(hikariPoolname);
74+
}
75+
}
76+
}

dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/JDBCDecorator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package datadog.trace.instrumentation.jdbc;
22

33
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
4-
import static datadog.trace.bootstrap.instrumentation.api.Tags.DB_OPERATION;
5-
import static datadog.trace.bootstrap.instrumentation.api.Tags.DB_SCHEMA;
6-
import static datadog.trace.bootstrap.instrumentation.api.Tags.DB_WAREHOUSE;
4+
import static datadog.trace.bootstrap.instrumentation.api.Tags.*;
75

86
import datadog.trace.api.Config;
97
import datadog.trace.api.DDSpanId;
@@ -140,6 +138,7 @@ public AgentSpan onConnection(final AgentSpan span, DBInfo dbInfo) {
140138

141139
setTagIfPresent(span, DB_WAREHOUSE, dbInfo.getWarehouse());
142140
setTagIfPresent(span, DB_SCHEMA, dbInfo.getSchema());
141+
setTagIfPresent(span, DB_POOL_NAME, dbInfo.getPoolName());
143142
}
144143
return super.onConnection(span, dbInfo);
145144
}
@@ -263,6 +262,7 @@ public boolean isSqlServer(final DBInfo dbInfo) {
263262
public long setContextInfo(Connection connection, DBInfo dbInfo) {
264263
final byte VERSION = 0;
265264
final long spanID = Config.get().getIdGenerationStrategy().generateSpanId();
265+
// potentially get build span like here
266266
AgentSpan instrumentationSpan =
267267
AgentTracer.get().buildSpan("set context_info").withTag("dd.instrumentation", true).start();
268268
DECORATE.afterStart(instrumentationSpan);

0 commit comments

Comments
 (0)