Skip to content

Commit

Permalink
Support ordering of InstrumenterModules
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Mar 27, 2024
1 parent 1a0ea42 commit e367ec9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -256,6 +257,8 @@ static List<InstrumenterModule> loadModules(ClassLoader loader) throws IOExcepti
log.error("Failed to load instrumentation module {}", moduleName, e);
}
}
// enforce module ordering (lowest-value first) before indexing
modules.sort(Comparator.comparing(InstrumenterModule::order));
return modules;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public Iterable<String> names() {
return instrumentationNames;
}

/** Modules with higher order values are applied <i>after</i> those with lower values. */
public int order() {
return 0;
}

public List<Instrumenter> typeInstrumentations() {
return singletonList(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class InstrumenterIndexTest extends DDSpecification {
InstrumenterIndex index = InstrumenterIndex.buildIndex()

then:
index.instrumentationCount() == 2
index.transformationCount() == 6
index.instrumentationCount() == 4
index.transformationCount() == 8

index.instrumentationId(unknownInstrumentation) == -1
index.transformationId(unknownTransformation) == -1
Expand All @@ -26,19 +26,28 @@ class InstrumenterIndexTest extends DDSpecification {

moduleIterator.hasNext()

// module with order=-100 is applied first
def firstModule = moduleIterator.next()
firstModule.class.simpleName == 'TestIndexFirstModule'
firstModule.order() == -100
index.instrumentationId(firstModule) == 0

moduleIterator.hasNext()

// multi-module declares several transformations
def multiModule = moduleIterator.next()
index.instrumentationId(multiModule) == 0
multiModule.class.simpleName == 'TestIndexMultiModule'
index.instrumentationId(multiModule) == 1

def multiItr = multiModule.typeInstrumentations().iterator()
index.transformationId(unknownTransformation) == -1
index.transformationId(multiItr.next()) == 0
index.transformationId(multiItr.next()) == 1
index.transformationId(unknownTransformation) == -1
index.transformationId(multiItr.next()) == 2
index.transformationId(unknownTransformation) == -1
index.transformationId(multiItr.next()) == 3
index.transformationId(unknownTransformation) == -1
index.transformationId(multiItr.next()) == 4
index.transformationId(multiItr.next()) == 5
index.transformationId(unknownTransformation) == -1
!multiItr.hasNext()

Expand All @@ -49,12 +58,21 @@ class InstrumenterIndexTest extends DDSpecification {

// self-module just declares itself as a transformation
def selfModule = moduleIterator.next()
index.instrumentationId(selfModule) == 1
selfModule.class.simpleName == 'TestIndexSelfModule'
index.instrumentationId(selfModule) == 2

def selfItr = selfModule.typeInstrumentations().iterator()
index.transformationId(selfItr.next()) == 5
index.transformationId(selfItr.next()) == 6
!selfItr.hasNext()

moduleIterator.hasNext()

// module with order=100 is applied last
def lastModule = moduleIterator.next()
lastModule.class.simpleName == 'TestIndexLastModule'
lastModule.order() == 100
index.instrumentationId(lastModule) == 3

!moduleIterator.hasNext()

index.instrumentationId(unknownInstrumentation) == -1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package datadog.trace.agent.test;

import datadog.trace.agent.tooling.InstrumenterModule;

public class TestIndexFirstModule extends InstrumenterModule {
public TestIndexFirstModule() {
super("test-index-priority");
}

@Override
public int order() {
return -100; // lower-values applied first
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package datadog.trace.agent.test;

import datadog.trace.agent.tooling.InstrumenterModule;

public class TestIndexLastModule extends InstrumenterModule {
public TestIndexLastModule() {
super("test-index-priority");
}

@Override
public int order() {
return 100; // higher-values applied last
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
datadog.trace.agent.test.TestIndexMultiModule
datadog.trace.agent.test.TestIndexLastModule
datadog.trace.agent.test.TestIndexFirstModule
datadog.trace.agent.test.TestIndexSelfModule

0 comments on commit e367ec9

Please sign in to comment.