Skip to content

Commit

Permalink
NoopTracer as default
Browse files Browse the repository at this point in the history
  • Loading branch information
rayzhang0603 committed Dec 21, 2016
1 parent 630fab8 commit a179cc0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 8 additions & 2 deletions motan-extension/filter-extension/filter-opentracing/pom.xml
Expand Up @@ -21,6 +21,7 @@
<url>https://github.com/weibocom/motan</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opentracing.version>0.20.2</opentracing.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -31,14 +32,19 @@
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>0.20.2</version>
<version>${opentracing.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-impl</artifactId>
<version>${opentracing.version}</version>
</dependency>

<!-- for test -->
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<version>0.20.2</version>
<version>${opentracing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -13,6 +13,7 @@
*/
package com.weibo.api.motan.filter.opentracing;

import io.opentracing.NoopTracer;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
Expand Down Expand Up @@ -49,8 +50,8 @@ public class OpenTracingFilter implements Filter {

@Override
public Response filter(Caller<?> caller, Request request) {
Tracer tracer = OpenTracingContext.getTracer();
if (tracer == null) {
Tracer tracer = getTracer();
if (tracer == null || tracer instanceof NoopTracer) {
return caller.call(request);
}
if (caller instanceof Provider) { // server end
Expand All @@ -59,6 +60,10 @@ public Response filter(Caller<?> caller, Request request) {
return processRefererTrace(tracer, caller, request);
}
}

protected Tracer getTracer(){
return OpenTracingContext.getTracer();
}

/**
* process trace in client end
Expand Down
Expand Up @@ -15,12 +15,13 @@
*/
package com.weibo.api.motan.filter.opentracing;

import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;

import java.util.Iterator;
import java.util.ServiceLoader;

import com.weibo.api.motan.util.LoggerUtil;

import io.opentracing.Tracer;
/**
*
* @Description TracerFactory
Expand All @@ -40,23 +41,29 @@ public interface TracerFactory {
Tracer getTracer();

class DefaultTracerFactory implements TracerFactory {
private static Tracer tracer = null;
private static Tracer tracer = NoopTracerFactory.create();

static {
loadDefaultTracer();
}

/**
* load SPI Tracer and set default. the first tracer was used if more than one tracer was
* found.
* load SPI Tracer and set default only if one tracer is found.
*/
private static void loadDefaultTracer() {
try {
Iterator<Tracer> implementations = ServiceLoader.load(Tracer.class, Tracer.class.getClassLoader()).iterator();
if (implementations.hasNext()) {
tracer = implementations.next();
LoggerUtil.info("io.opentracing.Tracer load in DefaultTracerFactory, " + tracer.getClass().getSimpleName()
Tracer firstTracer = implementations.next();
if(!implementations.hasNext()){
// only one tracer is found.
tracer = firstTracer;
LoggerUtil.info("io.opentracing.Tracer load in DefaultTracerFactory, " + tracer.getClass().getSimpleName()
+ " is used as default tracer.");
} else {
LoggerUtil.info("io.opentracing.Tracer load in DefaultTracerFactory, NoopTracer is used as default tracer since more than one tracer is found.");
}

}
} catch (Exception e) {
LoggerUtil.warn("DefaultTracerFactory load Tracer fail.", e);
Expand Down

0 comments on commit a179cc0

Please sign in to comment.