use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class TripleHessianInvokeTest method testInvoke.
@Test
public void testInvoke() throws InterruptedException {
RpcRunningState.setDebugMode(true);
ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
int port = getPort();
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
TripleHessianInterface helloService = consumerConfig.refer();
LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
helloService.call();
Assert.assertEquals("call", ref.getFlag());
// test Pressure Mark
boolean isLoadTest = helloService.testPressureMark("name");
Assert.assertFalse(isLoadTest);
SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
Map<String, String> bizBaggage = currentSpan.getSofaTracerSpanContext().getBizBaggage();
bizBaggage.put("mark", "T");
Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
isLoadTest = helloService.testPressureMark("name");
Assert.assertTrue(isLoadTest);
String s = helloService.call1();
Assert.assertEquals("call1", ref.getFlag());
Assert.assertEquals("call1", s);
Request request = new Request();
int age = RandomUtils.nextInt();
request.setAge(age);
String call2 = "call2";
request.setFlag(call2);
Response response = helloService.call2(request);
Assert.assertEquals(age, response.getAge());
Assert.assertEquals(call2, response.getFlag());
Response response1 = helloService.call2(null);
Assert.assertNull(response1);
providerConfig.unExport();
serverConfig.destroy();
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class TripleHessianInvokeTest method before.
@Before
public void before() {
SofaTracerSpan span = (SofaTracerSpan) tracer.buildSpan("test").start();
SofaTraceContextHolder.getSofaTraceContext().push(span);
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RestTracerAdapter method serverSend.
/**
* 适配服务端serverSend
*/
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
try {
SofaRequest sofaRequest = new SofaRequest();
SofaResponse sofaResponse = new SofaResponse();
if (response == null) {
sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
} else if (throwable != null) {
if (response.getStatus() == 500) {
sofaResponse.setAppResponse(throwable);
} else {
sofaResponse.setErrorMsg(throwable.getMessage());
}
Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
if (method != null) {
Class[] parameterTypes = ((Method) method).getParameterTypes();
String[] methodTypeString = new String[parameterTypes.length];
for (int i = 0; i < methodTypeString.length; i++) {
methodTypeString[i] = (parameterTypes[i].getName());
}
sofaRequest.setMethodArgSigs(methodTypeString);
}
}
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
RpcInternalContext context = RpcInternalContext.getContext();
if (serverSpan != null) {
serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
}
RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
Tracers.serverSend(sofaRequest, sofaResponse, null);
} catch (Throwable t) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("the process of rest tracer server send occur error ", t);
}
}
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RestTracerAdapter method beforeSend.
/**
* 存入tracer信息
*
* @param requestContext ClientRequestContext
*/
public static void beforeSend(ClientRequestContext requestContext) {
// tracer信息放入request 发到服务端
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
RpcInternalContext context = RpcInternalContext.getContext();
if (clientSpan != null) {
requestContext.getHeaders().add(RemotingConstants.NEW_RPC_TRACE_NAME, clientSpan.getSofaTracerSpanContext().serializeSpanContext());
}
// 客户端发送自己的应用名
String appName = (String) context.getAttachment(INTERNAL_KEY_APP_NAME);
if (appName != null) {
requestContext.getHeaders().add(RemotingConstants.HEAD_APP_NAME, appName);
}
RestBaggageItemsHandler.encodeBaggageItemToRequest(requestContext.getHeaders());
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class TripleTracerAdapter method serverReceived.
/**
* 适配服务端serverReceived
*/
public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefinition serverServiceDefinition, final ServerCall call, Metadata requestHeaders) {
try {
if (sofaRequest == null) {
sofaRequest = new SofaRequest();
}
Map<String, String> traceMap = new HashMap<String, String>();
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE)) {
sofaRequest.setTargetServiceUniqueName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
sofaRequest.setInterfaceName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
} else {
String serviceName = serverServiceDefinition.getServiceDescriptor().getName();
sofaRequest.setTargetServiceUniqueName(serviceName);
sofaRequest.setInterfaceName(serviceName);
}
final String serviceName = call.getMethodDescriptor().getServiceName();
sofaRequest.setTargetServiceUniqueName(serviceName);
sofaRequest.setInterfaceName(serviceName);
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_APP)) {
sofaRequest.setTargetAppName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_APP));
}
// 先取兼容的
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID)) {
traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID));
} else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TRACE_ID)) {
traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_TRACE_ID));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID)) {
traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID));
} else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_RPC_ID)) {
traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_RPC_ID));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION)) {
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION));
} else {
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SAMP_TYPE)) {
traceMap.put(TracerCompatibleConstants.SAMPLING_MARK, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SAMP_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_CURRENT_APP)) {
sofaRequest.addRequestProp(HEAD_APP_NAME, requestHeaders.get(TripleHeadKeys.HEAD_KEY_CURRENT_APP));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE)) {
sofaRequest.addRequestProp(RemotingConstants.HEAD_PROTOCOL, requestHeaders.get(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE)) {
sofaRequest.addRequestProp(RemotingConstants.HEAD_INVOKE_TYPE, requestHeaders.get(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE)) {
traceMap.put(TracerCompatibleConstants.PEN_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE)) {
traceMap.put(TracerCompatibleConstants.PEN_SYS_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE));
}
if (!traceMap.isEmpty()) {
sofaRequest.addRequestProp(RemotingConstants.RPC_TRACE_NAME, traceMap);
}
if (EventBus.isEnable(ServerReceiveEvent.class)) {
EventBus.post(new ServerReceiveEvent(sofaRequest));
}
String remoteIp = "";
SocketAddress socketAddress = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
if (socketAddress instanceof InetSocketAddress) {
remoteIp = ((InetSocketAddress) socketAddress).getHostName();
}
String methodName;
String fullMethodName = call.getMethodDescriptor().getFullMethodName();
methodName = StringUtils.substringAfter(fullMethodName, serviceName + "/");
sofaRequest.setMethodName(methodName);
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
// FIXME modify the dep relation
serverSpan.setTag("service", sofaRequest.getTargetServiceUniqueName());
serverSpan.setTag("method", methodName);
// 从请求里获取ConsumerTracerFilter额外传递的信息
serverSpan.setTag("remote.app", (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
serverSpan.setTag("protocol", RpcConstants.PROTOCOL_TYPE_TRIPLE);
serverSpan.setTag("remote.ip", remoteIp);
}
} catch (Throwable e) {
LOGGER.warn("triple serverReceived tracer error", e);
}
}
Aggregations