Reputation: 11
I find restTemplateInterceptor and feignRequestInterceptor from spring-cloud-sleuth-core, but our project use hessian connect microservices, and I find that spring-cloud-sleuth can not inject to hessian client. Can anyone share the code how to use spring-cloud-sleuth with hessian? Thanks~
Upvotes: 1
Views: 325
Reputation: 11
get trace for the spring_context,make a span from the bean of trace ,then add the info of the span to the http header by rewrite the hessianProxy,it will work。
provide demo
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.caucho.hessian.client.HessianConnection;
import com.caucho.hessian.client.HessianProxyFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.web.TraceFilter;
import org.springframework.cloud.sleuth.util.SpanNameUtil;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author yezhi.li
* @date 19-1-3 下午2:41
*/
public class MyHessianProxy extends com.caucho.hessian.client.HessianProxy {
//create function new proxy
private URL urlLocal =null;
protected static final String TRACE_REQUEST_ATTR = TraceFilter.class.getName()
+ ".TRACE";
private static final long serialVersionUID = 6732486042199684754L;
private Logger log = LoggerFactory.getLogger(MyHessianProxy.class);
protected MyHessianProxy(URL url, HessianProxyFactory factory) {
super(url, factory);
this.urlLocal =url;
}
protected MyHessianProxy(URL url, HessianProxyFactory factory, Class<?> type) {
super(url, factory, type);
this.urlLocal =url;
}
@Override
protected void addRequestHeaders(HessianConnection conn)
{
Tracer tracer = SpringUtil.getBean(Tracer.class);
String spanName = null;
try {
spanName = getName(urlLocal.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
Span newSpan = tracer.createSpan(spanName,trace.getcurrentSpan());
inject(newSpan ,conn);
//detach span
tracer.detach(newSpan);
}
private String getName(URI uri) {
// The returned name should comply with RFC 882 - Section 3.1.2.
// i.e Header values must composed of printable ASCII values.
return SpanNameUtil.shorten(uriScheme(uri) + ":" + uri.getRawPath());
}
private String uriScheme(URI uri) {
return uri.getScheme() == null ? "http" : uri.getScheme();
}
public void inject(Span span,HessianConnection conn){
setHeader(conn, Span.TRACE_ID_NAME, span.traceIdString());
setIdHeader(conn, Span.SPAN_ID_NAME, span.getSpanId());
setHeader(conn, Span.SAMPLED_NAME, span.isExportable() ? Span.SPAN_SAMPLED : Span.SPAN_NOT_SAMPLED);
setHeader(conn, Span.SPAN_NAME_NAME, span.getName());
setIdHeader(conn, Span.PARENT_ID_NAME, getParentId(span));
setHeader(conn, Span.PROCESS_ID_NAME, span.getProcessId());
for (Map.Entry<String, String> entry : span.baggageItems()) {
conn.addHeader(prefixedKey(entry.getKey()), entry.getValue());
}
}
private void setIdHeader(HessianConnection conn, String name, Long value) {
if (value != null) {
setHeader(conn, name, Span.idToHex(value));
}
}
private void setHeader(HessianConnection conn,String key,String value){
conn.addHeader(key,value);
}
private Long getParentId(Span span) {
return !span.getParents().isEmpty() ? span.getParents().get(0) : null;
}
private String prefixedKey(String key) {
if (key.startsWith(Span.SPAN_BAGGAGE_HEADER_PREFIX
+ "-")) {
return key;
}
return Span.SPAN_BAGGAGE_HEADER_PREFIX + "-"
+ key;
}
}
Upvotes: 1
Reputation: 11
There is no default spring-cloud-sleuth injection to hessian client, So, I have to make 2 hooks around the hessian client , add add zipkin inform code to the hook.
Upvotes: 0