forked from opensearch-project/OpenSearch
-
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.
Add wrapped tracer implementation (opensearch-project#8565)
* Add wrapped tracer implementation Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Add changelog entry Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Add @opensearch.internal annotation Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Fix test Signed-off-by: suranjay <surajkumar.tu@gmail.com> * Fix changelog entry Signed-off-by: suranjay <surajkumar.tu@gmail.com> --------- Signed-off-by: suranjay <surajkumar.tu@gmail.com>
- Loading branch information
1 parent
3d35388
commit de369e5
Showing
18 changed files
with
175 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
|
||
/** | ||
* Interface defining telemetry | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public interface Telemetry { | ||
|
||
|
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
/** | ||
* Base span | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public abstract class AbstractSpan implements 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
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
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
52 changes: 52 additions & 0 deletions
52
server/src/main/java/org/opensearch/telemetry/tracing/WrappedTracer.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,52 @@ | ||
/* | ||
* 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 org.opensearch.telemetry.TelemetrySettings; | ||
import org.opensearch.telemetry.tracing.noop.NoopTracer; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Wrapper implementation of Tracer. This delegates call to right tracer based on the tracer settings | ||
* | ||
* @opensearch.internal | ||
*/ | ||
final class WrappedTracer implements Tracer { | ||
|
||
private final Tracer defaultTracer; | ||
private final TelemetrySettings telemetrySettings; | ||
|
||
/** | ||
* Creates WrappedTracer instance | ||
* | ||
* @param telemetrySettings telemetry settings | ||
* @param defaultTracer default tracer instance | ||
*/ | ||
public WrappedTracer(TelemetrySettings telemetrySettings, Tracer defaultTracer) { | ||
this.defaultTracer = defaultTracer; | ||
this.telemetrySettings = telemetrySettings; | ||
} | ||
|
||
@Override | ||
public SpanScope startSpan(String spanName) { | ||
Tracer delegateTracer = getDelegateTracer(); | ||
return delegateTracer.startSpan(spanName); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
defaultTracer.close(); | ||
} | ||
|
||
// visible for testing | ||
Tracer getDelegateTracer() { | ||
return telemetrySettings.isTracingEnabled() ? defaultTracer : NoopTracer.INSTANCE; | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
server/src/test/java/org/opensearch/telemetry/tracing/WrappedTracerTests.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,69 @@ | ||
/* | ||
* 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 org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.common.settings.Setting; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.FeatureFlags; | ||
import org.opensearch.telemetry.TelemetrySettings; | ||
import org.opensearch.telemetry.tracing.noop.NoopTracer; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class WrappedTracerTests extends OpenSearchTestCase { | ||
|
||
public void testStartSpanWithTracingDisabledInvokesNoopTracer() throws Exception { | ||
Settings settings = Settings.builder().put(TelemetrySettings.TRACER_ENABLED_SETTING.getKey(), false).build(); | ||
TelemetrySettings telemetrySettings = new TelemetrySettings(settings, new ClusterSettings(settings, getClusterSettings())); | ||
DefaultTracer mockDefaultTracer = mock(DefaultTracer.class); | ||
|
||
try (WrappedTracer wrappedTracer = new WrappedTracer(telemetrySettings, mockDefaultTracer)) { | ||
wrappedTracer.startSpan("foo"); | ||
assertTrue(wrappedTracer.getDelegateTracer() instanceof NoopTracer); | ||
verify(mockDefaultTracer, never()).startSpan("foo"); | ||
} | ||
} | ||
|
||
public void testStartSpanWithTracingEnabledInvokesDefaultTracer() throws Exception { | ||
Settings settings = Settings.builder().put(TelemetrySettings.TRACER_ENABLED_SETTING.getKey(), true).build(); | ||
TelemetrySettings telemetrySettings = new TelemetrySettings(settings, new ClusterSettings(settings, getClusterSettings())); | ||
DefaultTracer mockDefaultTracer = mock(DefaultTracer.class); | ||
|
||
try (WrappedTracer wrappedTracer = new WrappedTracer(telemetrySettings, mockDefaultTracer)) { | ||
wrappedTracer.startSpan("foo"); | ||
|
||
assertTrue(wrappedTracer.getDelegateTracer() instanceof DefaultTracer); | ||
verify(mockDefaultTracer).startSpan("foo"); | ||
} | ||
} | ||
|
||
public void testClose() throws IOException { | ||
DefaultTracer mockDefaultTracer = mock(DefaultTracer.class); | ||
WrappedTracer wrappedTracer = new WrappedTracer(null, mockDefaultTracer); | ||
|
||
wrappedTracer.close(); | ||
|
||
verify(mockDefaultTracer).close(); | ||
} | ||
|
||
private Set<Setting<?>> getClusterSettings() { | ||
Set<Setting<?>> allTracerSettings = new HashSet<>(); | ||
ClusterSettings.FEATURE_FLAGGED_CLUSTER_SETTINGS.get(List.of(FeatureFlags.TELEMETRY)).stream().forEach((allTracerSettings::add)); | ||
return allTracerSettings; | ||
} | ||
} |