-
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: prevent span duplicates (#22)
* feature: prevent span duplicates * refactor: sonarcloud issues * prevent duplicate GitHub workflow * revert last commit * refactor: add null-check * refactor: replace exception with return value * test: add util tests * fix test * refactor: use ReadableSpan instead of SdkSpan * fix gradle group * apply spotless * exclude our classes from transformation * push docker images for every branch * update workflow * try fix workflow * fix image tag format * apply requested changes
- Loading branch information
Showing
9 changed files
with
237 additions
and
151 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
38 changes: 18 additions & 20 deletions
38
...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
27 changes: 27 additions & 0 deletions
27
...src/main/java/rocks/inspectit/gepard/agent/instrumentation/hook/action/util/SpanUtil.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.action.util; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
|
||
/** Util class to access span data, like the name or attributes. */ | ||
public class SpanUtil { | ||
|
||
private SpanUtil() {} | ||
|
||
/** | ||
* Checks, if the current span uses the provided span name. This check might be necessary to | ||
* prevent span duplicates. For example, we would like to create a span for a method, which is | ||
* already recorded by OpenTelemetry. In this case, we should not create a new span. | ||
* | ||
* @param spanName the name of the span with the format 'SimpleClassName.methodName', for instance | ||
* 'SpanUtil.spanAlreadyExists' | ||
* @return true, if the current span uses the provided span name | ||
*/ | ||
public static boolean spanAlreadyExists(String spanName) { | ||
Span span = Span.current(); | ||
|
||
if (span instanceof ReadableSpan readableSpan) return spanName.equals(readableSpan.getName()); | ||
return false; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...test/java/rocks/inspectit/gepard/agent/instrumentation/hook/action/util/SpanUtilTest.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,61 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.instrumentation.hook.action.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SpanUtilTest { | ||
|
||
private Tracer tracer; | ||
|
||
private final String spanName = "SpanUtilTest.method"; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
GlobalOpenTelemetry.resetForTest(); | ||
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder().buildAndRegisterGlobal(); | ||
tracer = openTelemetry.getTracer("inspectit-gepard"); | ||
} | ||
|
||
@Test | ||
void shouldReturnTrueWhenSpanExists() throws Exception { | ||
Span span = tracer.spanBuilder(spanName).startSpan(); | ||
Scope scope = span.makeCurrent(); | ||
|
||
boolean exists = SpanUtil.spanAlreadyExists(spanName); | ||
|
||
assertTrue(exists); | ||
|
||
scope.close(); | ||
span.end(); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenNoSpanExists() throws Exception { | ||
boolean exists = SpanUtil.spanAlreadyExists(spanName); | ||
|
||
assertFalse(exists); | ||
} | ||
|
||
@Test | ||
void shouldReturnFalseWhenOtherSpanExists() throws Exception { | ||
Span span = tracer.spanBuilder("dummy").startSpan(); | ||
Scope scope = span.makeCurrent(); | ||
|
||
boolean exists = SpanUtil.spanAlreadyExists(spanName); | ||
|
||
assertFalse(exists); | ||
|
||
scope.close(); | ||
span.end(); | ||
} | ||
} |
Oops, something went wrong.