Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add peer service tag in dbm sql commenter #7913

Merged
merged 10 commits into from
Jan 30, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void testToComment() {
dbService,
hostname,
dbName,
null,
env,
version,
traceParent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package datadog.trace.instrumentation.jdbc;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;

import datadog.trace.api.Config;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -15,6 +19,7 @@ public class SQLCommenter {
private static final String DATABASE_SERVICE = encode("dddbs");
private static final String DD_HOSTNAME = encode("ddh");
private static final String DD_DB_NAME = encode("dddb");
private static final String DD_PEER_SERVICE = "ddprs";
private static final String DD_ENV = encode("dde");
private static final String DD_VERSION = encode("ddpv");
private static final String TRACEPARENT = encode("traceparent");
Expand Down Expand Up @@ -94,13 +99,21 @@ public static String inject(
}
}

AgentSpan currSpan = activeSpan();
Object peerServiceObj = null;
if (currSpan != null) {
peerServiceObj = currSpan.getTag(Tags.PEER_SERVICE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jordan-wong is the goal to add the peer.service tag that the user manually set (this is rare) or the one that we are supposed to calculate in the tracer?

}

final Config config = Config.get();
final String parentService = config.getServiceName();
final String env = config.getEnv();
final String version = config.getVersion();
final int commentSize = capacity(traceParent, parentService, dbService, env, version);
StringBuilder sb = new StringBuilder(sql.length() + commentSize);
boolean commentAdded = false;
String peerService = peerServiceObj != null ? peerServiceObj.toString() : null;

if (appendComment) {
sb.append(sql);
sb.append(SPACE);
Expand All @@ -113,6 +126,7 @@ public static String inject(
dbService,
hostname,
dbName,
peerService,
env,
version,
traceParent);
Expand All @@ -127,9 +141,11 @@ public static String inject(
dbService,
hostname,
dbName,
peerService,
env,
version,
traceParent);

sb.append(CLOSE_COMMENT);
sb.append(SPACE);
sb.append(sql);
Expand Down Expand Up @@ -199,14 +215,19 @@ protected static boolean toComment(
final String dbService,
final String hostname,
final String dbName,
final String peerService,
final String env,
final String version,
final String traceparent) {
int emptySize = sb.length();

append(sb, PARENT_SERVICE, parentService, false);
append(sb, DATABASE_SERVICE, dbService, sb.length() > emptySize);
append(sb, DD_HOSTNAME, hostname, sb.length() > emptySize);
append(sb, DD_DB_NAME, dbName, sb.length() > emptySize);
if (peerService != null) {
append(sb, DD_PEER_SERVICE, peerService, sb.length() > emptySize);
}
append(sb, DD_ENV, env, sb.length() > emptySize);
append(sb, DD_VERSION, version, sb.length() > emptySize);
if (injectTrace) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.Tags

import datadog.trace.instrumentation.jdbc.SQLCommenter

import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace

class SQLCommenterTest extends AgentTestRunner {

def "test find first word"() {
Expand Down Expand Up @@ -35,15 +41,13 @@ class SQLCommenterTest extends AgentTestRunner {
String sqlWithComment = ""
if (injectTrace) {
sqlWithComment = SQLCommenter.inject(query, dbService, dbType, host, dbName, traceParent, true, appendComment)
} else {
if (appendComment) {
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
} else {
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
}
} else if (appendComment) {
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
}
else {
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
}

sqlWithComment == expected

then:
sqlWithComment == expected
Expand Down Expand Up @@ -105,4 +109,38 @@ class SQLCommenterTest extends AgentTestRunner {
"/*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"
"/*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"
}

def "test encode Sql Comment with peer service"() {
setup:
injectSysConfig("dd.service", ddService)
injectSysConfig("dd.env", ddEnv)
injectSysConfig("dd.version", ddVersion)

when:
String sqlWithComment = ""
runUnderTrace("testTrace"){
AgentSpan currSpan = AgentTracer.activeSpan()
currSpan.setTag(Tags.PEER_SERVICE, peerService)

if (injectTrace) {
sqlWithComment = SQLCommenter.inject(query, dbService, dbType, host, dbName, traceParent, true, appendComment)
}
else if (appendComment) {
sqlWithComment = SQLCommenter.append(query, dbService, dbType, host, dbName)
}
else {
sqlWithComment = SQLCommenter.prepend(query, dbService, dbType, host, dbName)
}
}

then:
sqlWithComment == expected

where:
query | ddService | ddEnv | dbService | dbType | host | dbName | ddVersion | injectTrace | appendComment | traceParent | peerService | expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can probably update this to only have the cases that include ddprs since that is what we want to test here. the other cases should already be tested in other tests so no need to repeat it.

"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'*/"
"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'*/"
"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'*/"
"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'*/"
}
}
Loading