Skip to content

Commit

Permalink
added GLobalVariables to jaxRs instrumentation for storing useAnnotat…
Browse files Browse the repository at this point in the history
…ionValueForTransactionName config value
  • Loading branch information
kananindzya committed Sep 1, 2020
1 parent 7bc4add commit 62e0e74
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package co.elastic.apm.agent.jaxrs;

import co.elastic.apm.agent.impl.ElasticApmTracer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationSource;
Expand All @@ -41,16 +40,11 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static co.elastic.apm.agent.jaxrs.JaxRsTransactionNameInstrumentation.USE_ANNOTATION_VALUE_CONFIG;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class JaxRsOffsetMappingFactory implements Advice.OffsetMapping.Factory<JaxRsOffsetMappingFactory.JaxRsPath> {

public static boolean useAnnotationValueForTransactionName;

public JaxRsOffsetMappingFactory(ElasticApmTracer tracer) {
useAnnotationValueForTransactionName = tracer.getConfig(JaxRsConfiguration.class).isUseJaxRsPathForTransactionName();
}

@Override
public Class<JaxRsPath> getAnnotationType() {
return JaxRsPath.class;
Expand All @@ -62,7 +56,7 @@ public Advice.OffsetMapping make(ParameterDescription.InDefinedShape target, Ann
@Override
public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Sort sort) {
Object value = null;
if (useAnnotationValueForTransactionName) {
if (JaxRsTransactionNameInstrumentation.CONFIG.get(USE_ANNOTATION_VALUE_CONFIG)) {
value = getTransactionAnnotationValueFromAnnotations(instrumentedMethod, instrumentedType);
}
return Target.ForStackManipulation.of(value);
Expand Down Expand Up @@ -181,7 +175,7 @@ private String ensureStartsWithSlash(String value) {

String buildTransactionName() {
String path = this.classLevelPath + this.methodLevelPath;
return this.method + " " + ((path.isEmpty()) ? "/": path);
return this.method + " " + ((path.isEmpty()) ? "/" : path);
}

public boolean isComplete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.jaxrs.JaxRsOffsetMappingFactory.JaxRsPath;
import co.elastic.apm.agent.sdk.state.GlobalVariables;
import co.elastic.apm.agent.util.VersionUtils;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.NamedElement;
Expand All @@ -41,6 +42,8 @@
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.classLoaderCanLoadClass;
import static co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers.isInAnyPackage;
Expand All @@ -56,26 +59,24 @@

public class JaxRsTransactionNameInstrumentation extends TracerAwareInstrumentation {

public static boolean useAnnotationValueForTransactionName;

private final Collection<String> applicationPackages;
private final JaxRsConfiguration configuration;
private final ElasticApmTracer tracer;
static final String USE_ANNOTATION_VALUE_CONFIG = "useAnnotationValueForTransactionName";
static final Map<String, Boolean> CONFIG = GlobalVariables.get(JaxRsConfiguration.class, "configs", new HashMap<>(1));

public JaxRsTransactionNameInstrumentation(ElasticApmTracer tracer) {
this.tracer = tracer;
applicationPackages = tracer.getConfig(StacktraceConfiguration.class).getApplicationPackages();
configuration = tracer.getConfig(JaxRsConfiguration.class);
useAnnotationValueForTransactionName = configuration.isUseJaxRsPathForTransactionName();
CONFIG.put(USE_ANNOTATION_VALUE_CONFIG, configuration.isUseJaxRsPathForTransactionName());
}

@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
public static void setTransactionName(@SimpleMethodSignature String signature,
@JaxRsPath @Nullable String pathAnnotationValue) {
@JaxRsPath @Nullable String pathAnnotationValue) {
final Transaction transaction = TracerAwareInstrumentation.tracer.currentTransaction();
if (transaction != null) {
String transactionName = signature;
if (useAnnotationValueForTransactionName) {
if (CONFIG.get(USE_ANNOTATION_VALUE_CONFIG)) {
if (pathAnnotationValue != null) {
transactionName = pathAnnotationValue;
}
Expand Down Expand Up @@ -140,7 +141,7 @@ public Collection<String> getInstrumentationGroupNames() {
@Nullable
@Override
public Advice.OffsetMapping.Factory<?> getOffsetMapping() {
return new JaxRsOffsetMappingFactory(tracer);
return new JaxRsOffsetMappingFactory();
}

}

0 comments on commit 62e0e74

Please sign in to comment.