Skip to content
Draft
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 @@ -37,12 +37,7 @@
* <p>When it is a set of commands that is being executed we want to have the information about the
* number of processed commands - this can be achieved by hooking into {@linkplain
* io.lettuce.core.RedisPublisher.RedisSubscription#onNext(Object)} method, incrementing the tracked
* count until the associated subscription command has been cancelled. In order to propagate the
* parent span active at the time of issuing the Redis command it is necessary to instrument
* {@linkplain io.lettuce.core.AbstractRedisReactiveCommands}, intercepting all <code>createMono
* </code> and <code>create*Flux</code> methods - capturing the parent span when {@linkplain
* reactor.core.publisher.Mono} or {@linkplain reactor.core.publisher.Flux} instance is created and
* reactivating each time they are subscribed.
* count until the associated subscription command has been cancelled.
*/
@AutoService(InstrumenterModule.class)
public class LettuceReactiveClientInstrumentation extends InstrumenterModule.Tracing
Expand All @@ -56,16 +51,13 @@ public LettuceReactiveClientInstrumentation() {
public String[] knownMatchingTypes() {
return new String[] {
"io.lettuce.core.RedisPublisher$RedisSubscription",
"io.lettuce.core.RedisPublisher$SubscriptionCommand",
"io.lettuce.core.AbstractRedisReactiveCommands"
"io.lettuce.core.RedisPublisher$SubscriptionCommand"
};
}

@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".rx.RedisSubscriptionSubscribeAdvice",
packageName + ".rx.RedisSubscriptionSubscribeAdvice$State",
packageName + ".rx.RedisSubscriptionState",
packageName + ".LettuceInstrumentationUtil",
packageName + ".LettuceClientDecorator"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package datadog.trace.instrumentation.lettuce5.rx;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import io.lettuce.core.api.StatefulConnection;

public final class RedisSubscriptionState {
public boolean cancelled = false;
public int count = 0;
public AgentSpan parentSpan;
public StatefulConnection<?, ?> connection;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package datadog.trace.instrumentation.lettuce5.rx;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.lettuce5.LettuceClientDecorator.DECORATE;
import static datadog.trace.instrumentation.lettuce5.LettuceInstrumentationUtil.expectsResponse;

import datadog.context.ContextScope;
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.instrumentation.lettuce5.LettuceClientDecorator;
Expand All @@ -16,33 +14,18 @@
import org.reactivestreams.Subscription;

public class RedisSubscriptionSubscribeAdvice {
public static final class State {
public final ContextScope parentScope;
public final AgentSpan span;

public State(ContextScope parentScope, AgentSpan span) {
this.parentScope = parentScope;
this.span = span;
}
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static State beforeSubscribe(
public static AgentSpan beforeSubscribe(
@Advice.This Subscription subscription,
@Advice.FieldValue("command") RedisCommand command,
@Advice.FieldValue("subscriptionCommand") RedisCommand subscriptionCommand) {

ContextScope parentScope = null;
RedisSubscriptionState state =
(RedisSubscriptionState)
InstrumentationContext.get(
"io.lettuce.core.RedisPublisher$RedisSubscription",
"datadog.trace.instrumentation.lettuce5.rx.RedisSubscriptionState")
.get(subscription);
AgentSpan parentSpan = state != null ? state.parentSpan : null;
if (parentSpan != null) {
parentScope = activateSpan(parentSpan);
}
AgentSpan span =
startSpan(
LettuceClientDecorator.REDIS_CLIENT.toString(), LettuceClientDecorator.OPERATION_NAME);
Expand All @@ -56,24 +39,21 @@ public static State beforeSubscribe(
}
DECORATE.onCommand(span, command);

return new State(parentScope, span);
return span;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void afterSubscribe(
@Advice.FieldValue("command") RedisCommand command,
@Advice.FieldValue("subscriptionCommand") RedisCommand subscriptionCommand,
@Advice.Enter State state,
@Advice.Enter AgentSpan span,
@Advice.Thrown Throwable throwable) {
if (throwable != null || !expectsResponse(command)) {
DECORATE.onError(state.span, throwable);
DECORATE.beforeFinish(state.span);
state.span.finish();
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
InstrumentationContext.get(RedisCommand.class, AgentSpan.class)
.put(subscriptionCommand, null);
}
if (state.parentScope != null) {
state.parentScope.close();
}
}
}
Loading