-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate context to lettuce callbacks (#3839)
- Loading branch information
Showing
16 changed files
with
580 additions
and
92 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...ntelemetry/javaagent/instrumentation/lettuce/v4_0/LettuceAsyncCommandInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.lettuce.v4_0; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import com.lambdaworks.redis.protocol.AsyncCommand; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; | ||
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class LettuceAsyncCommandInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("com.lambdaworks.redis.protocol.AsyncCommand"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor(), LettuceAsyncCommandInstrumentation.class.getName() + "$SaveContextAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("complete").or(named("completeExceptionally")).or(named("cancel")), | ||
LettuceAsyncCommandInstrumentation.class.getName() + "$RestoreContextAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class SaveContextAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void saveContext(@Advice.This AsyncCommand<?, ?, ?> asyncCommand) { | ||
Context context = Java8BytecodeBridge.currentContext(); | ||
// get the context that submitted this command and attach it, it will be used to run callbacks | ||
context = context.get(LettuceSingletons.COMMAND_CONTEXT_KEY); | ||
InstrumentationContext.get(AsyncCommand.class, Context.class).put(asyncCommand, context); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RestoreContextAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.This AsyncCommand<?, ?, ?> asyncCommand, @Advice.Local("otelScope") Scope scope) { | ||
Context context = | ||
InstrumentationContext.get(AsyncCommand.class, Context.class).get(asyncCommand); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Local("otelScope") Scope scope) { | ||
scope.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.