-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for tracing runnable scenarios (#9187)
Signed-off-by: Gagan Juneja <gjjuneja@amazon.com>
- Loading branch information
1 parent
ab8e306
commit c3a0784
Showing
11 changed files
with
234 additions
and
6 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
29 changes: 29 additions & 0 deletions
29
libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/SpanContext.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,29 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.telemetry.tracing; | ||
|
||
/** | ||
* Wrapped Span will be exposed to the code outside of tracing package for sharing the {@link Span} without having access to | ||
* its properties. | ||
*/ | ||
public final class SpanContext { | ||
private final Span span; | ||
|
||
/** | ||
* Constructor. | ||
* @param span span to be wrapped. | ||
*/ | ||
public SpanContext(Span span) { | ||
this.span = span; | ||
} | ||
|
||
Span getSpan() { | ||
return span; | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
.../telemetry/src/main/java/org/opensearch/telemetry/tracing/runnable/TraceableRunnable.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,44 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.telemetry.tracing.runnable; | ||
|
||
import org.opensearch.telemetry.tracing.SpanContext; | ||
import org.opensearch.telemetry.tracing.SpanScope; | ||
import org.opensearch.telemetry.tracing.Tracer; | ||
|
||
/** | ||
* Wraps the runnable and add instrumentation to trace the {@link Runnable} | ||
*/ | ||
public class TraceableRunnable implements Runnable { | ||
private final Runnable runnable; | ||
private final SpanContext parent; | ||
private final Tracer tracer; | ||
private final String spanName; | ||
|
||
/** | ||
* Constructor. | ||
* @param tracer tracer | ||
* @param spanName spanName | ||
* @param parent parent Span. | ||
* @param runnable runnable. | ||
*/ | ||
public TraceableRunnable(Tracer tracer, String spanName, SpanContext parent, Runnable runnable) { | ||
this.tracer = tracer; | ||
this.spanName = spanName; | ||
this.parent = parent; | ||
this.runnable = runnable; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try (SpanScope spanScope = tracer.startSpan(spanName, parent)) { | ||
runnable.run(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
libs/telemetry/src/main/java/org/opensearch/telemetry/tracing/runnable/package-info.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 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/** | ||
* Contains tracing related classes | ||
*/ | ||
package org.opensearch.telemetry.tracing.runnable; |
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
60 changes: 60 additions & 0 deletions
60
libs/telemetry/src/test/java/org/opensearch/telemetry/tracing/TraceableRunnableTests.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,60 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.telemetry.tracing; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.telemetry.tracing.runnable.TraceableRunnable; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.test.telemetry.tracing.MockTracingTelemetry; | ||
|
||
public class TraceableRunnableTests extends OpenSearchTestCase { | ||
|
||
private final ThreadContextBasedTracerContextStorage contextStorage = new ThreadContextBasedTracerContextStorage( | ||
new ThreadContext(Settings.EMPTY), | ||
new MockTracingTelemetry() | ||
); | ||
|
||
public void testRunnableWithNullParent() throws Exception { | ||
String spanName = "testRunnable"; | ||
DefaultTracer defaultTracer = new DefaultTracer(new MockTracingTelemetry(), contextStorage); | ||
final AtomicBoolean isRunnableCompleted = new AtomicBoolean(false); | ||
TraceableRunnable traceableRunnable = new TraceableRunnable( | ||
defaultTracer, | ||
spanName, | ||
null, | ||
() -> { isRunnableCompleted.set(true); } | ||
); | ||
traceableRunnable.run(); | ||
assertTrue(isRunnableCompleted.get()); | ||
assertEquals(spanName, defaultTracer.getCurrentSpan().getSpan().getSpanName()); | ||
assertEquals(null, defaultTracer.getCurrentSpan().getSpan().getParentSpan()); | ||
} | ||
|
||
public void testRunnableWithParent() throws Exception { | ||
String spanName = "testRunnable"; | ||
String parentSpanName = "parentSpan"; | ||
DefaultTracer defaultTracer = new DefaultTracer(new MockTracingTelemetry(), contextStorage); | ||
defaultTracer.startSpan(parentSpanName); | ||
SpanContext parentSpan = defaultTracer.getCurrentSpan(); | ||
AtomicReference<SpanContext> currrntSpan = new AtomicReference<>(new SpanContext(null)); | ||
final AtomicBoolean isRunnableCompleted = new AtomicBoolean(false); | ||
TraceableRunnable traceableRunnable = new TraceableRunnable(defaultTracer, spanName, parentSpan, () -> { | ||
isRunnableCompleted.set(true); | ||
currrntSpan.set(defaultTracer.getCurrentSpan()); | ||
}); | ||
traceableRunnable.run(); | ||
assertTrue(isRunnableCompleted.get()); | ||
assertEquals(spanName, currrntSpan.get().getSpan().getSpanName()); | ||
assertEquals(parentSpan.getSpan(), currrntSpan.get().getSpan().getParentSpan()); | ||
assertEquals(parentSpan.getSpan(), defaultTracer.getCurrentSpan().getSpan()); | ||
} | ||
} |
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