Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static AgentScope beforeCommand(final RedisCommand<?, ?, ?> command) {
final AgentSpan span = startSpan("redis.query");
DECORATE.afterStart(span);
DECORATE.onCommand(span, command);
return activateSpan(span, finishSpanEarly(command));
return activateSpan(span);
}

public static void afterCommand(
Expand All @@ -49,7 +49,7 @@ public static void afterCommand(
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
} else if (!finishSpanEarly(command)) {
} else if (expectsResponse(command)) {
asyncCommand.handleAsync(
(value, ex) -> {
if (ex instanceof CancellationException) {
Expand All @@ -61,6 +61,10 @@ public static void afterCommand(
span.finish();
return null;
});
} else {
// No response is expected, so we must finish the span now.
DECORATE.beforeFinish(span);
span.finish();
}
scope.close();
// span may be finished by handleAsync call above.
Expand Down Expand Up @@ -89,11 +93,11 @@ public static void afterConnect(final AgentScope scope, final Throwable throwabl
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.lettuce.LettuceClientDecorator.DECORATE;
import static datadog.trace.instrumentation.lettuce.LettuceInstrumentationUtil.doFinishSpanEarly;
import static datadog.trace.instrumentation.lettuce.LettuceInstrumentationUtil.expectsResponse;

import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand All @@ -20,7 +20,7 @@ public static AgentScope onEnter(@Advice.Argument(0) final RedisCommand command)
DECORATE.afterStart(span);
DECORATE.onCommand(span, command);

return activateSpan(span, doFinishSpanEarly(command));
return activateSpan(span);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand All @@ -40,10 +40,13 @@ public static void stopSpan(
}

// close spans on error or normal completion
if (!doFinishSpanEarly(command)) {
if (expectsResponse(command)) {
asyncCommand.handleAsync(new LettuceAsyncBiFunction<>(span));
} else {
DECORATE.beforeFinish(span);
span.finish();
}
scope.close();
// span finished by LettuceAsyncBiFunction
// span may be finished by LettuceAsyncBiFunction
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,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);
}

// Workaround to keep trace agent from crashing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.lettuce.rx;

import static datadog.trace.instrumentation.lettuce.LettuceInstrumentationUtil.doFinishSpanEarly;
import static datadog.trace.instrumentation.lettuce.LettuceInstrumentationUtil.expectsResponse;

import io.lettuce.core.protocol.RedisCommand;
import java.util.function.Supplier;
Expand All @@ -21,7 +21,7 @@ public static void monitorSpan(
@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());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.trace.instrumentation.lettuce.rx;

import datadog.trace.instrumentation.lettuce.LettuceInstrumentationUtil;
import static datadog.trace.instrumentation.lettuce.LettuceInstrumentationUtil.expectsResponse;

import io.lettuce.core.protocol.RedisCommand;
import java.util.function.Supplier;
import net.bytebuddy.asm.Advice;
Expand All @@ -20,7 +21,7 @@ public static RedisCommand extractCommandName(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void accept(final R r) {
DECORATE.afterStart(span);
DECORATE.onCommand(span, command);
if (finishSpanOnClose) {
DECORATE.beforeFinish(span);
span.finish();
}
}
Expand Down