public ContextData beforeInvoke(Identification id) { try { Span spanData = ContextGenerator.generateSpanFromThreadLocal(id); // 设置SpanType的类型 spanData.setTag(Tag.SPAN_TYPE, SpanType.RPC_CLIENT); if (Config.BuriedPoint.PRINTF) { logger.debug( "TraceId:" + spanData.getTraceId() + "\tParentLevelId:" + spanData.getParentLevel() + "\tLevelId:" + spanData.getLevelId() + "\tbusinessKey:" + spanData.getBusinessKey()); } CurrentThreadSpanStack.push(spanData); sendRequestSpan(spanData, id); return new ContextData( spanData.getTraceId(), generateSubParentLevelId(spanData), spanData.getRouteKey()); } catch (Throwable t) { logger.error(t.getMessage(), t); return new EmptyContextData(); } }
protected ContextData beforeInvoke(Span spanData, Identification id) { if (Config.BuriedPoint.PRINTF) { easyLogger.debug( "TraceId:" + spanData.getTraceId() + "\tParentLevelId:" + spanData.getParentLevel() + "\tLevelId:" + spanData.getLevelId() + "\tbusinessKey:" + spanData.getBusinessKey()); } // 将新创建的Context存放到ThreadLocal栈中。 CurrentThreadSpanStack.push(spanData); // 根据SpanData生成RequestSpan,并保存 CurrentThreadSpanStack.push(spanData); sendRequestSpan(spanData, id); // 并将当前的Context返回回去 return new ContextData(spanData); }
public DynamicType.Builder<?> define(String transformClassName, DynamicType.Builder<?> builder) throws PluginException { String interceptorDefineClassName = this.getClass().getName(); if (StringUtil.isEmpty(transformClassName)) { logger.warn( "classname of being intercepted is not defined by {}.", interceptorDefineClassName); return builder; } logger.debug( "prepare to enhance class {} by {}.", transformClassName, interceptorDefineClassName); /** find witness classes for enhance class */ String[] witnessClasses = witnessClasses(); if (witnessClasses != null) { for (String witnessClass : witnessClasses) { Resolution witnessClassResolution = PluginBootstrap.CLASS_TYPE_POOL.describe(witnessClass); if (!witnessClassResolution.isResolved()) { logger.warn( "enhance class {} by plugin {} is not working. Because witness class {} is not existed.", transformClassName, interceptorDefineClassName, witnessClass); return builder; } } } /** find origin class source code for interceptor */ DynamicType.Builder<?> newClassBuilder = this.enhance(transformClassName, builder); logger.debug( "enhance class {} by {} completely.", transformClassName, interceptorDefineClassName); return newClassBuilder; }
protected void afterInvoke() { try { // 弹出上下文的栈顶中的元素 Span spanData = CurrentThreadSpanStack.pop(); if (Config.BuriedPoint.PRINTF) { easyLogger.debug( "TraceId-ACK:" + spanData.getTraceId() + "\tParentLevelId:" + spanData.getParentLevel() + "\tLevelId:" + spanData.getLevelId() + "\tbusinessKey:" + spanData.getBusinessKey()); } sendAckSpan(spanData); } catch (Throwable t) { easyLogger.error(t.getMessage(), t); } }