com.undefinedlabs.scope.rules.ActorScopeAgentRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scope-rule-akka Show documentation
Show all versions of scope-rule-akka Show documentation
Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly
identify, troubleshoot and fix failed builds.
This artifact contains the classes to instrument the Akka.
package com.undefinedlabs.scope.rules;
import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.named;
import akka.actor.Actor;
import com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.ScopeSpanContainer;
import com.undefinedlabs.scope.propagation.wrappers.TracingWrapper;
import com.undefinedlabs.scope.utils.tag.TagKeys;
import com.undefinedlabs.scope.utils.tag.TagValues;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMapAdapter;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
public class ActorScopeAgentRule extends AbstractScopeAgentRule {
@Override
protected ElementMatcher super TypeDescription> typeMatcher() {
return hasSuperType(named("akka.actor.Actor"));
}
@Override
protected ElementMatcher classLoaderMatcher() {
return hasClassesNamed("akka.actor.ActorRef");
}
@Override
protected Map extends ElementMatcher super MethodDescription>, String> transformers() {
final Map, String> transformers = new HashMap<>();
transformers.put(
named("aroundReceive"), ActorScopeAgentRule.class.getName() + "$AroundReceiveAdvice");
return transformers;
}
public static class AroundReceiveAdvice {
@Advice.OnMethodEnter
public static ScopeSpanContainer enter(
@Advice.This final Actor thiz, @Advice.Argument(value = 1, readOnly = false) Object msg) {
if (!(msg instanceof TracingWrapper)) {
return null;
}
final TracingWrapper wrapper = (TracingWrapper) msg;
final Tracer tracer = ScopeGlobalTracer.get();
final SpanContext previousSpanContext =
tracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(wrapper.getHeaders()));
if (previousSpanContext == null) {
msg = wrapper.getPayload();
return null;
}
final Object payload = wrapper.getPayload();
final Tracer.SpanBuilder spanBuilder =
tracer.buildSpan("Akka RECEIVED").asChildOf(previousSpanContext);
final Span span =
spanBuilder
.withTag(TagKeys.SPAN_KIND, TagValues.Message.SPAN_KIND_CONSUMER)
.withTag(TagKeys.COMPONENT, TagValues.Component.AKKA)
.withTag(TagKeys.Network.Akka.Actor.PATH, thiz.self().path().toStringWithoutAddress())
.withTag(TagKeys.Network.Akka.Actor.TYPE, thiz.getClass().getName())
.withTag(TagKeys.Network.Akka.Actor.RECV_SENDER, thiz.sender().path().toString())
.withTag(TagKeys.Network.Akka.Actor.ADDRESS, thiz.self().path().address().toString())
.withTag(
TagKeys.Network.Akka.Actor.MESSAGE_TYPE,
(payload != null) ? payload.getClass().getName() : null)
.withTag(TagKeys.LANGUAGE, TagValues.Language.SCALA)
.start();
final ScopeSpanContainer scopeSpanContainer =
new ScopeSpanContainer(span, tracer.activateSpan(span));
msg = payload;
return scopeSpanContainer;
}
@Advice.OnMethodExit
public static void exit(
@Advice.This final Actor thiz,
@Advice.Argument(1) final Object msg,
@Advice.Enter final Object scopeSpanContainerObj) {
if (scopeSpanContainerObj == null) {
return;
}
final ScopeSpanContainer scopeSpanContainer = (ScopeSpanContainer) scopeSpanContainerObj;
final Span span = scopeSpanContainer.getSpan();
final Scope scope = scopeSpanContainer.getScope();
span.setTag(TagKeys.ERROR, false);
span.finish();
scope.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy