Skip to content

Commit c27a0a7

Browse files
committed
trace injection for prepared statements
1 parent d3151b6 commit c27a0a7

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public static AgentScope onEnter(@Advice.This final Statement statement) {
8888
span.setTag(DBM_TRACE_INJECTED, true);
8989
} else {
9090
span = startSpan(DATABASE_QUERY);
91+
// TODO: call setApplicationName only if postgres && inject comment && inject trace
92+
// context && trace_prepared_statements
93+
// if (DECORATE.isPostgres(dbInfo)) {
94+
DECORATE.setApplicationName(span, connection);
95+
// }
9196
}
9297
DECORATE.afterStart(span);
9398
DECORATE.onConnection(span, dbInfo);

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.InstrumentationTags.DBM_TRACE_INJECTED;
45
import static datadog.trace.bootstrap.instrumentation.api.Tags.*;
56

67
import datadog.trace.api.Config;
@@ -248,6 +249,10 @@ public String traceParent(AgentSpan span, int samplingPriority) {
248249
return sb.toString();
249250
}
250251

252+
public boolean isPostgres(final DBInfo dbInfo) {
253+
return "postgres".equals(dbInfo.getType());
254+
}
255+
251256
public boolean isSqlServer(final DBInfo dbInfo) {
252257
return "sqlserver".equals(dbInfo.getType());
253258
}
@@ -312,6 +317,40 @@ public long setContextInfo(Connection connection, DBInfo dbInfo) {
312317
return spanID;
313318
}
314319

320+
// TODO: add description
321+
public void setApplicationName(AgentSpan span, Connection connection) {
322+
// TODO: measure time spent in instrumentation and add it as a tag in span
323+
try {
324+
Integer priority = span.forceSamplingDecision();
325+
if (priority == null) {
326+
return;
327+
}
328+
final String traceParent = DECORATE.traceParent(span, priority);
329+
final String traceContext = "_DD_" + traceParent;
330+
331+
// SET doesn't work with parameters
332+
StringBuilder sql = new StringBuilder();
333+
sql.append("SET application_name = '");
334+
sql.append(traceContext);
335+
sql.append("';");
336+
337+
try (Statement statement = connection.createStatement()) {
338+
statement.execute(sql.toString());
339+
} catch (SQLException e) {
340+
throw e;
341+
}
342+
} catch (Exception e) {
343+
log.debug(
344+
"Failed to set extra DBM data in application_name for trace {}. "
345+
+ "To disable this behavior, set trace_prepared_statements to 'false'. "
346+
+ "See https://docs.datadoghq.com/database_monitoring/connect_dbm_and_apm/ for more info.{}",
347+
span.getTraceId().toHexString(),
348+
e);
349+
DECORATE.onError(span, e);
350+
}
351+
span.setTag(DBM_TRACE_INJECTED, true);
352+
}
353+
315354
@Override
316355
protected void postProcessServiceAndOperationName(
317356
AgentSpan span, DatabaseClientDecorator.NamingEntry namingEntry) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public static AgentScope onEnter(
103103
if (span != null && INJECT_COMMENT) {
104104
String traceParent = null;
105105

106+
// TODO: don't propagate trace info in the comments for postgres if full+ mode is defined
106107
if (injectTraceContext) {
107108
Integer priority = span.forceSamplingDecision();
108109
if (priority != null) {

0 commit comments

Comments
 (0)