聊聊sharding-jdbc的ShardingTracer

本文主要研究一下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

    原文作者:codecraft
    原文地址: https://segmentfault.com/a/1190000019991540
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞