Skip to content

Commit

Permalink
extend ClassInstrumentationConfiguration and adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Sep 11, 2024
1 parent 9a1bc17 commit 04d0088
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Set<Class<?>> getNextBatch(int batchSize) {

try {
ClassInstrumentationConfiguration instrumentationConfiguration =
configurationResolver.getActiveConfiguration(clazz);
configurationResolver.getClassInstrumentationConfiguration(clazz);
boolean shouldRetransform =
instrumentationState.shouldRetransform(clazz, instrumentationConfiguration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
*/
public class Scope {

private boolean enabled;

private String fqn;

private List<String> methods;

private boolean enabled;

public Scope() {}

public Scope(String fqn, boolean enabled) {
public Scope(boolean enabled, String fqn, List<String> methods) {
this.fqn = fqn;
this.methods = methods;
this.enabled = enabled;
}

public Scope(String fqn, List<String> methods, boolean enabled) {
this.fqn = fqn;
this.methods = methods;
this.enabled = enabled;
public boolean isEnabled() {
return enabled;
}

public String getFqn() {
Expand All @@ -34,8 +33,4 @@ public String getFqn() {
public List<String> getMethods() {
return methods;
}

public boolean isEnabled() {
return enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,24 @@ public static InstrumentationState create() {
return new InstrumentationState();
}

public boolean shouldRetransform(
Class<?> clazz, ClassInstrumentationConfiguration currentConfig) {
ClassInstrumentationConfiguration activeConfig =
activeInstrumentations.asMap().entrySet().stream()
.filter(entry -> entry.getKey().isEqualTo(clazz)) // find class
.map(Map.Entry::getValue) // get configuration
.findAny()
.orElse(null);

if (Objects.nonNull(activeConfig)) return !activeConfig.equals(currentConfig);
return currentConfig.isActive();
}

/**
* Checks, if the provided class is already instrumented.
* Checks, if the provided class should be retransformed. A retransformation is necessary, if the
* new configuration differs from the current configuration.
*
* @param clazz the class object
* @return true, if the provided class is already instrumented.
* @param clazz the class
* @param newConfig the new instrumentation configuration
* @return true, if the provided class should be retransformed
*/
public boolean isInstrumented(Class<?> clazz) {
public boolean shouldRetransform(Class<?> clazz, ClassInstrumentationConfiguration newConfig) {
ClassInstrumentationConfiguration activeConfig =
activeInstrumentations.asMap().entrySet().stream()
.filter(entry -> entry.getKey().isEqualTo(clazz))
.map(Map.Entry::getValue)
.filter(entry -> entry.getKey().isEqualTo(clazz)) // find class
.map(Map.Entry::getValue) // get configuration
.findAny()
.orElse(null);

// if (Objects.nonNull(activeConfig)) return activeConfig.isInstrumented();
return false;
if (Objects.nonNull(activeConfig)) return !activeConfig.equals(newConfig);
return newConfig.isActive();
}

/**
Expand All @@ -63,10 +52,10 @@ public boolean isInstrumented(Class<?> clazz) {
* @return true, if the provided type is already instrumented.
*/
public boolean isInstrumented(InstrumentedType instrumentedType) {
ClassInstrumentationConfiguration activeConfig =
ClassInstrumentationConfiguration config =
activeInstrumentations.getIfPresent(instrumentedType);

// if (Objects.nonNull(activeConfig)) return activeConfig.isInstrumented();
if (Objects.nonNull(config)) return config.isActive();
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public record ClassInstrumentationConfiguration(Set<InstrumentationScope> active
public static final ClassInstrumentationConfiguration NO_INSTRUMENTATION =
new ClassInstrumentationConfiguration(Collections.emptySet());

/**
* Checks, if this configuration induces bytecode changes to the target class.
*
* @return true, if this configuration expects instrumentation
*/
public boolean isActive() {
return !activeScopes.isEmpty();
}

@Override
public boolean equals(Object o) {
if (o instanceof ClassInstrumentationConfiguration otherConfig)
Expand All @@ -25,13 +34,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(activeScopes);
}

/**
* Checks, if this configuration induces bytecode changes to the target class.
*
* @return true, if this configuration expects instrumentation
*/
public boolean isActive() {
return !activeScopes.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import rocks.inspectit.gepard.agent.internal.configuration.model.instrumentation.Scope;

/**
* @param fqn
* @param methods
* @param fqn the fully qualified name of a class
* @param methods the methods of the class to instrument
*/
public record InstrumentationScope(String fqn, List<String> methods) {

/**
* @param scope
* @return
* Creates an {@link InstrumentationScope} out of a {@link Scope}
*
* @param scope the scope
* @return the instrumentation scope
*/
public static InstrumentationScope create(Scope scope) {
return new InstrumentationScope(scope.getFqn(), scope.getMethods());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,43 @@ public static ConfigurationResolver create(ConfigurationHolder holder) {
}

/**
* @param clazz
* @return
* Gets the current instrumentation configuration for the specified class.
*
* @param clazz the class
* @return The active configuration or {@link
* ClassInstrumentationConfiguration#NO_INSTRUMENTATION}
*/
public ClassInstrumentationConfiguration getActiveConfiguration(Class<?> clazz) {
public ClassInstrumentationConfiguration getClassInstrumentationConfiguration(Class<?> clazz) {
String className = clazz.getName();
Set<InstrumentationScope> activeScopes = scopeResolver.getActiveScopes(className);

if (activeScopes.isEmpty()) return ClassInstrumentationConfiguration.NO_INSTRUMENTATION;
return new ClassInstrumentationConfiguration(activeScopes);
}

public ClassInstrumentationConfiguration getActiveConfiguration(TypeDescription typeDescription) {
String className = typeDescription.getName();
Set<InstrumentationScope> activeScopes = scopeResolver.getActiveScopes(className);

// TODO Das sollte eigtl nicht passieren können?
if (activeScopes.isEmpty()) return ClassInstrumentationConfiguration.NO_INSTRUMENTATION;
return new ClassInstrumentationConfiguration(activeScopes);
return getClassInstrumentationConfiguration(className);
}

/**
* Checks, if the provided class requires instrumentation.
* Gets the current instrumentation configuration for the specified class.
*
* @param clazz the class object
* @return true, if the provided class should be instrumented
* @param typeDescription the type
* @return The active configuration or {@link
* ClassInstrumentationConfiguration#NO_INSTRUMENTATION}
*/
public boolean shouldInstrument(Class<?> clazz) {
// TODO REMOVE
String className = clazz.getName();
return shouldInstrument(className);
public ClassInstrumentationConfiguration getClassInstrumentationConfiguration(
TypeDescription typeDescription) {
String typeName = typeDescription.getName();
return getClassInstrumentationConfiguration(typeName);
}

/**
* Checks, if the provided type requires instrumentation.
* Gets the current instrumentation configuration for the provided class name
*
* @param type the class type description
* @return true, if the provided type should be instrumented
* @param fullyQualifiedName the class name
* @return The active configuration or {@link
* ClassInstrumentationConfiguration#NO_INSTRUMENTATION}
*/
public boolean shouldInstrument(TypeDescription type) {
String typeName = type.getName();
return shouldInstrument(typeName);
private ClassInstrumentationConfiguration getClassInstrumentationConfiguration(
String fullyQualifiedName) {
Set<InstrumentationScope> activeScopes = scopeResolver.getActiveScopes(fullyQualifiedName);

if (activeScopes.isEmpty()) return ClassInstrumentationConfiguration.NO_INSTRUMENTATION;
return new ClassInstrumentationConfiguration(activeScopes);
}

/**
Expand All @@ -82,16 +78,6 @@ public boolean shouldInstrument(TypeDescription type) {
*/
public ElementMatcher.Junction<MethodDescription> getMethodMatcher(
TypeDescription typeDescription) {
return scopeResolver.buildMethodMatcher(typeDescription);
}

/**
* Checks, if the provided class name requires instrumentation.
*
* @param fullyQualifiedName the full name of the class
* @return true, if the provided class should be instrumented
*/
private boolean shouldInstrument(String fullyQualifiedName) {
return scopeResolver.shouldInstrument(fullyQualifiedName);
return scopeResolver.getMethodMatcher(typeDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
public class CustomElementMatchers {

private CustomElementMatchers() {}

/**
* Creates an {@link ElementMatcher} matching items with the given name settings. Currently, there
* are no nameSettings available, so we will just use the methodName. Will be enhanced, as soon as
Expand Down
Loading

0 comments on commit 04d0088

Please sign in to comment.