Skip to content
Merged
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,6 +37,7 @@
import javax.annotation.Nullable;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import java.util.Collection;
import java.util.ServiceLoader;

import static co.elastic.apm.bci.bytebuddy.ClassLoaderNameMatcher.classLoaderWithName;
Expand Down Expand Up @@ -91,9 +92,8 @@ public static void initInstrumentation(ElasticApmTracer tracer, Instrumentation
.with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE);
AgentBuilder agentBuilder = getAgentBuilder(byteBuddy);
int numberOfAdvices = 0;
final boolean instrument = tracer.getConfig(CoreConfiguration.class).isInstrument();
for (final ElasticApmInstrumentation advice : instrumentations) {
if (advice.includeWhenInstrumentationIsDisabled() || instrument) {
if (isIncluded(advice, tracer.getConfig(CoreConfiguration.class))) {
numberOfAdvices++;
agentBuilder = applyAdvice(tracer, agentBuilder, advice);
}
Expand All @@ -103,6 +103,19 @@ public static void initInstrumentation(ElasticApmTracer tracer, Instrumentation
resettableClassFileTransformer = agentBuilder.installOn(ElasticApmAgent.instrumentation);
}

private static boolean isIncluded(ElasticApmInstrumentation advice, CoreConfiguration coreConfiguration) {
final Collection<String> disabledInstrumentations = coreConfiguration.getDisabledInstrumentations();
return !isGroupDisabled(disabledInstrumentations, advice.getInstrumentationGroupName()) && isInstrumentationEnabled(advice, coreConfiguration);
}

private static boolean isGroupDisabled(Collection<String> disabledInstrumentations, String instrumentationGroupName) {
return disabledInstrumentations.contains(instrumentationGroupName);
}

private static boolean isInstrumentationEnabled(ElasticApmInstrumentation advice, CoreConfiguration coreConfiguration) {
return advice.includeWhenInstrumentationIsDisabled() || coreConfiguration.isInstrument();
}

private static AgentBuilder applyAdvice(final ElasticApmTracer tracer, final AgentBuilder agentBuilder,
final ElasticApmInstrumentation advice) {
logger.debug("Applying advice {}", advice.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ public boolean includeWhenInstrumentationIsDisabled() {
return false;
}

/**
* Returns a name which groups several instrumentations into a logical group.
* <p>
* This name is used in {@link co.elastic.apm.configuration.CoreConfiguration#disabledInstrumentations} to exclude a logical group
* of instrumentations.
* </p>
* @return a name which groups several instrumentations into a logical group
*/
public abstract String getInstrumentationGroupName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.stagemonitor.configuration.converter.ListValueConverter;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class CoreConfiguration extends ConfigurationOptionProvider {
Expand Down Expand Up @@ -164,6 +166,12 @@ public void assertValid(Double value) {
.description("Enables distributed tracing and uses the updated json schema to serialize payloads, transactions and spans")
.buildWithDefault(false);

private final ConfigurationOption<Collection<String>> disabledInstrumentations = ConfigurationOption.stringsOption()
.key("disabled_instrumentations")
.configurationCategory(CORE_CATEGORY)
.description("A list of instrumentations which should be disabled. Valid options are `jdbc`, `servlet-api` and `spring-mvc`.")
.buildWithDefault(Collections.<String>emptyList());

public boolean isActive() {
return active.get();
}
Expand Down Expand Up @@ -199,4 +207,8 @@ public List<WildcardMatcher> getSanitizeFieldNames() {
public boolean isDistributedTracingEnabled() {
return distributedTracing.get();
}

public Collection<String> getDisabledInstrumentations() {
return disabledInstrumentations.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package co.elastic.apm.bci;

import co.elastic.apm.configuration.CoreConfiguration;
import co.elastic.apm.configuration.SpyConfiguration;
import co.elastic.apm.impl.ElasticApmTracer;
import net.bytebuddy.agent.ByteBuddyAgent;
Expand All @@ -27,31 +28,44 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.stagemonitor.configuration.ConfigurationRegistry;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

class InstrumentationTest {

@BeforeEach
void setUp() {
ElasticApmAgent.initInstrumentation(ElasticApmTracer.builder()
.configurationRegistry(SpyConfiguration.createSpyConfig())
.build(), ByteBuddyAgent.install());
}

@AfterAll
static void afterAll() {
@AfterEach
void afterAll() {
ElasticApmAgent.reset();
}

@Test
void testIntercept() {
init(SpyConfiguration.createSpyConfig());
assertThat(interceptMe()).isEqualTo("intercepted");
}

@Test
void testDisabled() {
final ConfigurationRegistry config = SpyConfiguration.createSpyConfig();
when(config.getConfig(CoreConfiguration.class).getDisabledInstrumentations()).thenReturn(Collections.singletonList("test"));
init(config);
assertThat(interceptMe()).isEmpty();
}

private void init(ConfigurationRegistry config) {
ElasticApmAgent.initInstrumentation(ElasticApmTracer.builder()
.configurationRegistry(config)
.build(),
ByteBuddyAgent.install(),
Collections.singletonList(new TestInstrumentation()));
}

private String interceptMe() {
return "";
}
Expand All @@ -71,5 +85,10 @@ public ElementMatcher<? super TypeDescription> getTypeMatcher() {
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return ElementMatchers.named("interceptMe");
}

@Override
public String getInstrumentationGroupName() {
return "test";
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
public class ElasticApmApiInstrumentation extends ElasticApmInstrumentation {

static final String PUBLIC_API_INSTRUMENTATION_GROUP = "public-api";
private final ElementMatcher<? super MethodDescription> methodMatcher;

ElasticApmApiInstrumentation(ElementMatcher<? super MethodDescription> methodMatcher) {
Expand All @@ -56,6 +57,11 @@ public boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getInstrumentationGroupName() {
return PUBLIC_API_INSTRUMENTATION_GROUP;
}

public static class StartTransactionInstrumentation extends ElasticApmApiInstrumentation {
public StartTransactionInstrumentation() {
super(named("doStartTransaction"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;

import static co.elastic.apm.plugin.api.ElasticApmApiInstrumentation.PUBLIC_API_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.named;

/**
Expand Down Expand Up @@ -56,6 +57,11 @@ public boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getInstrumentationGroupName() {
return PUBLIC_API_INSTRUMENTATION_GROUP;
}

public static class SetNameInstrumentation extends SpanInstrumentation {
public SetNameInstrumentation() {
super(named("setName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;

import static co.elastic.apm.plugin.api.ElasticApmApiInstrumentation.PUBLIC_API_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.named;

/**
Expand All @@ -51,6 +52,11 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return methodMatcher;
}

@Override
public String getInstrumentationGroupName() {
return PUBLIC_API_INSTRUMENTATION_GROUP;
}

@Override
public boolean includeWhenInstrumentationIsDisabled() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*/
public class ConnectionInstrumentation extends ElasticApmInstrumentation {

static final String JDBC_INSTRUMENTATION_GROUP = "jdbc";
private static final Map<PreparedStatement, String> statementSqlMap =
Collections.synchronizedMap(new WeakHashMap<PreparedStatement, String>());

Expand Down Expand Up @@ -90,4 +91,9 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
.and(takesArgument(0, String.class));
}

@Override
public String getInstrumentationGroupName() {
return JDBC_INSTRUMENTATION_GROUP;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static co.elastic.apm.jdbc.ConnectionInstrumentation.JDBC_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
Expand Down Expand Up @@ -90,4 +91,9 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
.and(isPublic())
.and(takesArguments(0));
}

@Override
public String getInstrumentationGroupName() {
return JDBC_INSTRUMENTATION_GROUP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.sql.SQLException;
import java.sql.Statement;

import static co.elastic.apm.jdbc.ConnectionInstrumentation.JDBC_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.isSubTypeOf;
Expand Down Expand Up @@ -92,4 +93,9 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
.and(isPublic())
.and(takesArgument(0, String.class));
}

@Override
public String getInstrumentationGroupName() {
return JDBC_INSTRUMENTATION_GROUP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -28,6 +28,7 @@

import javax.annotation.Nullable;

import static co.elastic.apm.opentracing.impl.ApmSpanInstrumentation.OPENTRACING_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class ApmScopeInstrumentation extends ElasticApmInstrumentation {
Expand Down Expand Up @@ -59,4 +60,9 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
public boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getInstrumentationGroupName() {
return OPENTRACING_INSTRUMENTATION_GROUP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -37,6 +37,7 @@
import javax.annotation.Nullable;
import java.util.Map;

import static co.elastic.apm.opentracing.impl.ApmSpanInstrumentation.OPENTRACING_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class ApmSpanBuilderInstrumentation extends ElasticApmInstrumentation {
Expand Down Expand Up @@ -68,6 +69,11 @@ public boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getInstrumentationGroupName() {
return OPENTRACING_INSTRUMENTATION_GROUP;
}

public static class StartApmSpanInstrumentation extends ApmSpanBuilderInstrumentation {

public StartApmSpanInstrumentation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

public class ApmSpanInstrumentation extends ElasticApmInstrumentation {

static final String OPENTRACING_INSTRUMENTATION_GROUP = "opentracing";

private final ElementMatcher<? super MethodDescription> methodMatcher;

public ApmSpanInstrumentation(ElementMatcher<? super MethodDescription> methodMatcher) {
Expand All @@ -60,6 +62,11 @@ public boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getInstrumentationGroupName() {
return OPENTRACING_INSTRUMENTATION_GROUP;
}

public static class FinishInstrumentation extends ApmSpanInstrumentation {
public FinishInstrumentation() {
super(named("finishInternal"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import javax.annotation.Nullable;

import static co.elastic.apm.opentracing.impl.ApmSpanInstrumentation.OPENTRACING_INSTRUMENTATION_GROUP;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class ScopeManagerInstrumentation extends ElasticApmInstrumentation {
Expand All @@ -56,6 +57,11 @@ public boolean includeWhenInstrumentationIsDisabled() {
return true;
}

@Override
public String getInstrumentationGroupName() {
return OPENTRACING_INSTRUMENTATION_GROUP;
}

public static class ActivateInstrumentation extends ScopeManagerInstrumentation {

public ActivateInstrumentation() {
Expand Down
Loading