|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making tRPC available. |
| 3 | + * |
| 4 | + * Copyright (C) 2023 Tencent. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * If you have downloaded a copy of the tRPC source code from Tencent, |
| 8 | + * please note that tRPC source code is licensed under the Apache 2.0 License, |
| 9 | + * A copy of the Apache 2.0 License can be found in the LICENSE file. |
| 10 | + */ |
| 11 | + |
| 12 | +package com.tencent.trpc.core.trace; |
| 13 | + |
| 14 | +import com.tencent.trpc.core.extension.ExtensionLoader; |
| 15 | +import com.tencent.trpc.core.logger.Logger; |
| 16 | +import com.tencent.trpc.core.logger.LoggerFactory; |
| 17 | +import com.tencent.trpc.core.rpc.Invoker; |
| 18 | +import com.tencent.trpc.core.rpc.Request; |
| 19 | +import com.tencent.trpc.core.rpc.Response; |
| 20 | +import com.tencent.trpc.core.rpc.RpcClientContext; |
| 21 | +import com.tencent.trpc.core.rpc.RpcContext; |
| 22 | +import com.tencent.trpc.core.rpc.RpcContextValueKeys; |
| 23 | +import com.tencent.trpc.core.rpc.RpcInvocation; |
| 24 | +import com.tencent.trpc.core.rpc.def.DefRequest; |
| 25 | +import com.tencent.trpc.core.rpc.def.DefResponse; |
| 26 | +import com.tencent.trpc.core.trace.spi.TracerFactory; |
| 27 | +import com.tencent.trpc.core.utils.RpcContextUtils; |
| 28 | +import io.opentracing.Span; |
| 29 | +import io.opentracing.SpanContext; |
| 30 | +import io.opentracing.Tracer; |
| 31 | +import io.opentracing.Tracer.SpanBuilder; |
| 32 | +import io.opentracing.noop.NoopSpan; |
| 33 | +import io.opentracing.noop.NoopTracerFactory; |
| 34 | +import java.net.InetSocketAddress; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | +import org.junit.After; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.powermock.api.mockito.PowerMockito; |
| 43 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 44 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 45 | + |
| 46 | +@RunWith(PowerMockRunner.class) |
| 47 | +@PowerMockIgnore({"javax.management.*"}) |
| 48 | +public class TracerClientFilterTest { |
| 49 | + |
| 50 | + private TracerClientFilter filter = new TracerClientFilter() { |
| 51 | + |
| 52 | + private final Logger logger = LoggerFactory.getLogger(TracerClientFilter.class); |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getPluginName() { |
| 56 | + return "tjg"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Span start(SpanBuilder spanBuilder, RpcContext context, Invoker<?> invoker, Request request) { |
| 61 | + if (spanBuilder == null || request == null || request.getMeta() == null) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + return spanBuilder.start(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void finish(Span span, Request request, Response response, Throwable t) { |
| 69 | + if (span == null) { |
| 70 | + return; |
| 71 | + } |
| 72 | + span.finish(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public SpanContext extract(Tracer tracer, Map<String, Object> attachments) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Map<String, Object> inject(Tracer tracer, Span span) { |
| 82 | + if (logger.isDebugEnabled()) { |
| 83 | + logger.debug("inject tracer:{},span:{}", tracer, span); |
| 84 | + } |
| 85 | + return new HashMap<>(); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + @Before |
| 90 | + public void before() { |
| 91 | + TracerFactoryManager.getManager().registPlugin("mtest", TestTraceFactory.class); |
| 92 | + } |
| 93 | + |
| 94 | + @After |
| 95 | + public void after() { |
| 96 | + ExtensionLoader.destroyAllPlugin(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testNormalSpan() { |
| 101 | + RpcClientContext context = new RpcClientContext(); |
| 102 | + RpcContextUtils.putValueMapValue(context, RpcContextValueKeys.CTX_TRACE_SPAN, NoopSpan.INSTANCE); |
| 103 | + RpcContextUtils.putValueMapValue(context, RpcContextValueKeys.CTX_TRACER, NoopTracerFactory.create()); |
| 104 | + Request request = new DefRequest(); |
| 105 | + request.setContext(context); |
| 106 | + RpcInvocation invocation = new RpcInvocation(); |
| 107 | + invocation.setRpcServiceName("rpcServiceName"); |
| 108 | + invocation.setRpcMethodName("rpcMethodName"); |
| 109 | + request.getMeta().setRemoteAddress(InetSocketAddress.createUnresolved("127.0.0.1", 8888)); |
| 110 | + request.getMeta().setLocalAddress(InetSocketAddress.createUnresolved("127.0.0.1", 9999)); |
| 111 | + request.setInvocation(invocation); |
| 112 | + CompletableFuture<Response> future = new CompletableFuture<Response>(); |
| 113 | + Response rsp = new DefResponse(); |
| 114 | + future.complete(rsp); |
| 115 | + Invoker<?> invoker = (Invoker<?>) PowerMockito.mock(Invoker.class); |
| 116 | + PowerMockito.when(invoker.invoke(request)).thenReturn(future); |
| 117 | + filter.filter(invoker, request); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testDebugLog() { |
| 122 | + RpcClientContext context = new RpcClientContext(); |
| 123 | + Request request = new DefRequest(); |
| 124 | + request.setContext(context); |
| 125 | + RpcInvocation invocation = new RpcInvocation(); |
| 126 | + invocation.setRpcServiceName("rpcServiceName"); |
| 127 | + invocation.setRpcMethodName("rpcMethodName"); |
| 128 | + request.getMeta().setRemoteAddress(InetSocketAddress.createUnresolved("127.0.0.1", 8888)); |
| 129 | + request.getMeta().setLocalAddress(InetSocketAddress.createUnresolved("127.0.0.1", 9999)); |
| 130 | + request.setInvocation(invocation); |
| 131 | + CompletableFuture<Response> future = new CompletableFuture<Response>(); |
| 132 | + Response rsp = new DefResponse(); |
| 133 | + future.complete(rsp); |
| 134 | + Invoker<?> invoker = (Invoker<?>) PowerMockito.mock(Invoker.class); |
| 135 | + PowerMockito.when(invoker.invoke(request)).thenReturn(future); |
| 136 | + filter.filter(invoker, request); |
| 137 | + } |
| 138 | + |
| 139 | + private static class TestTraceFactory implements TracerFactory { |
| 140 | + |
| 141 | + @Override |
| 142 | + public Tracer getTracer(String serverName, Integer port) { |
| 143 | + return NoopTracerFactory.create(); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments