Skip to content

Commit

Permalink
HBASE-25481 Add host and port attribute when tracing rpc call at clie…
Browse files Browse the repository at this point in the history
…nt side (#2857)

Signed-off-by: Guanghao Zhang <zghao@apache.org>
  • Loading branch information
Apache9 committed Apr 9, 2021
1 parent 05863d6 commit 06642ab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ private Call callMethod(final Descriptors.MethodDescriptor md, final HBaseRpcCon
final RpcCallback<Message> callback) {
Span span = TraceUtil.createSpan("RpcClient.callMethod")
.setAttribute(TraceUtil.RPC_SERVICE_KEY, md.getService().getName())
.setAttribute(TraceUtil.RPC_METHOD_KEY, md.getName());
.setAttribute(TraceUtil.RPC_METHOD_KEY, md.getName())
.setAttribute(TraceUtil.REMOTE_HOST_KEY, addr.getHostName())
.setAttribute(TraceUtil.REMOTE_PORT_KEY, addr.getPort());
try (Scope scope = span.makeCurrent()) {
final MetricsConnection.CallStats cs = MetricsConnection.newCallStats();
cs.setStartTime(EnvironmentEdgeManager.currentTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public final class TraceUtil {
public static final AttributeKey<String> SERVER_NAME_KEY =
AttributeKey.stringKey("db.hbase.server.name");

public static final AttributeKey<String> REMOTE_HOST_KEY = SemanticAttributes.NET_PEER_NAME;

public static final AttributeKey<Long> REMOTE_PORT_KEY = SemanticAttributes.NET_PEER_PORT;

private TraceUtil() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,14 @@ private SpanData waitSpan(String name) {
return traceRule.getSpans().stream().filter(s -> s.getName().equals(name)).findFirst().get();
}

private void assertRpcAttribute(SpanData data, String methodName) {
private void assertRpcAttribute(SpanData data, String methodName, InetSocketAddress addr) {
assertEquals(SERVICE.getDescriptorForType().getName(),
data.getAttributes().get(TraceUtil.RPC_SERVICE_KEY));
assertEquals(methodName,
data.getAttributes().get(TraceUtil.RPC_METHOD_KEY));
assertEquals(methodName, data.getAttributes().get(TraceUtil.RPC_METHOD_KEY));
if (addr != null) {
assertEquals(addr.getHostName(), data.getAttributes().get(TraceUtil.REMOTE_HOST_KEY));
assertEquals(addr.getPort(), data.getAttributes().get(TraceUtil.REMOTE_PORT_KEY).intValue());
}
}

@Test
Expand All @@ -471,8 +474,8 @@ public void testTracing() throws IOException, ServiceException {
rpcServer.start();
BlockingInterface stub = newBlockingStub(client, rpcServer.getListenerAddress());
stub.pause(null, PauseRequestProto.newBuilder().setMs(100).build());
assertRpcAttribute(waitSpan("RpcClient.callMethod"), "pause");
assertRpcAttribute(waitSpan("RpcServer.callMethod"), "pause");
assertRpcAttribute(waitSpan("RpcClient.callMethod"), "pause", rpcServer.getListenerAddress());
assertRpcAttribute(waitSpan("RpcServer.callMethod"), "pause", null);
assertSameTraceId();
for (SpanData data : traceRule.getSpans()) {
assertThat(
Expand All @@ -484,8 +487,8 @@ public void testTracing() throws IOException, ServiceException {
traceRule.clearSpans();
assertThrows(ServiceException.class,
() -> stub.error(null, EmptyRequestProto.getDefaultInstance()));
assertRpcAttribute(waitSpan("RpcClient.callMethod"), "error");
assertRpcAttribute(waitSpan("RpcServer.callMethod"), "error");
assertRpcAttribute(waitSpan("RpcClient.callMethod"), "error", rpcServer.getListenerAddress());
assertRpcAttribute(waitSpan("RpcServer.callMethod"), "error", null);
assertSameTraceId();
for (SpanData data : traceRule.getSpans()) {
assertEquals(StatusCode.ERROR, data.getStatus().getStatusCode());
Expand Down

0 comments on commit 06642ab

Please sign in to comment.