-
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.
* refactor: add method hook state * change slf4j dependency * add README note * test: add method hook tests * apply spotless * feature: record spans in method hooks (wip) * refactor: record custom spans with action (wip) * append bootstrap README * apply spotless * refactor: sonarcloud issues * add tests * little refactor * fix test * refactor tests * add tracing integration test * minor fixes
- Loading branch information
Showing
28 changed files
with
834 additions
and
243 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
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
55 changes: 55 additions & 0 deletions
55
...nt/src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/action/SpanAction.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,55 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.action; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.context.Context; | ||
import java.util.Objects; | ||
import rocks.inspectit.gepard.agent.instrumentation.hook.action.exception.CouldNotCloseSpanScopeException; | ||
import rocks.inspectit.gepard.agent.internal.otel.OpenTelemetryAccessor; | ||
|
||
/** This action contains the logic to start and end a {@link Span}. */ | ||
public class SpanAction { | ||
|
||
private static final String INSTRUMENTATION_SCOPE_NAME = "inspectit-gepard"; | ||
|
||
private final OpenTelemetry openTelemetry; | ||
|
||
public SpanAction() { | ||
this.openTelemetry = OpenTelemetryAccessor.getOpenTelemetry(); | ||
} | ||
|
||
/** | ||
* Starts a new {@link Span}. Should be called before {@link SpanAction#endSpan}. | ||
* | ||
* @param spanName the name of the span | ||
* @return the scope of the started span | ||
*/ | ||
public AutoCloseable startSpan(String spanName) { | ||
Span.current().getSpanContext(); | ||
|
||
Tracer tracer = openTelemetry.getTracer(INSTRUMENTATION_SCOPE_NAME); | ||
Span span = tracer.spanBuilder(spanName).setParent(Context.current()).startSpan(); | ||
return span.makeCurrent(); | ||
} | ||
|
||
/** | ||
* Ends the current span and closes its scope. Should be called after {@link | ||
* SpanAction#startSpan}. | ||
* | ||
* @param spanScope the scope of the span, which should be finished | ||
*/ | ||
public void endSpan(AutoCloseable spanScope) { | ||
Span current = Span.current(); | ||
|
||
if (Objects.nonNull(spanScope)) { | ||
try { | ||
spanScope.close(); | ||
} catch (Exception e) { | ||
throw new CouldNotCloseSpanScopeException(e); | ||
} | ||
} | ||
current.end(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...t/gepard/agent/instrumentation/hook/action/exception/CouldNotCloseSpanScopeException.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,12 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.action.exception; | ||
|
||
import io.opentelemetry.context.Scope; | ||
|
||
/** Exception errors, while trying to close a {@link Scope} */ | ||
public class CouldNotCloseSpanScopeException extends RuntimeException { | ||
|
||
public CouldNotCloseSpanScopeException(Throwable cause) { | ||
super("Could not close span scope", cause); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* (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; | ||
|
||
public class MethodHookGenerator { | ||
|
||
private MethodHookGenerator() {} | ||
|
||
/** | ||
* Creates an executable method hook based on the given configuration. | ||
* | ||
* @param method the hooked method | ||
* @return the created method hook | ||
*/ | ||
public static MethodHook createHook(MethodDescription method) { | ||
SpanAction spanAction = new SpanAction(); | ||
return new MethodHook(method.getName(), spanAction); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...agent/src/main/java/rocks/inspectit/gepard/agent/internal/otel/OpenTelemetryAccessor.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,34 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.otel; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
|
||
/** | ||
* Singleton to access the {@link OpenTelemetry} instance. We use this accessor, because according | ||
* to the documentation of {@link GlobalOpenTelemetry}, the get() method should only be called once | ||
* during the application. | ||
*/ | ||
public class OpenTelemetryAccessor { | ||
|
||
private static OpenTelemetry openTelemetry; | ||
|
||
private OpenTelemetryAccessor() {} | ||
|
||
/** | ||
* @return the global {@link OpenTelemetry} instance | ||
*/ | ||
public static OpenTelemetry getOpenTelemetry() { | ||
return openTelemetry; | ||
} | ||
|
||
/** | ||
* Sets the global {@link OpenTelemetry} instance for inspectIT. This will allow us to create | ||
* traces or metrics. Should only be called once. | ||
* | ||
* @param otel the openTelemetry object | ||
*/ | ||
public static void setOpenTelemetry(OpenTelemetry otel) { | ||
openTelemetry = otel; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rd-agent/src/main/java/rocks/inspectit/gepard/agent/transformation/InspectitListener.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,55 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.transformation; | ||
|
||
import net.bytebuddy.agent.builder.AgentBuilder; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.dynamic.DynamicType; | ||
import net.bytebuddy.utility.JavaModule; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Callbacks, which are executed before or after {@link DynamicTransformer#transform}. Currently, | ||
* just used for debugging transformation. | ||
*/ | ||
public class InspectitListener implements AgentBuilder.Listener { | ||
private static final Logger log = LoggerFactory.getLogger(InspectitListener.class); | ||
|
||
@Override | ||
public void onError( | ||
String typeName, | ||
ClassLoader classLoader, | ||
JavaModule module, | ||
boolean loaded, | ||
Throwable throwable) { | ||
log.debug("Dynamic transformation failed for type '{}': {}", typeName, throwable.getMessage()); | ||
} | ||
|
||
@Override | ||
public void onDiscovery( | ||
String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) { | ||
// unused | ||
} | ||
|
||
@Override | ||
public void onTransformation( | ||
TypeDescription typeDescription, | ||
ClassLoader classLoader, | ||
JavaModule module, | ||
boolean loaded, | ||
DynamicType dynamicType) { | ||
// unused | ||
} | ||
|
||
@Override | ||
public void onIgnored( | ||
TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, boolean loaded) { | ||
// unused | ||
} | ||
|
||
@Override | ||
public void onComplete( | ||
String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) { | ||
// unused | ||
} | ||
} |
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
Oops, something went wrong.