Skip to content

Commit

Permalink
trace injection for prepared statements
Browse files Browse the repository at this point in the history
  • Loading branch information
nenadnoveljic committed Nov 12, 2024
1 parent d3151b6 commit c27a0a7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public static AgentScope onEnter(@Advice.This final Statement statement) {
span.setTag(DBM_TRACE_INJECTED, true);
} else {
span = startSpan(DATABASE_QUERY);
// TODO: call setApplicationName only if postgres && inject comment && inject trace
// context && trace_prepared_statements
// if (DECORATE.isPostgres(dbInfo)) {
DECORATE.setApplicationName(span, connection);
// }
}
DECORATE.afterStart(span);
DECORATE.onConnection(span, dbInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.trace.instrumentation.jdbc;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.InstrumentationTags.DBM_TRACE_INJECTED;
import static datadog.trace.bootstrap.instrumentation.api.Tags.*;

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

public boolean isPostgres(final DBInfo dbInfo) {
return "postgres".equals(dbInfo.getType());
}

public boolean isSqlServer(final DBInfo dbInfo) {
return "sqlserver".equals(dbInfo.getType());
}
Expand Down Expand Up @@ -312,6 +317,40 @@ public long setContextInfo(Connection connection, DBInfo dbInfo) {
return spanID;
}

// TODO: add description
public void setApplicationName(AgentSpan span, Connection connection) {
// TODO: measure time spent in instrumentation and add it as a tag in span
try {
Integer priority = span.forceSamplingDecision();
if (priority == null) {
return;
}
final String traceParent = DECORATE.traceParent(span, priority);
final String traceContext = "_DD_" + traceParent;

// SET doesn't work with parameters
StringBuilder sql = new StringBuilder();
sql.append("SET application_name = '");
sql.append(traceContext);
sql.append("';");

try (Statement statement = connection.createStatement()) {
statement.execute(sql.toString());
} catch (SQLException e) {
throw e;
}
} catch (Exception e) {
log.debug(
"Failed to set extra DBM data in application_name for trace {}. "
+ "To disable this behavior, set trace_prepared_statements to 'false'. "
+ "See https://docs.datadoghq.com/database_monitoring/connect_dbm_and_apm/ for more info.{}",
span.getTraceId().toHexString(),
e);
DECORATE.onError(span, e);
}
span.setTag(DBM_TRACE_INJECTED, true);
}

@Override
protected void postProcessServiceAndOperationName(
AgentSpan span, DatabaseClientDecorator.NamingEntry namingEntry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public static AgentScope onEnter(
if (span != null && INJECT_COMMENT) {
String traceParent = null;

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

0 comments on commit c27a0a7

Please sign in to comment.