|
10 | 10 | import static datadog.trace.instrumentation.jdbc.JDBCDecorator.DECORATE;
|
11 | 11 | import static datadog.trace.instrumentation.jdbc.JDBCDecorator.INJECT_COMMENT;
|
12 | 12 | import static java.util.Collections.singletonMap;
|
| 13 | +import static net.bytebuddy.implementation.bytecode.assign.Assigner.Typing.DYNAMIC; |
13 | 14 | import static net.bytebuddy.matcher.ElementMatchers.isPublic;
|
14 | 15 | import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
|
15 | 16 |
|
@@ -75,6 +76,7 @@ public static class StatementAdvice {
|
75 | 76 | @Advice.OnMethodEnter(suppress = Throwable.class)
|
76 | 77 | public static AgentScope onEnter(
|
77 | 78 | @Advice.Argument(value = 0, readOnly = false) String sql,
|
| 79 | + @Advice.AllArguments(typing = DYNAMIC) Object[] args, |
78 | 80 | @Advice.This final Statement statement) {
|
79 | 81 | // TODO consider matching known non-wrapper implementations to avoid this check
|
80 | 82 | final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Statement.class);
|
@@ -127,6 +129,21 @@ public static AgentScope onEnter(
|
127 | 129 | // context_info and v$session.action respectively.
|
128 | 130 | // we should not also inject it into SQL comments to avoid duplication
|
129 | 131 | final boolean injectTraceInComment = injectTraceContext && !isSqlServer && !isOracle;
|
| 132 | + |
| 133 | + boolean appendComment = StatementInstrumentation.appendComment; |
| 134 | + |
| 135 | + // There is a bug in the SQL Server JDBC driver that prevents |
| 136 | + // the generated keys from being returned when the |
| 137 | + // SQL comment is prepended to the SQL query. |
| 138 | + // We only append in this case to avoid the comment from being truncated. |
| 139 | + // @see https://github.com/microsoft/mssql-jdbc/issues/2729 |
| 140 | + if (isSqlServer |
| 141 | + && args.length == 2 |
| 142 | + && args[1] instanceof Integer |
| 143 | + && (Integer) args[1] == Statement.RETURN_GENERATED_KEYS) { |
| 144 | + appendComment = true; |
| 145 | + } |
| 146 | + |
130 | 147 | sql =
|
131 | 148 | SQLCommenter.inject(
|
132 | 149 | sql,
|
|
0 commit comments