Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 0 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@

<!-- OpenTelemetry versions -->
<opentelemetry.version>1.62.0</opentelemetry.version>
<opentelemetry-semconv.version>1.41.1</opentelemetry-semconv.version>
<!-- Test properties -->
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>
<test.exclude.pattern>_</test.exclude.pattern>
Expand Down Expand Up @@ -394,21 +393,11 @@
<artifactId>opentelemetry-api</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>${opentelemetry-semconv.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
Expand Down
9 changes: 1 addition & 8 deletions ratis-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,10 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ratis.trace;

import org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.util.function.CheckedSupplier;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

enum NoOpTraceProvider implements TraceProvider {
INSTANCE;

@Override
public <T, THROWABLE extends Throwable> CompletableFuture<T> traceClientSend(
CheckedSupplier<CompletableFuture<T>, THROWABLE> action,
RaftClientRequest.Type type, RaftPeerId server) throws THROWABLE {
return action.get();
}

@Override
public <T, THROWABLE extends Throwable> CompletableFuture<T> traceServerRequest(
CheckedSupplier<CompletableFuture<T>, THROWABLE> action,
RaftClientRequest request, String memberId, String spanName) throws THROWABLE {
return action.get();
}

@Override
public <T> CompletableFuture<T> traceAppendEntries(
CheckedSupplier<CompletableFuture<T>, IOException> action,
AppendEntriesRequestProto request, String memberId) throws IOException {
return action.get();
}
}
28 changes: 2 additions & 26 deletions ratis-common/src/main/java/org/apache/ratis/trace/TraceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
*/
package org.apache.ratis.trace;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.util.Preconditions;
import org.apache.ratis.util.function.CheckedSupplier;

import java.util.concurrent.CompletableFuture;

/** Client-side OpenTelemetry helpers. */
/** Client-side tracing helpers. */
public final class TraceClient {
private static final String LEADER = "LEADER";

private TraceClient() {
}

Expand All @@ -39,25 +34,6 @@ private TraceClient() {
public static <T, THROWABLE extends Throwable> CompletableFuture<T> asyncSend(
CheckedSupplier<CompletableFuture<T>, THROWABLE> action,
RaftClientRequest.Type type, RaftPeerId server) throws THROWABLE {
if (!TraceUtils.isEnabled()) {
return action.get();
}
return TraceUtils.traceAsyncMethod(action,
() -> createClientOperationSpan(type, server, SpanNames.ASYNC_SEND));
}

private static Span createClientOperationSpan(RaftClientRequest.Type type, RaftPeerId server,
String spanName) {
Preconditions.assertNotNull(spanName, () -> "Span name cannot be null");
Preconditions.assertTrue(!spanName.isEmpty(), "Span name should not be empty");
String peerId = server == null ? LEADER : String.valueOf(server);
final Span span = TraceUtils.getGlobalTracer()
.spanBuilder(spanName)
.setSpanKind(SpanKind.CLIENT)
.startSpan();
span.setAttribute(RatisAttributes.PEER_ID, peerId);
span.setAttribute(RatisAttributes.OPERATION_NAME, spanName);
span.setAttribute(RatisAttributes.OPERATION_TYPE, String.valueOf(type));
return span;
return TraceUtils.getProvider().traceClientSend(action, type, server);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ratis.trace;

import org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.util.function.CheckedSupplier;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

public interface TraceProvider {
<T, THROWABLE extends Throwable> CompletableFuture<T> traceClientSend(
CheckedSupplier<CompletableFuture<T>, THROWABLE> action,
RaftClientRequest.Type type, RaftPeerId server) throws THROWABLE;

<T, THROWABLE extends Throwable> CompletableFuture<T> traceServerRequest(
CheckedSupplier<CompletableFuture<T>, THROWABLE> action,
RaftClientRequest request, String memberId, String spanName) throws THROWABLE;

<T> CompletableFuture<T> traceAppendEntries(
CheckedSupplier<CompletableFuture<T>, IOException> action,
AppendEntriesRequestProto request, String memberId) throws IOException;
}
50 changes: 3 additions & 47 deletions ratis-common/src/main/java/org/apache/ratis/trace/TraceServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@
*/
package org.apache.ratis.trace;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto;
import org.apache.ratis.proto.RaftProtos.RaftRpcRequestProto;
import org.apache.ratis.proto.RaftProtos.SpanContextProto;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.util.function.CheckedSupplier;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/** Server-side OpenTelemetry helpers. */
/** Server-side tracing helpers. */
public final class TraceServer {
private TraceServer() {
}
Expand All @@ -41,25 +35,7 @@ private TraceServer() {
public static <T, THROWABLE extends Throwable> CompletableFuture<T> traceAsyncMethod(
CheckedSupplier<CompletableFuture<T>, THROWABLE> action,
RaftClientRequest request, String memberId, String spanName) throws THROWABLE {
if (!TraceUtils.isEnabled()) {
return action.get();
}
return TraceUtils.traceAsyncMethod(action,
() -> createServerSpanFromClientRequest(request, memberId, spanName));
}

private static Span createServerSpanFromClientRequest(RaftClientRequest request, String memberId,
String spanName) {
final Context remoteContext = TraceUtils.extractContextFromProto(request.getSpanContext());
final Span span = TraceUtils.getGlobalTracer()
.spanBuilder(spanName)
.setParent(remoteContext)
.setSpanKind(SpanKind.SERVER)
.startSpan();
span.setAttribute(RatisAttributes.CLIENT_ID, String.valueOf(request.getClientId()));
span.setAttribute(RatisAttributes.CALL_ID, String.valueOf(request.getCallId()));
span.setAttribute(RatisAttributes.MEMBER_ID, memberId);
return span;
return TraceUtils.getProvider().traceServerRequest(action, request, memberId, spanName);
}

/**
Expand All @@ -69,26 +45,6 @@ private static Span createServerSpanFromClientRequest(RaftClientRequest request,
public static <T> CompletableFuture<T> traceAppendEntriesAsync(
CheckedSupplier<CompletableFuture<T>, IOException> action,
AppendEntriesRequestProto request, String memberId) throws IOException {
if (!TraceUtils.isEnabled()) {
return action.get();
}
final RaftRpcRequestProto rpc = request.getServerRequest();
final SpanContextProto spanContext = rpc.getSpanContext();
// If the leader sent no parent span context, still trace as a root span
// rather than skipping tracing entirely.
final Context remoteContext = (spanContext == null || spanContext.getContextMap().isEmpty())
? Context.root()
: TraceUtils.extractContextFromProto(spanContext);
return TraceUtils.traceAsyncMethod(action, () -> {
final Span span = TraceUtils.getGlobalTracer()
.spanBuilder(SpanNames.APPEND_ENTRIES_ASYNC)
.setParent(remoteContext)
.setSpanKind(SpanKind.INTERNAL)
.startSpan();
span.setAttribute(RatisAttributes.MEMBER_ID, memberId);
span.setAttribute(RatisAttributes.PEER_ID, String.valueOf(RaftPeerId.valueOf(rpc.getRequestorId())));
span.setAttribute(RatisAttributes.APPEND_ENTRIES_COUNT, (long) request.getEntriesCount());
return span;
});
return TraceUtils.getProvider().traceAppendEntries(action, request, memberId);
}
}
Loading
Loading