package com.vipshop.microscope.trace;

import com.vipshop.microscope.trace.span.Category;

public class Tracer {
	
	/**
	 * Handle common method opeations.
	 * 
	 * @param spanName the name of method
	 * @param category the category of service
	 */
	public static void clientSend(String spanName, Category category){
		TraceFactory.getTrace().clientSend(spanName, category);
	}
	
	/**
	 * Handle MyBatis/Hibernate/SQL/database operations.
	 * 
	 * @param name the name of method
	 * @param serverIP the database name where sql execute
	 * @param category the category of service
	 */
	public static void clientSend(String name, String serverIP, Category category) {
		TraceFactory.getTrace().clientSend(name, serverIP, category);
	}

	/**
	 * Handle corss-JVM operations.(httpclient request)
	 * 
	 * @param traceId the traceId from client
	 * @param spanId  the spanId from client
	 * @param name the name of method
	 * @param category the category of service
	 */
	public static void clientSend(String traceId, String spanId, String name, Category category){
		TraceFactory.getTrace(traceId, spanId).clientSend(name, category);
	}
	
	/**
	 * Complete a span.
	 */
	public static void clientReceive() {
		TraceFactory.getTrace().clientReceive();
	}

	/**
	 * Set result code.
	 * 
	 * If exception happens. set ResultCode = EXCEPTION.
	 * 
	 * @param e
	 */
	public static void setResultCode(String result) {
		Trace trace = TraceFactory.getContext();
		if (trace != null) {
			trace.setResutlCode(result);
		}
	}
	
	/**
	 * Record a Key-Value for debug.
	 * 
	 * @param key key String
	 * @param value value String
	 */
	public static void record(String key, String value) {
		Trace trace = TraceFactory.getContext();
		if (trace != null) {
			trace.record(key, value);
		}
	}
	
	/**
	 * Asyn thread invoke.
	 * 
	 * Get trace object from {@code ThreadLocal}.
	 * 
	 * @return
	 */
	public static Trace getContext() {
		return TraceFactory.getContext();
	}
	
	/**
	 * Asyn thread invoke.
	 * 
	 * Set trace object to {@code ThreadLocal}
	 * with the new Thread.
	 * 
	 * @param trace
	 */
	public static void setContext(Trace trace) {
		TraceFactory.setContext(trace);
	}
	
	/**
	 * Clean {@code ThreadLocal}
	 */
	public static void cleanContext() {
		TraceFactory.cleanContext();
	}
	
	/**
	 * Get traceId from {@code ThreadLocal}
	 * 
	 * @return traceId
	 */
	public static String getTraceId() {
		return TraceFactory.getTraceIdFromThreadLocal();
	}
	
	/**
	 * Get spanId from {@code ThreadLocal}
	 * 
	 * @return spanId
	 */
	public static String getSpanId() {
		return TraceFactory.getSpanIdFromThreadLocal();
	}
	
}

@@ -84,16 +84,448 @@
 egory;%0A%0A
+/**%0A * Tracing client API for Java.%0A * %0A * %3Cp%3E Basically, we use %7B@code collector%7D as our backend system,%0A * we build a java tracing client to collector message, use%0A * %7B@code ThreadTransporter%7D transport spans to %7B@code collector%7D.%0A * %0A * %3Cp%3E Application programmers should use this API in app code%0A * if necessary. But in some case, we will embed tracing API%0A * to framework if possible.%0A * %0A * @author Xu Fei%0A * @version 1.0%0A */%0A
 public c

add comment