All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.giants.dubbo.chain.trace.zipkin.DubboConsumerInterceptor Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.giants.dubbo.chain.trace.zipkin;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.fastjson.JSON;
import com.giants.common.exception.BusinessException;
import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.ClientRequestAdapter;
import com.github.kristofa.brave.ClientRequestInterceptor;
import com.github.kristofa.brave.ClientResponseAdapter;
import com.github.kristofa.brave.ClientResponseInterceptor;
import com.github.kristofa.brave.ClientSpanThreadBinder;
import com.github.kristofa.brave.IdConversion;
import com.github.kristofa.brave.KeyValueAnnotation;
import com.github.kristofa.brave.SpanId;
import com.github.kristofa.brave.internal.Nullable;
import com.github.kristofa.brave.internal.Util;
import com.twitter.zipkin.gen.Span;

/**
 * dubbo消费者拦截器
 * @author vencent.lu
 *
 */
@Activate(group = Constants.CONSUMER)
public class DubboConsumerInterceptor implements Filter{
	
    private final ClientRequestInterceptor clientRequestInterceptor;
    private final ClientResponseInterceptor clientResponseInterceptor;
    private final ClientSpanThreadBinder clientSpanThreadBinder;
   
    public DubboConsumerInterceptor() {
        Brave brave = BravePack.getInstance().getBrave();
        this.clientRequestInterceptor = Util.checkNotNull(brave.clientRequestInterceptor(), null);
        this.clientResponseInterceptor = Util.checkNotNull(brave.clientResponseInterceptor(), null);
        this.clientSpanThreadBinder = Util.checkNotNull(brave.clientSpanThreadBinder(), null);
    }

   
	public Result invoke(Invoker arg0, Invocation arg1) throws RpcException {
		clientRequestInterceptor.handle(new GrpcClientRequestAdapter(arg1));
		final Span currentClientSpan = clientSpanThreadBinder.getCurrentClientSpan();
		Result result ;
		try {
			result =  arg0.invoke(arg1);
            clientSpanThreadBinder.setCurrentSpan(currentClientSpan);
            clientResponseInterceptor.handle(new GrpcClientResponseAdapter(result));
        } finally {
            clientSpanThreadBinder.setCurrentSpan(null);
        }
		return result;
	}

    static final class GrpcClientRequestAdapter implements ClientRequestAdapter {
    	private Invocation invocation;
        public GrpcClientRequestAdapter(Invocation invocation) {
            this.invocation = invocation;
        }

       
        public String getSpanName() {
            return new StringBuilder(this.invocation.getInvoker().getInterface().getName()).append('.')
                    .append(this.invocation.getMethodName()).toString();
        }

       
        public void addSpanIdToRequest(@Nullable SpanId spanId) {
        	Map at = this.invocation.getAttachments();
            if (spanId == null) {
                at.put("Sampled", "0");
            } else {
            	
                at.put("Sampled", "1");
                at.put("TraceId", spanId.traceIdString());
                at.put("SpanId", IdConversion.convertToString(spanId.spanId));
                
                if (spanId.nullableParentId() != null) {
                    at.put("ParentSpanId", IdConversion.convertToString(spanId.parentId));
                }
            }
        }

       
        public Collection requestAnnotations() {
            KeyValueAnnotation an = KeyValueAnnotation.create("params", JSON.toJSONString(this.invocation.getArguments()));
            return Collections.singletonList(an);
        }
       
        public com.twitter.zipkin.gen.Endpoint serverAddress() {
            return null;
        }
    }

    static final class GrpcClientResponseAdapter implements ClientResponseAdapter {

        private final Result result;

        public GrpcClientResponseAdapter(Result result) {
            this.result = result;
        }
        
        public Collection responseAnnotations() {
            if (!result.hasException()) {
                return Collections.emptyList();
            } else {
                if (result.getException() instanceof BusinessException) {
                    return Collections.singletonList(KeyValueAnnotation.create("result", result.getException().getMessage()));
                } else {
                    String errorValue = result.getException().getMessage();
                    if (errorValue == null) {
                        errorValue = result.getException().toString();
                    }
                    return Collections.singletonList(KeyValueAnnotation.create("error", errorValue));
                }                
            }
                
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy