-
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: record custom spans with action (wip)
- Loading branch information
Showing
15 changed files
with
241 additions
and
101 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
8 changes: 0 additions & 8 deletions
8
...src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/action/EndSpanAction.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...nt/src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/action/HookAction.java
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
...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,53 @@ | ||
/* (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 created span | ||
* @return the scope of the started span | ||
*/ | ||
public AutoCloseable startSpan(String spanName) { | ||
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(); | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
...c/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/action/StartSpanAction.java
This file was deleted.
Oops, something went wrong.
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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...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,31 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.otel; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Singleton to access the {@link OpenTelemetry} instance. We use this accessor, because according | ||
* to the documentation of {@link GlobalOpenTelemetry}, the method should only be called once during | ||
* the application. | ||
*/ | ||
public class OpenTelemetryAccessor { | ||
|
||
private static OpenTelemetry openTelemetry; | ||
|
||
private OpenTelemetryAccessor() {} | ||
|
||
/** | ||
* Get the global {@link OpenTelemetry} instance. The global instance should have been set by the | ||
* OpenTelemetry Java agent beforehand. | ||
* | ||
* @return the global {@link OpenTelemetry} instance | ||
*/ | ||
public static OpenTelemetry getOpenTelemetry() { | ||
if (Objects.isNull(openTelemetry)) { | ||
openTelemetry = GlobalOpenTelemetry.get(); | ||
} | ||
return openTelemetry; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...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,50 @@ | ||
/* (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) {} | ||
|
||
@Override | ||
public void onTransformation( | ||
TypeDescription typeDescription, | ||
ClassLoader classLoader, | ||
JavaModule module, | ||
boolean loaded, | ||
DynamicType dynamicType) {} | ||
|
||
@Override | ||
public void onIgnored( | ||
TypeDescription typeDescription, | ||
ClassLoader classLoader, | ||
JavaModule module, | ||
boolean loaded) {} | ||
|
||
@Override | ||
public void onComplete( | ||
String typeName, ClassLoader classLoader, JavaModule module, boolean loaded) {} | ||
} |
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
Oops, something went wrong.