共计 3289 个字符,预计需要花费 9 分钟才能阅读完成。
序
本文主要研究一下 sharding-jdbc 的 ShardingTracer
ShardingTracer
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/main/java/org/apache/shardingsphere/opentracing/ShardingTracer.java
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingTracer {
private static final String OPENTRACING_TRACER_CLASS_NAME = "org.apache.shardingsphere.opentracing.tracer.class";
/**
* Initialize sharding tracer.
*/
public static void init() {String tracerClassName = System.getProperty(OPENTRACING_TRACER_CLASS_NAME);
Preconditions.checkNotNull(tracerClassName, "Can not find opentracing tracer implementation class via system property `%s`", OPENTRACING_TRACER_CLASS_NAME);
try {init((Tracer) Class.forName(tracerClassName).newInstance());
} catch (final ReflectiveOperationException ex) {throw new ShardingException("Initialize opentracing tracer class failure.", ex);
}
}
/**
* Initialize sharding tracer.
*
* @param tracer opentracing tracer
*/
public static void init(final Tracer tracer) {if (!GlobalTracer.isRegistered()) {GlobalTracer.register(tracer);
}
}
/**
* Get tracer.
*
* @return tracer
*/
public static Tracer get() {return GlobalTracer.get();
}
}
- ShardingTracer 的 init 方法会重系统 properties 中读取 OPENTRACING_TRACER_CLASS_NAME 的值,然后使用 Class.forName 加载并创建实例,然后在通过 opentracing 的 GlobalTracer.register 进行注册
ShardingTracerTest
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/test/java/org/apache/shardingsphere/opentracing/ShardingTracerTest.java
public final class ShardingTracerTest {
@Before
public void setUp() {System.setProperty("org.apache.shardingsphere.opentracing.tracer.class", FooTracer.class.getName());
clearGlobalTracer();}
@After
public void tearDown() {System.getProperties().remove("org.apache.shardingsphere.opentracing.tracer.class");
}
@Test
public void assertDuplicatedLoading() {ShardingTracer.init(mock(Tracer.class));
Tracer t1 = ShardingTracer.get();
ShardingTracer.init();
assertEquals(t1, ShardingTracer.get());
ShardingTracer.init(mock(Tracer.class));
assertEquals(t1, ShardingTracer.get());
}
@Test
public void assertTracer() {ShardingTracer.init();
assertThat((GlobalTracer) ShardingTracer.get(), isA(GlobalTracer.class));
assertTrue(GlobalTracer.isRegistered());
assertThat(ShardingTracer.get(), is(ShardingTracer.get()));
}
@Test(expected = ShardingException.class)
public void assertTracerClassError() {System.setProperty("org.apache.shardingsphere.opentracing.tracer.class", "com.foo.FooTracer");
ShardingTracer.init();}
@SneakyThrows
private static void clearGlobalTracer() {Field tracerField = GlobalTracer.class.getDeclaredField("tracer");
tracerField.setAccessible(true);
tracerField.set(GlobalTracer.class, NoopTracerFactory.create());
}
}
- 这里指定了 OPENTRACING_TRACER_CLASS_NAME 值为 FooTracer
FooTracer
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/test/java/org/apache/shardingsphere/opentracing/fixture/FooTracer.java
public final class FooTracer implements Tracer {
@Override
public SpanBuilder buildSpan(final String operationName) {return null;}
@Override
public <C> void inject(final SpanContext spanContext, final Format<C> format, final C carrier) { }
@Override
public <C> SpanContext extract(final Format<C> format, final C carrier) {return null;}
@Override
public ActiveSpan activeSpan() {return null;}
@Override
public ActiveSpan makeActive(final Span span) {return null;}
}
- FooTracer 实现了 Tracer 接口,这里为空实现
小结
ShardingTracer 的 init 方法会重系统 properties 中读取 OPENTRACING_TRACER_CLASS_NAME 的值,然后使用 Class.forName 加载并创建实例,然后在通过 opentracing 的 GlobalTracer.register 进行注册
doc
- ShardingTracer
正文完
发表至:无分类
2019-08-07