Skip to content

Commit 6ec8455

Browse files
authored
Add peer service tag in dbm sql commenter (#7913)
* add peer.service to the SQLCommenter when explicity specified, add tests for it * Remove comments, cleanup * add working version with unit tests passing * remove comments and dev print statements * remove accidentally added extra param * add null check for ActiveSpan * refactor toComment behavior * fix small bug * clean up PR, rename variables and refactor test logic for readability
1 parent a6b67c3 commit 6ec8455

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

dd-java-agent/instrumentation/jdbc/src/jmh/java/datadog/trace/instrumentation/jdbc/SQLCommenterBenchmark.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void testToComment() {
3333
dbService,
3434
hostname,
3535
dbName,
36+
null,
3637
env,
3738
version,
3839
traceParent);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package datadog.trace.instrumentation.jdbc;
22

3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
4+
35
import datadog.trace.api.Config;
6+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
7+
import datadog.trace.bootstrap.instrumentation.api.Tags;
48
import java.io.UnsupportedEncodingException;
59
import java.net.URLEncoder;
610
import java.nio.charset.StandardCharsets;
@@ -15,6 +19,7 @@ public class SQLCommenter {
1519
private static final String DATABASE_SERVICE = encode("dddbs");
1620
private static final String DD_HOSTNAME = encode("ddh");
1721
private static final String DD_DB_NAME = encode("dddb");
22+
private static final String DD_PEER_SERVICE = "ddprs";
1823
private static final String DD_ENV = encode("dde");
1924
private static final String DD_VERSION = encode("ddpv");
2025
private static final String TRACEPARENT = encode("traceparent");
@@ -94,13 +99,21 @@ public static String inject(
9499
}
95100
}
96101

102+
AgentSpan currSpan = activeSpan();
103+
Object peerServiceObj = null;
104+
if (currSpan != null) {
105+
peerServiceObj = currSpan.getTag(Tags.PEER_SERVICE);
106+
}
107+
97108
final Config config = Config.get();
98109
final String parentService = config.getServiceName();
99110
final String env = config.getEnv();
100111
final String version = config.getVersion();
101112
final int commentSize = capacity(traceParent, parentService, dbService, env, version);
102113
StringBuilder sb = new StringBuilder(sql.length() + commentSize);
103114
boolean commentAdded = false;
115+
String peerService = peerServiceObj != null ? peerServiceObj.toString() : null;
116+
104117
if (appendComment) {
105118
sb.append(sql);
106119
sb.append(SPACE);
@@ -113,6 +126,7 @@ public static String inject(
113126
dbService,
114127
hostname,
115128
dbName,
129+
peerService,
116130
env,
117131
version,
118132
traceParent);
@@ -127,9 +141,11 @@ public static String inject(
127141
dbService,
128142
hostname,
129143
dbName,
144+
peerService,
130145
env,
131146
version,
132147
traceParent);
148+
133149
sb.append(CLOSE_COMMENT);
134150
sb.append(SPACE);
135151
sb.append(sql);
@@ -199,14 +215,19 @@ protected static boolean toComment(
199215
final String dbService,
200216
final String hostname,
201217
final String dbName,
218+
final String peerService,
202219
final String env,
203220
final String version,
204221
final String traceparent) {
205222
int emptySize = sb.length();
223+
206224
append(sb, PARENT_SERVICE, parentService, false);
207225
append(sb, DATABASE_SERVICE, dbService, sb.length() > emptySize);
208226
append(sb, DD_HOSTNAME, hostname, sb.length() > emptySize);
209227
append(sb, DD_DB_NAME, dbName, sb.length() > emptySize);
228+
if (peerService != null) {
229+
append(sb, DD_PEER_SERVICE, peerService, sb.length() > emptySize);
230+
}
210231
append(sb, DD_ENV, env, sb.length() > emptySize);
211232
append(sb, DD_VERSION, version, sb.length() > emptySize);
212233
if (injectTrace) {

dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import datadog.trace.agent.test.AgentTestRunner
2+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
3+
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
4+
import datadog.trace.bootstrap.instrumentation.api.Tags
5+
26
import datadog.trace.instrumentation.jdbc.SQLCommenter
37

8+
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
9+
410
class SQLCommenterTest extends AgentTestRunner {
511

612
def "test find first word"() {
@@ -35,15 +41,13 @@ class SQLCommenterTest extends AgentTestRunner {
3541
String sqlWithComment = ""
3642
if (injectTrace) {
3743
sqlWithComment = SQLCommenter.inject(query, dbService, dbType, host, dbName, traceParent, true, appendComment)
38-
} else {
39-
if (appendComment) {
40-
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
41-
} else {
42-
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
43-
}
44+
} else if (appendComment) {
45+
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
46+
}
47+
else {
48+
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
4449
}
4550

46-
sqlWithComment == expected
4751

4852
then:
4953
sqlWithComment == expected
@@ -105,4 +109,38 @@ class SQLCommenterTest extends AgentTestRunner {
105109
"/*customer-comment*/ SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/ /*customer-comment*/ SELECT * FROM foo"
106110
"/*traceparent" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | false | null | "/*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion'*/ /*traceparent"
107111
}
112+
113+
def "test encode Sql Comment with peer service"() {
114+
setup:
115+
injectSysConfig("dd.service", ddService)
116+
injectSysConfig("dd.env", ddEnv)
117+
injectSysConfig("dd.version", ddVersion)
118+
119+
when:
120+
String sqlWithComment = ""
121+
runUnderTrace("testTrace"){
122+
AgentSpan currSpan = AgentTracer.activeSpan()
123+
currSpan.setTag(Tags.PEER_SERVICE, peerService)
124+
125+
if (injectTrace) {
126+
sqlWithComment = SQLCommenter.inject(query, dbService, dbType, host, dbName, traceParent, true, appendComment)
127+
}
128+
else if (appendComment) {
129+
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
130+
}
131+
else {
132+
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
133+
}
134+
}
135+
136+
then:
137+
sqlWithComment == expected
138+
139+
where:
140+
query | ddService | ddEnv | dbService | dbType | host | dbName | ddVersion | injectTrace | appendComment | traceParent | peerService | expected
141+
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
142+
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "postgres" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
143+
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "testPeer" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',ddprs='testPeer',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
144+
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "postgres" | "h" | "n" | "TestVersion" | true | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "testPeer" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',ddprs='testPeer',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
145+
}
108146
}

0 commit comments

Comments
 (0)