RATIS-2553. Isolate OpenTelemetry tracing dependencies#1478
Conversation
|
@HTHou , We do plan to make tracing configurable; see #1341 (comment) Thanks a lot for starting the work! Will review this soon. Cc @taklwu |
| private static TraceProvider tryNewOpenTelemetryTraceProvider() { | ||
| try { | ||
| return new OpenTelemetryTraceProvider(); | ||
| } catch (LinkageError e) { | ||
| LOG.warn("OpenTelemetry tracing is enabled but OpenTelemetry is not available; tracing is disabled", e); |
There was a problem hiding this comment.
Let's throw an exception instead of fallback:
private static TraceProvider newOpenTelemetryTraceProvider() {
try {
return new OpenTelemetryTraceProvider();
} catch (Throwable e) {
throw new IllegalStateException("OpenTelemetry tracing is enabled but OpenTelemetry is not available; tracing is disabled", e);
}
}| public static Tracer getGlobalTracer() { | ||
| return GlobalOpenTelemetry.getTracer("org.apache.ratis", VersionInfo.getSoftwareInfoVersion()); | ||
| } |
There was a problem hiding this comment.
Let's remove it since it is only used once.
| public static boolean isEnabled() { | ||
| return TRACER.get() != null; | ||
| return getProvider().isEnabled(); | ||
| } |
There was a problem hiding this comment.
Let's check the provider class:
public static boolean isEnabled() {
return !(getProvider() instanceof NoOpTraceProvider);
}| private static final Logger LOG = LoggerFactory.getLogger(OpenTelemetryTraceProvider.class); | ||
| private static final String LEADER = "LEADER"; | ||
|
|
||
| private final Tracer tracer = OpenTelemetryTraceUtils.getGlobalTracer(); |
There was a problem hiding this comment.
Check null for tracer:
private final Tracer tracer = Objects.requireNonNull(
GlobalOpenTelemetry.getTracer("org.apache.ratis", VersionInfo.getSoftwareInfoVersion()),
"tracer == null");| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| interface TraceProvider { | ||
| boolean isEnabled(); |
| @SuppressWarnings("FutureReturnValueIgnored") | ||
| private static <T> void addListener(CompletableFuture<T> future, |
There was a problem hiding this comment.
Let's keep the javadoc:
/**
* This is method is used when you just want to add a listener to the given future. We will call
* {@link CompletableFuture#whenComplete(BiConsumer)} to register the {@code action} to the
* {@code future}. Ignoring the return value of a Future is considered as a bad practice as it may
* suppress exceptions thrown from the code that completes the future, and this method will catch
* all the exception thrown from the {@code action} to catch possible code bugs.
* <p/>
* And the error phone check will always report FutureReturnValueIgnored because every method in
* the {@link CompletableFuture} class will return a new {@link CompletableFuture}, so you always
* have one future that has not been checked. So we introduce this method and add a suppression
* warnings annotation here.
*/
@SuppressWarnings("FutureReturnValueIgnored")
private static <T> void addListener(CompletableFuture<T> future,| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.ratis.trace; |
There was a problem hiding this comment.
Move it to a new package:
package org.apache.ratis.trace.opentelemetry;| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.ratis.trace; |
There was a problem hiding this comment.
Move it to a new package:
package org.apache.ratis.trace.opentelemetry;| private static final TextMapPropagator PROPAGATOR = | ||
| GlobalOpenTelemetry.getPropagators().getTextMapPropagator(); |
There was a problem hiding this comment.
Add it back to OpenTelemetryTraceUtils and use it.
|
Updated the PR to address the inline review comments:
Also rebased on the latest Verified with:
|
szetszwo
left a comment
There was a problem hiding this comment.
+1 the change looks good.
What changes were proposed in this pull request?
This patch isolates OpenTelemetry-specific tracing code behind a TraceProvider facade. The default tracing path uses a no-op provider, so downstream projects can exclude OpenTelemetry jars without loading OTel classes from the default Ratis tracing path.
OpenTelemetry tracing behavior is preserved when tracing is enabled and the OpenTelemetry jars are available. The patch also removes OpenTelemetry SDK and semantic convention dependencies from the main dependency path and keeps SDK testing dependencies in test scope.
What is the link to the Apache JIRA
https://issues.apache.org/jira/browse/RATIS-2553
How was this patch tested?
./mvnw -pl ratis-common,ratis-server,ratis-test -am test-compile -DskipTests./mvnw -pl ratis-server -am -Dtest=RaftServerImplTracingTests -DfailIfNoTests=false test./mvnw -pl ratis-server -am dependency:tree -Dscope=compile -Dincludes=io.opentelemetry,io.opentelemetry.semconv -DskipTestsTraceUtils.setTracerWhenEnabled(true)falls back to no-op tracing instead of failing with linkage errors.