use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class TripleTracerAdapter method beforeSend.
/**
* 存入tracer信息
*
* @param sofaRequest SofaRequest
* @param requestHeader Metadata
*/
public static void beforeSend(SofaRequest sofaRequest, ConsumerConfig consumerConfig, Metadata requestHeader) {
// 客户端设置请求服务端的Header
// tracer信息放入request 发到服务端
Map<String, String> header = new HashMap<String, String>();
header.put(RemotingConstants.HEAD_METHOD_NAME, sofaRequest.getMethodName());
header.put(RemotingConstants.HEAD_TARGET_SERVICE, sofaRequest.getTargetServiceUniqueName());
header.put(RemotingConstants.HEAD_TARGET_APP, sofaRequest.getTargetAppName());
// 客户端的启动
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
// 获取并不弹出
SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
if (clientSpan == null) {
SelfLog.warn("ClientSpan is null.Before call interface=" + sofaRequest.getInterfaceName() + ",method=" + sofaRequest.getMethodName());
} else {
SofaTracerSpanContext sofaTracerSpanContext = clientSpan.getSofaTracerSpanContext();
header.put(TripleHeadKeys.HEAD_KEY_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
header.put(TripleHeadKeys.HEAD_KEY_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
header.put(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
header.put(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
header.put(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getBizSerializedBaggage());
header.put(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getSysSerializedBaggage());
}
// 获取 RPC 上下文
RpcInvokeContext internalContext = RpcInvokeContext.getContext();
String route = (String) internalContext.get(USERID_KEY);
if (StringUtils.isNotBlank(route)) {
Map<String, String> map = new HashMap<>();
map.put(USERID_KEY, route);
header.put(TripleHeadKeys.HEAD_KEY_UNIT_INFO.name(), JSONUtils.toJSONString(map));
}
if (StringUtils.isNotEmpty(consumerConfig.getUniqueId())) {
header.put(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION.name(), consumerConfig.getUniqueId());
}
header.put(TripleHeadKeys.HEAD_KEY_META_TYPE.name(), "rpc");
header.put(TripleHeadKeys.HEAD_KEY_CURRENT_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
header.put(TripleHeadKeys.HEAD_KEY_CONSUMER_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
header.put(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_PROTOCOL));
header.put(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_INVOKE_TYPE));
final String source = consumerConfig.getParameter("interworking.source");
if (StringUtils.isNotBlank(source)) {
header.put(TripleHeadKeys.HEAD_KEY_SOURCE_TENANTID.name(), source);
}
final String target = consumerConfig.getParameter("interworking.target");
if (StringUtils.isNotBlank(target)) {
header.put(TripleHeadKeys.HEAD_KEY_TARGET_TENANTID.name(), target);
}
for (Map.Entry<String, String> entry : header.entrySet()) {
if (StringUtils.isNotBlank(entry.getValue())) {
requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
}
}
// set custom headers
try {
Set<Map.Entry<String, String>> customHeader = RpcInvokeContext.getContext().getCustomHeader().entrySet();
for (Map.Entry<String, String> entry : customHeader) {
if (StringUtils.isNotBlank(entry.getValue())) {
requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
}
}
} finally {
RpcInvokeContext.getContext().clearCustomHeader();
}
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class PressureMarkTransformFilterTest method testProviderPressure.
@Test
public void testProviderPressure() {
Metadata metadata = new Metadata();
metadata.put(HEAD_KEY_TRAFFIC_TYPE, "pressure");
Context context = Context.current().withValue(TracingContextKey.getKeyMetadata(), metadata);
context.attach();
PressureMarkTransformFilter filter = new PressureMarkTransformFilter();
filter.invoke(invoker, request);
SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RpcSofaTracer method startRpc.
@Override
public void startRpc(SofaRequest request) {
// 客户端的启动
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.pop();
SofaTracerSpan clientSpan = (SofaTracerSpan) this.sofaTracer.buildSpan(request.getInterfaceName()).asChildOf(serverSpan).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start();
if (RpcInternalContext.isAttachmentEnable()) {
RpcInternalContext context = RpcInternalContext.getContext();
clientSpan.setTag(RpcSpanTags.LOCAL_APP, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_APP_NAME));
clientSpan.setTag(RpcSpanTags.PROTOCOL, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_PROTOCOL_NAME));
SofaTracerSpanContext spanContext = clientSpan.getSofaTracerSpanContext();
if (spanContext != null) {
context.setAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID, spanContext.getTraceId());
context.setAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID, spanContext.getSpanId());
}
}
clientSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
clientSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
clientSpan.setTag(RpcSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
// 需要主动缓存自己的 serverSpan,原因是:asChildOf 关注的是 spanContext
clientSpan.setParentSofaTracerSpan(serverSpan);
// push
sofaTraceContext.push(clientSpan);
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RpcSofaTracer method profile.
@Override
public void profile(String profileApp, String code, String message) {
// 注意 profile 需要打印 traceId 等信息,所以要打开 tracer 才可以
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan sofaTracerSpan = sofaTraceContext.getCurrentSpan();
if (sofaTracerSpan != null) {
sofaTracerSpan.profile(profileApp, code, message);
}
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RpcSofaTracer method clientAsyncReceivedPrepare.
@Override
public void clientAsyncReceivedPrepare() {
// 新的线程
RpcInternalContext rpcInternalContext = RpcInternalContext.getContext();
SofaTracerSpan clientSpan = (SofaTracerSpan) rpcInternalContext.getAttachment(RpcConstants.INTERNAL_KEY_TRACER_SPAN);
if (clientSpan == null) {
return;
}
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
sofaTraceContext.push(clientSpan);
}
Aggregations