-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: update methods hooks according to rules
- Loading branch information
Showing
15 changed files
with
265 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ks/inspectit/gepard/agent/instrumentation/hook/configuration/MethodHookConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.configuration; | ||
|
||
import java.util.Objects; | ||
import rocks.inspectit.gepard.config.model.instrumentation.rules.RuleTracingConfiguration; | ||
|
||
/** | ||
* Configuration for one specific method hook. Currently just for tracing. Later we will add | ||
* entry-/exit-actions and metrics. | ||
* | ||
* @param methodName the name of the hooked method | ||
* @param tracing the tracing configuration | ||
*/ | ||
public record MethodHookConfiguration(String methodName, RuleTracingConfiguration tracing) { | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other instanceof MethodHookConfiguration otherConfig) | ||
return methodName.equals(otherConfig.methodName) && tracing.equals(otherConfig.tracing); | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(methodName, tracing); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ctit/gepard/agent/instrumentation/hook/configuration/MethodHookConfigurationResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.configuration; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import net.bytebuddy.description.method.MethodDescription; | ||
import rocks.inspectit.gepard.agent.instrumentation.hook.configuration.exception.ConflictingConfigurationException; | ||
import rocks.inspectit.gepard.agent.internal.instrumentation.model.ClassInstrumentationConfiguration; | ||
import rocks.inspectit.gepard.agent.internal.instrumentation.model.rules.InstrumentationRule; | ||
import rocks.inspectit.gepard.config.model.instrumentation.rules.RuleTracingConfiguration; | ||
|
||
/** | ||
* Resolves a {@link ClassInstrumentationConfiguration} to a {@link MethodHookConfiguration} of a | ||
* specific method. | ||
*/ | ||
public class MethodHookConfigurationResolver { | ||
|
||
/** | ||
* Resolve the configuration for a specific method hook. | ||
* | ||
* @param method the method of the current class | ||
* @param classConfig the instrumentation configuration of the current class | ||
* @return the hook configuration for the provided method | ||
*/ | ||
public MethodHookConfiguration resolve( | ||
MethodDescription method, ClassInstrumentationConfiguration classConfig) { | ||
|
||
Set<InstrumentationRule> matchedRules = | ||
classConfig.activeRules().stream() | ||
.filter(rule -> rule.methodMatcher().matches(method)) | ||
.collect(Collectors.toSet()); | ||
|
||
String methodName = method.getName(); | ||
RuleTracingConfiguration tracing = resolveTracing(matchedRules); | ||
return new MethodHookConfiguration(methodName, tracing); | ||
} | ||
|
||
/** | ||
* Resolve the tracing configuration for a specific method hook. | ||
* | ||
* @param rules the rules for the current method | ||
* @return the tracing configuration | ||
*/ | ||
private RuleTracingConfiguration resolveTracing(Set<InstrumentationRule> rules) { | ||
boolean allMatch = rules.stream().allMatch(rule -> rule.tracing().getStartSpan()); | ||
|
||
if (allMatch && !rules.isEmpty()) return rules.stream().findFirst().get().tracing(); | ||
else throw new ConflictingConfigurationException("Conflict in rule tracing configuration"); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...agent/instrumentation/hook/configuration/exception/ConflictingConfigurationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.configuration.exception; | ||
|
||
/** Exception errors, while finding conflicts inside the instrumentation configuration. */ | ||
public class ConflictingConfigurationException extends RuntimeException { | ||
|
||
public ConflictingConfigurationException(String message) { | ||
super(message); | ||
} | ||
} |
14 changes: 9 additions & 5 deletions
14
...main/java/rocks/inspectit/gepard/agent/instrumentation/hook/util/MethodHookGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.util; | ||
|
||
import net.bytebuddy.description.method.MethodDescription; | ||
import rocks.inspectit.gepard.agent.instrumentation.hook.MethodHook; | ||
import rocks.inspectit.gepard.agent.instrumentation.hook.action.SpanAction; | ||
import rocks.inspectit.gepard.agent.instrumentation.hook.configuration.MethodHookConfiguration; | ||
|
||
/** Creates method hook objects */ | ||
public class MethodHookGenerator { | ||
|
||
private MethodHookGenerator() {} | ||
|
||
/** | ||
* Creates an executable method hook based on the given configuration. | ||
* | ||
* @param method the hooked method | ||
* @param hookConfig the configuration for the hook | ||
* @return the created method hook | ||
*/ | ||
public static MethodHook createHook(MethodDescription method) { | ||
SpanAction spanAction = new SpanAction(); | ||
return new MethodHook(method.getName(), spanAction); | ||
public static MethodHook createHook(MethodHookConfiguration hookConfig) { | ||
MethodHook.Builder builder = MethodHook.builder().setConfiguration(hookConfig); | ||
|
||
if (hookConfig.tracing().getStartSpan()) builder.setSpanAction(new SpanAction()); | ||
|
||
return builder.build(); | ||
} | ||
} |
Oops, something went wrong.