Migrate lettuce instrumentation away from deprecated finishSpanOnClose (DataDog/dd-trace-java#1445)
MODIFY
instrumentation/lettuce/lettuce-4.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v4_0/InstrumentationPoints.java
instrumentation/lettuce/lettuce-4.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v4_0/InstrumentationPoints.java
@@ -54,9 +54,7 @@ public final class InstrumentationPoints {
LettuceClientDecorator.DECORATE.onError(span, throwable);
LettuceClientDecorator.DECORATE.beforeFinish(span);
span.end();
- } else if (finishSpanEarly(command)) {
- span.end();
- } else {
+ } else if (expectsResponse(command)) {
asyncCommand.handleAsync(
(value, ex) -> {
if (ex instanceof CancellationException) {
@@ -68,6 +66,10 @@ public final class InstrumentationPoints {
span.end();
return null;
});
+ } else {
+ // No response is expected, so we must finish the span now.
+ LettuceClientDecorator.DECORATE.beforeFinish(span);
+ span.end();
}
spanWithScope.closeScope();
// span may be finished by handleAsync call above.
@@ -97,11 +99,11 @@ public final class InstrumentationPoints {
* we must close the span early in order to provide info for the users
*
* @param command
- * @return true if finish the span early (the command will not have a return value)
+ * @return false if the span should finish early (the command will not have a return value)
*/
- public static boolean finishSpanEarly(final RedisCommand<?, ?, ?> command) {
+ public static boolean expectsResponse(final RedisCommand<?, ?, ?> command) {
final ProtocolKeyword keyword = command.getType();
- return isNonInstrumentingCommand(keyword) || isNonInstrumentingKeyword(keyword);
+ return !(isNonInstrumentingCommand(keyword) || isNonInstrumentingKeyword(keyword));
}
private static boolean isNonInstrumentingCommand(final ProtocolKeyword keyword) {

MODIFY
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/LettuceAsyncCommandsAdvice.java
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/LettuceAsyncCommandsAdvice.java
*/
package io.opentelemetry.auto.instrumentation.lettuce.v5_0;
+import static io.opentelemetry.auto.instrumentation.lettuce.v5_0.LettuceInstrumentationUtil.expectsResponse;
import static io.opentelemetry.trace.Span.Kind.CLIENT;
import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
@@ -56,12 +57,13 @@ public class LettuceAsyncCommandsAdvice {
}
// close spans on error or normal completion
- if (LettuceInstrumentationUtil.doFinishSpanEarly(command)) {
- span.end();
- } else {
+ if (expectsResponse(command)) {
asyncCommand.handleAsync(new LettuceAsyncBiFunction<>(span));
+ } else {
+ LettuceClientDecorator.DECORATE.beforeFinish(span);
+ span.end();
}
spanWithScope.closeScope();
- // span finished by LettuceAsyncBiFunction
+ // span may be finished by LettuceAsyncBiFunction
}
}

MODIFY
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/LettuceInstrumentationUtil.java
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/LettuceInstrumentationUtil.java
@@ -34,11 +34,11 @@ public class LettuceInstrumentationUtil {
* we must close the span early in order to provide info for the users
*
* @param command
- * @return true if finish the span early (the command will not have a return value)
+ * @return false if the span should finish early (the command will not have a return value)
*/
- public static boolean doFinishSpanEarly(final RedisCommand command) {
+ public static boolean expectsResponse(final RedisCommand command) {
final String commandName = LettuceInstrumentationUtil.getCommandName(command);
- return nonInstrumentingCommands.contains(commandName);
+ return !nonInstrumentingCommands.contains(commandName);
}
/**

MODIFY
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/rx/LettuceFluxCreationAdvice.java
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/rx/LettuceFluxCreationAdvice.java
*/
package io.opentelemetry.auto.instrumentation.lettuce.v5_0.rx;
-import static io.opentelemetry.auto.instrumentation.lettuce.v5_0.LettuceInstrumentationUtil.doFinishSpanEarly;
+import static io.opentelemetry.auto.instrumentation.lettuce.v5_0.LettuceInstrumentationUtil.expectsResponse;
import io.lettuce.core.protocol.RedisCommand;
import java.util.function.Supplier;
@@ -36,7 +36,7 @@ public class LettuceFluxCreationAdvice {
@Advice.Enter final RedisCommand command,
@Advice.Return(readOnly = false) Flux<?> publisher) {
- final boolean finishSpanOnClose = doFinishSpanEarly(command);
+ final boolean finishSpanOnClose = !expectsResponse(command);
final LettuceFluxTerminationRunnable handler =
new LettuceFluxTerminationRunnable(command, finishSpanOnClose);
publisher = publisher.doOnSubscribe(handler.getOnSubscribeConsumer());

MODIFY
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/rx/LettuceMonoCreationAdvice.java
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/rx/LettuceMonoCreationAdvice.java
*/
package io.opentelemetry.auto.instrumentation.lettuce.v5_0.rx;
+import static io.opentelemetry.auto.instrumentation.lettuce.v5_0.LettuceInstrumentationUtil.expectsResponse;
+
import io.lettuce.core.protocol.RedisCommand;
-import io.opentelemetry.auto.instrumentation.lettuce.v5_0.LettuceInstrumentationUtil;
import java.util.function.Supplier;
import net.bytebuddy.asm.Advice;
import reactor.core.publisher.Mono;
@@ -35,7 +36,7 @@ public class LettuceMonoCreationAdvice {
public static void monitorSpan(
@Advice.Enter final RedisCommand command,
@Advice.Return(readOnly = false) Mono<?> publisher) {
- final boolean finishSpanOnClose = LettuceInstrumentationUtil.doFinishSpanEarly(command);
+ final boolean finishSpanOnClose = !expectsResponse(command);
final LettuceMonoDualConsumer mdc = new LettuceMonoDualConsumer(command, finishSpanOnClose);
publisher = publisher.doOnSubscribe(mdc);
// register the call back to close the span only if necessary

MODIFY
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/rx/LettuceMonoDualConsumer.java
instrumentation/lettuce/lettuce-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/lettuce/v5_0/rx/LettuceMonoDualConsumer.java
@@ -48,6 +48,7 @@ public class LettuceMonoDualConsumer<R, T, U extends Throwable>
.startSpan();
DECORATE.afterStart(span);
if (finishSpanOnClose) {
+ DECORATE.beforeFinish(span);
span.end();
}
}

Migrate lettuce instrumentation away from deprecated finishSpanOnClose (DataDog/dd-trace-java#1445)