Skip to content

Commit 8b1ebd8

Browse files
committed
Full APM/DBM mode for Oracle
1 parent baedf8d commit 8b1ebd8

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ public static class Builder {
9191

9292
public Builder type(String type) {
9393
this.type = type;
94-
// Those DBs use the full text of the query including the comments as a cache key,
95-
// so we disable full propagation support for them to avoid destroying the cache.
96-
if (type.equals("oracle")) this.fullPropagationSupport = false;
9794
return this;
9895
}
9996

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public static AgentScope onEnter(@Advice.This final Statement statement) {
9191
} else if (DECORATE.isPostgres(dbInfo) && DBM_TRACE_PREPARED_STATEMENTS) {
9292
span = startSpan(DATABASE_QUERY);
9393
DECORATE.setApplicationName(span, connection);
94+
} else if (DECORATE.isOracle(dbInfo)) {
95+
span = startSpan(DATABASE_QUERY);
96+
DECORATE.setAction(span, connection);
9497
} else {
9598
span = startSpan(DATABASE_QUERY);
9699
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ public String traceParent(AgentSpan span, int samplingPriority) {
252252
return sb.toString();
253253
}
254254

255+
public boolean isOracle(final DBInfo dbInfo) {
256+
return "oracle".equals(dbInfo.getType());
257+
}
258+
255259
public boolean isPostgres(final DBInfo dbInfo) {
256260
return dbInfo.getType().startsWith("postgres");
257261
}
@@ -260,6 +264,38 @@ public boolean isSqlServer(final DBInfo dbInfo) {
260264
return "sqlserver".equals(dbInfo.getType());
261265
}
262266

267+
/**
268+
* Executes `connection.setClientInfo("OCSID.ACTION", traceContext)` statement on the Oracle DB to
269+
* set the trace parent in `v$session.action`. This is used because it isn't possible to propagate
270+
* trace parent with the comment.
271+
*
272+
* @param span The span of the instrumented statement
273+
* @param connection The same connection as the one that will be used for the actual statement
274+
*/
275+
public void setAction(AgentSpan span, Connection connection) {
276+
try {
277+
278+
Integer priority = span.forceSamplingDecision();
279+
if (priority == null) {
280+
return;
281+
}
282+
final String traceContext = "_DD_" + DECORATE.traceParent(span, priority);
283+
284+
connection.setClientInfo("OCSID.ACTION", traceContext);
285+
286+
} catch (Throwable e) {
287+
log.debug(
288+
"Failed to set extra DBM data in application_name for trace {}. "
289+
+ "To disable this behavior, set trace_prepared_statements to 'false'. "
290+
+ "See https://docs.datadoghq.com/database_monitoring/connect_dbm_and_apm/ for more info.{}",
291+
span.getTraceId().toHexString(),
292+
e);
293+
DECORATE.onError(span, e);
294+
} finally {
295+
span.setTag("_dd.dbm_trace_injected", true);
296+
}
297+
}
298+
263299
/**
264300
* Executes a `SET CONTEXT_INFO` statement on the DB with the active trace ID and the given span
265301
* ID. This context will be "attached" to future queries on the same connection. See <a

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,18 @@ public static AgentScope onEnter(
8888
final AgentSpan span;
8989
final boolean isSqlServer = DECORATE.isSqlServer(dbInfo);
9090

91-
if (isSqlServer && INJECT_COMMENT && injectTraceContext) {
92-
// The span ID is pre-determined so that we can reference it when setting the context
93-
final long spanID = DECORATE.setContextInfo(connection, dbInfo);
94-
// we then force that pre-determined span ID for the span covering the actual query
95-
span = AgentTracer.get().buildSpan(DATABASE_QUERY).withSpanId(spanID).start();
91+
if (INJECT_COMMENT && injectTraceContext) {
92+
if (isSqlServer) {
93+
// The span ID is pre-determined so that we can reference it when setting the context
94+
final long spanID = DECORATE.setContextInfo(connection, dbInfo);
95+
// we then force that pre-determined span ID for the span covering the actual query
96+
span = AgentTracer.get().buildSpan(DATABASE_QUERY).withSpanId(spanID).start();
97+
} else if (DECORATE.isOracle(dbInfo)) {
98+
span = startSpan(DATABASE_QUERY);
99+
DECORATE.setAction(span, connection);
100+
} else {
101+
span = startSpan(DATABASE_QUERY);
102+
}
96103
} else {
97104
span = startSpan(DATABASE_QUERY);
98105
}

0 commit comments

Comments
 (0)