Skip to content
Draft
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 @@ -200,12 +200,18 @@ public static ClassFileTransformer installBytebuddyAgent(

int installedCount = 0;
for (InstrumenterModule module : instrumenterModules) {
if (!module.isApplicable(enabledSystems)) {
if (!enabledSystems.contains(module.targetSystem())) {
if (DEBUG) {
log.debug("Not applicable - instrumentation.class={}", module.getClass().getName());
}
continue;
}
if (!module.isEnabled(enabledSystems)) {
if (DEBUG) {
log.debug("Not enabled - instrumentation.class={}", module.getClass().getName());
}
continue;
}
if (DEBUG) {
log.debug("Loading - instrumentation.class={}", module.getClass().getName());
}
Expand Down Expand Up @@ -291,7 +297,7 @@ public InstrumenterModule next() {

public static Set<InstrumenterModule.TargetSystem> getEnabledSystems() {
EnumSet<InstrumenterModule.TargetSystem> enabledSystems =
EnumSet.noneOf(InstrumenterModule.TargetSystem.class);
EnumSet.of(InstrumenterModule.TargetSystem.COMMON);
InstrumenterConfig cfg = InstrumenterConfig.get();
if (cfg.isTraceEnabled()) {
enabledSystems.add(InstrumenterModule.TargetSystem.TRACING);
Expand All @@ -300,9 +306,11 @@ public static Set<InstrumenterModule.TargetSystem> getEnabledSystems() {
enabledSystems.add(InstrumenterModule.TargetSystem.PROFILING);
}
if (cfg.getAppSecActivation() != ProductActivation.FULLY_DISABLED) {
enabledSystems.add(InstrumenterModule.TargetSystem.SECURITY);
enabledSystems.add(InstrumenterModule.TargetSystem.APPSEC);
}
if (cfg.getIastActivation() != ProductActivation.FULLY_DISABLED) {
enabledSystems.add(InstrumenterModule.TargetSystem.SECURITY);
enabledSystems.add(InstrumenterModule.TargetSystem.IAST);
}
if (cfg.isCiVisibilityEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,15 @@ public CombiningTransformerBuilder(

/** Builds matchers and transformers for an instrumentation module and its members. */
public void applyInstrumentation(InstrumenterModule module) {
if (module.isEnabled()) {
int instrumentationId = instrumenterIndex.instrumentationId(module);
if (instrumentationId < 0) {
// this is a non-indexed instrumentation configured at runtime
instrumentationId = nextRuntimeInstrumentationId++;
}
InstrumenterState.registerInstrumentation(module, instrumentationId);
prepareInstrumentation(module, instrumentationId);
for (Instrumenter member : module.typeInstrumentations()) {
buildTypeInstrumentation(member);
}
int instrumentationId = instrumenterIndex.instrumentationId(module);
if (instrumentationId < 0) {
// this is a non-indexed instrumentation configured at runtime
instrumentationId = nextRuntimeInstrumentationId++;
}
InstrumenterState.registerInstrumentation(module, instrumentationId);
prepareInstrumentation(module, instrumentationId);
for (Instrumenter member : module.typeInstrumentations()) {
buildTypeInstrumentation(member);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package datadog.trace.agent.test


import datadog.trace.agent.tooling.InstrumenterModule
import datadog.trace.agent.tooling.InstrumenterModule.TargetSystem
import datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers
import datadog.trace.agent.tooling.bytebuddy.outline.TypePoolFacade
import datadog.trace.test.util.DDSpecification
Expand All @@ -17,12 +17,12 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
def target = new TestDefaultInstrumenter("test")

expect:
target.enabled
assertEnabled(target)
}

def "default enabled override #enabled"() {
expect:
target.enabled == enabled
assertEnabled(target, enabled)

where:
enabled | target
Expand Down Expand Up @@ -51,7 +51,7 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
}

expect:
target.enabled == enabled
assertEnabled(target, enabled)

where:
enabled << [true, false]
Expand All @@ -65,7 +65,7 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
def target = new TestDefaultInstrumenter("test")

then:
target.enabled == enabled
assertEnabled(target, enabled)

where:
value | enabled
Expand All @@ -80,7 +80,7 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
def target = new TestDefaultInstrumenter("test")

expect:
target.enabled == enabled
assertEnabled(target, enabled)

where:
value | enabled
Expand All @@ -96,7 +96,7 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
def target = new TestDefaultInstrumenter(name, altName)

expect:
target.enabled == enabled
assertEnabled(target, enabled)

where:
value | enabled | name | altName
Expand All @@ -119,7 +119,7 @@ class DefaultInstrumenterForkedTest extends DDSpecification {

then:
System.getenv("DD_INTEGRATION_${value}_ENABLED") == "true"
target.enabled == enabled
assertEnabled(target, enabled)

where:
value | enabled | name | altName
Expand All @@ -132,6 +132,10 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
"PERIOD_TEST" | true | "period.test" | "asdf"
}

def assertEnabled(target, enabled = true) {
target.isEnabled(EnumSet.of(TargetSystem.TRACING)) == enabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it fail with assert?

Suggested change
target.isEnabled(EnumSet.of(TargetSystem.TRACING)) == enabled
assert target.isEnabled(EnumSet.of(TargetSystem.TRACING)) == enabled

}

class TestDefaultInstrumenter extends InstrumenterModule.Tracing {

TestDefaultInstrumenter(String instrumentationName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public CallSiteBenchmarkInstrumentation() {
}

@Override
public ElementMatcher<TypeDescription> callerType() {
return CallSiteMatcher.INSTANCE;
public TargetSystem targetSystem() {
return TargetSystem.COMMON; // always active during this benchmark
}

@Override
public boolean isEnabled() {
return "callSite".equals(System.getProperty("dd.benchmark.instrumentation", ""));
public ElementMatcher<TypeDescription> callerType() {
return CallSiteMatcher.INSTANCE;
}

@Override
public boolean isApplicable(final Set<TargetSystem> enabledSystems) {
return true;
public boolean isEnabled(Set<TargetSystem> enabledSystems) {
return "callSite".equals(System.getProperty("dd.benchmark.instrumentation", ""));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public String[] helperClassNames() {
}

@Override
public boolean isEnabled() {
return "callee".equals(System.getProperty("dd.benchmark.instrumentation", ""));
public TargetSystem targetSystem() {
return TargetSystem.COMMON; // always active during this benchmark
}

@Override
public boolean isApplicable(final Set<TargetSystem> enabledSystems) {
return true;
public boolean isEnabled(Set<TargetSystem> enabledSystems) {
return "callee".equals(System.getProperty("dd.benchmark.instrumentation", ""));
}

public static class Muzzle extends ReferenceMatcher {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,13 @@ public abstract class InstrumenterModule implements Instrumenter {
/**
* Since several systems share the same instrumentation infrastructure in order to enable only the
* applicable {@link Instrumenter instrumenters} on startup each {@linkplain InstrumenterModule}
* must declare its target system. The systems currently supported include:
*
* <ul>
* <li>{@link TargetSystem#TRACING tracing}
* <li>{@link TargetSystem#PROFILING profiling}
* <li>{@link TargetSystem#APPSEC appsec}
* <li>{@link TargetSystem#IAST iast}
* <li>{@link TargetSystem#CIVISIBILITY ci-visibility}
* <li>{@link TargetSystem#USM usm}
* </ul>
* must declare its target system.
*/
public enum TargetSystem {
COMMON, // instrumentation common to every system
TRACING,
PROFILING,
SECURITY, // instrumentation shared between APPSEC and IAST
APPSEC,
IAST,
CIVISIBILITY,
Expand Down Expand Up @@ -177,25 +170,16 @@ public Map<String, String> contextStore() {
return emptyMap();
}

public abstract TargetSystem targetSystem();

protected boolean defaultEnabled() {
return InstrumenterConfig.get().isIntegrationsEnabled();
}

public boolean isEnabled() {
public boolean isEnabled(Set<TargetSystem> enabledSystems) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I found the enabledSystems name a bit confusing for the isEnabled() method.
"Is the module enabled for those enabled systems"

What about public boolean isEnabledFor(Set<TargetSystem> systems);?
From the module POV, it does not have to know if the given systems are enabled or not, just to answer "should I be enabled for those systems". That's a nitpick but I feel it would add clarity when there is no Javadoc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily for this PR, but I've been pondering whether annotations would be a better way to specify this type of information.
My thinking is that annotations would force you to only use static information. And we could also index the information via an annotation processor.

return enabled;
}

/**
* Indicates the applicability of an {@linkplain InstrumenterModule} to the given system.<br>
*
* @param enabledSystems a set of all the enabled target systems
* @return {@literal true} if the set of enabled systems contains all the ones required by this
* particular {@linkplain InstrumenterModule}
*/
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return false;
}

protected final boolean isShortcutMatchingEnabled(boolean defaultToShortcut) {
return InstrumenterConfig.get()
.isIntegrationShortcutMatchingEnabled(singletonList(name()), defaultToShortcut);
Expand All @@ -208,8 +192,8 @@ public Tracing(String instrumentationName, String... additionalNames) {
}

@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.TRACING);
public TargetSystem targetSystem() {
return TargetSystem.TRACING;
}
}

Expand All @@ -220,13 +204,13 @@ public Profiling(String instrumentationName, String... additionalNames) {
}

@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.PROFILING);
public TargetSystem targetSystem() {
return TargetSystem.PROFILING;
}

@Override
public boolean isEnabled() {
return super.isEnabled()
public boolean isEnabled(Set<TargetSystem> enabledSystems) {
return super.isEnabled(enabledSystems)
&& !ConfigProvider.getInstance()
.getBoolean(ProfilingConfig.PROFILING_ULTRA_MINIMAL, false);
}
Expand All @@ -239,8 +223,8 @@ public AppSec(String instrumentationName, String... additionalNames) {
}

@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.APPSEC);
public TargetSystem targetSystem() {
return TargetSystem.APPSEC;
}
}

Expand All @@ -251,14 +235,22 @@ public Iast(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}

@Override
public TargetSystem targetSystem() {
return TargetSystem.SECURITY;
}

@Override
public List<Instrumenter> typeInstrumentations() {
preloadClassNames();
return super.typeInstrumentations();
}

@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
public boolean isEnabled(Set<TargetSystem> enabledSystems) {
if (!super.isEnabled(enabledSystems)) {
return false;
}
if (enabledSystems.contains(TargetSystem.IAST)) {
return true;
}
Expand Down Expand Up @@ -307,8 +299,8 @@ public Usm(String instrumentationName, String... additionalNames) {
}

@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.USM);
public TargetSystem targetSystem() {
return TargetSystem.USM;
}
}

Expand All @@ -319,8 +311,8 @@ public CiVisibility(String instrumentationName, String... additionalNames) {
}

@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.CIVISIBILITY);
public TargetSystem targetSystem() {
return TargetSystem.CIVISIBILITY;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public CallSiteInstrumentation(
super(name, additionalNames);
}

@Override
public TargetSystem targetSystem() {
return TargetSystem.SECURITY;
}

@Override
public void typeAdvice(TypeTransformer transformer) {
transformer.applyAdvice(new CallSiteTransformer(name(), advices()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import datadog.trace.api.iast.telemetry.IastMetric;
import datadog.trace.api.iast.telemetry.IastMetricCollector;
import datadog.trace.api.iast.telemetry.Verbosity;
import datadog.trace.util.Strings;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
Expand All @@ -44,7 +45,7 @@ public class IastPostProcessorFactory implements Advice.PostProcessor.Factory {
INSTANCE = verbosity == Verbosity.OFF ? null : new IastPostProcessorFactory(verbosity);
}

private static final String IAST_ANNOTATIONS_PKG = Sink.class.getPackage().getName();
private static final String IAST_ANNOTATIONS_PKG = Strings.getPackageName(Sink.class.getName());
private static final String SINK_NAME = Sink.class.getSimpleName();
private static final String PROPAGATION_NAME = Propagation.class.getSimpleName();
private static final String SOURCE_NAME = Source.class.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import spock.lang.Shared
class InstrumenterIndexTest extends DDSpecification {

@Shared
def unknownInstrumentation = new InstrumenterModule('unknown') {}
def unknownInstrumentation = new InstrumenterModule('unknown') {
@Override
InstrumenterModule.TargetSystem targetSystem() {
return InstrumenterModule.TargetSystem.COMMON
}
}

@Shared
def unknownTransformation = new Instrumenter() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public TestIndexFirstModule() {
super("test-index-priority");
}

@Override
public TargetSystem targetSystem() {
return TargetSystem.COMMON;
}

@Override
public int order() {
return -100; // lower-values applied first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public TestIndexLastModule() {
super("test-index-priority");
}

@Override
public TargetSystem targetSystem() {
return TargetSystem.COMMON;
}

@Override
public int order() {
return 100; // higher-values applied last
Expand Down
Loading