Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapped tracer implementation #8565

Merged
merged 6 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Pass localNode info to all plugins on node start ([#7919](https://github.com/opensearch-project/OpenSearch/pull/7919))
- Improved performance of parsing floating point numbers ([#7909](https://github.com/opensearch-project/OpenSearch/pull/7909))
- Move span actions to Scope ([#8411](https://github.com/opensearch-project/OpenSearch/pull/8411))
- Add wrapper tracer implementation
suranjay marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* TracerManager represents a single global class that is used to access tracers.
*
* <p>
* The Tracer singleton object can be retrieved using tracerManager.getTracer(). The TracerManager object
* is created during class initialization and cannot subsequently be changed.
*/
reta marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -30,21 +30,20 @@ public class TracerFactory implements Closeable {
private static final Logger logger = LogManager.getLogger(TracerFactory.class);

private final TelemetrySettings telemetrySettings;
private final Tracer defaultTracer;
private final Tracer tracer;

public TracerFactory(TelemetrySettings telemetrySettings, Optional<Telemetry> telemetry, ThreadContext threadContext) {
reta marked this conversation as resolved.
Show resolved Hide resolved
this.telemetrySettings = telemetrySettings;
this.defaultTracer = telemetry.map(Telemetry::getTracingTelemetry)
.map(tracingTelemetry -> createDefaultTracer(tracingTelemetry, threadContext))
.orElse(NoopTracer.INSTANCE);
this.tracer = tracer(telemetry, threadContext);
}

/**
* Returns the tracer instance
*
* @return tracer instance
*/
public Tracer getTracer() {
return telemetrySettings.isTracingEnabled() ? defaultTracer : NoopTracer.INSTANCE;
return tracer;
}

/**
Expand All @@ -53,12 +52,19 @@ public Tracer getTracer() {
@Override
public void close() {
try {
defaultTracer.close();
tracer.close();
} catch (IOException e) {
logger.warn("Error closing tracer", e);
}
}

private Tracer tracer(Optional<Telemetry> telemetry, ThreadContext threadContext) {
return telemetry.map(Telemetry::getTracingTelemetry)
.map(tracingTelemetry -> createDefaultTracer(tracingTelemetry, threadContext))
.map(defaultTracer -> createWrappedTracer(defaultTracer))
.orElse(NoopTracer.INSTANCE);
}

private Tracer createDefaultTracer(TracingTelemetry tracingTelemetry, ThreadContext threadContext) {
TracerContextStorage<String, Span> tracerContextStorage = new ThreadContextBasedTracerContextStorage(
threadContext,
Expand All @@ -67,4 +73,8 @@ private Tracer createDefaultTracer(TracingTelemetry tracingTelemetry, ThreadCont
return new DefaultTracer(tracingTelemetry, tracerContextStorage);
}

private Tracer createWrappedTracer(Tracer defaultTracer) {
return new WrappedTracer(telemetrySettings, defaultTracer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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
*/
public class WrappedTracer implements Tracer {
reta marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,28 @@ public void close() {
tracerFactory.close();
}

public void testGetTracerWithTracingDisabledReturnsNoopTracer() {
public void testGetTracerWithUnavailableTracingTelemetryReturnsNoopTracer() {
Settings settings = Settings.builder().put(TelemetrySettings.TRACER_ENABLED_SETTING.getKey(), false).build();
TelemetrySettings telemetrySettings = new TelemetrySettings(settings, new ClusterSettings(settings, getClusterSettings()));
Telemetry mockTelemetry = mock(Telemetry.class);
when(mockTelemetry.getTracingTelemetry()).thenReturn(mock(TracingTelemetry.class));
tracerFactory = new TracerFactory(telemetrySettings, Optional.of(mockTelemetry), new ThreadContext(Settings.EMPTY));
tracerFactory = new TracerFactory(telemetrySettings, Optional.empty(), new ThreadContext(Settings.EMPTY));

Tracer tracer = tracerFactory.getTracer();

assertTrue(tracer instanceof NoopTracer);
assertTrue(tracer.startSpan("foo") == SpanScope.NO_OP);
}

public void testGetTracerWithTracingEnabledReturnsDefaultTracer() {
public void testGetTracerWithAvailableTracingTelemetryReturnsWrappedTracer() {
Settings settings = Settings.builder().put(TelemetrySettings.TRACER_ENABLED_SETTING.getKey(), true).build();
TelemetrySettings telemetrySettings = new TelemetrySettings(settings, new ClusterSettings(settings, getClusterSettings()));
Telemetry mockTelemetry = mock(Telemetry.class);
when(mockTelemetry.getTracingTelemetry()).thenReturn(mock(TracingTelemetry.class));
tracerFactory = new TracerFactory(telemetrySettings, Optional.of(mockTelemetry), new ThreadContext(Settings.EMPTY));

Tracer tracer = tracerFactory.getTracer();
assertTrue(tracer instanceof DefaultTracer);
assertTrue(tracer instanceof WrappedTracer);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.spy;
import static org.mockito.Mockito.verify;

public class WrappedTracerTests extends OpenSearchTestCase {

public void testStartSpanWithTracingDisabledInvokesNoopTracer() {
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);

WrappedTracer wrappedTracer = spy(new WrappedTracer(telemetrySettings, mockDefaultTracer));
suranjay marked this conversation as resolved.
Show resolved Hide resolved

wrappedTracer.startSpan("foo");

assertTrue(wrappedTracer.getDelegateTracer() instanceof NoopTracer);
verify(mockDefaultTracer, never()).startSpan("foo");
}

public void testStartSpanWithTracingEnabledInvokesDefaultTracer() {
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);

WrappedTracer wrappedTracer = spy(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 = spy(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;
}
}