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 akka.actor.Actor;
import com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.ScopeSpanContainer;
import com.undefinedlabs.scope.propagation.wrappers.TracingWrapper;
import com.undefinedlabs.scope.rules.transformer.ScopeAgentAdvicedTransformer;
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 net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import java.util.Collections;
import static net.bytebuddy.matcher.ElementMatchers.*;
public class ActorScopeAgentRule extends AbstractScopeAgentRule {
@Override
protected Iterable extends AgentBuilder> transformers() {
final Class instrumentedClass = lookUp("akka.actor.Actor");
return (instrumentedClass != null) ?
Collections.singleton(newAgentBuilder()
.type(isSubTypeOf(instrumentedClass), isSystemClassLoader()).transform(new ScopeAgentAdvicedTransformer(AroundReceiveAdvice.class, named("aroundReceive"))))
: emptyCollection();
}
public static class AroundReceiveAdvice {
@Advice.OnMethodEnter
public static ScopeSpanContainer enter(@Advice.This 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 Actor thiz, @Advice.Argument(1) Object msg, @Advice.Enter 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