Commit e2b6de98 authored by terrymanu's avatar terrymanu
Browse files

refactor ShardingTracer

parent e1a72ad5
Loading
Loading
Loading
Loading
+11 −16
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@

package io.shardingsphere.opentracing;

import com.google.common.base.Preconditions;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.opentracing.config.ConfigurationLoader;
import io.shardingsphere.opentracing.listener.execution.DMLExecuteEventListener;
import io.shardingsphere.opentracing.listener.execution.DQLExecuteEventListener;
import io.shardingsphere.opentracing.listener.execution.OverallExecuteEventListener;
@@ -38,26 +38,25 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingTracer {
    
    private static final String OPENTRACING_TRACER_CLASS_NAME = "shardingsphere.opentracing.tracer.class";
    
    /**
     * Initialize tracer.
     * Initialize sharding tracer.
     */
    public static void init() {
        if (GlobalTracer.isRegistered()) {
            return;
        }
        ConfigurationLoader configuration = new ConfigurationLoader();
        String tracerClassName = configuration.getTracerClassName();
        String tracerClassName = System.getProperty(OPENTRACING_TRACER_CLASS_NAME);
        Preconditions.checkNotNull(tracerClassName, "Can not find opentracing tracer implementation class.");
        try {
            init((Tracer) Class.forName(tracerClassName).newInstance());
        } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
            throw new ShardingException("Parse tracer class name", ex);
        } catch (final ReflectiveOperationException ex) {
            throw new ShardingException("Initialize opentracing tracer class failure.", ex);
        }
    }
    
    /**
     * Initialize tracer from another one.
     * Initialize sharding tracer.
     *
     * @param tracer that is delegated
     * @param tracer opentracing tracer
     */
    public static void init(final Tracer tracer) {
        if (GlobalTracer.isRegistered()) {
@@ -72,15 +71,11 @@ public final class ShardingTracer {
    }
    
    /**
     * Get the tracer from container.
     * Get tracer.
     *
     * @return tracer
     */
    public static Tracer get() {
        if (GlobalTracer.isRegistered()) {
            return GlobalTracer.get();
        }
        init();
        return GlobalTracer.get();
    }
}
+0 −40
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.opentracing.config;

import com.google.common.base.Preconditions;
import lombok.Getter;

/**
 * Config loader.
 *
 * @author gaohongtao
 * @author wangkai
 */
@Getter
public final class ConfigurationLoader {
    
    private static final String OPENTRACING_TRACER_CLASS_NAME = "shardingsphere.opentracing.tracer.class";
    
    private final String tracerClassName;
    
    public ConfigurationLoader() {
        tracerClassName = System.getProperty(OPENTRACING_TRACER_CLASS_NAME);
        Preconditions.checkNotNull(tracerClassName, "Can not find opentracing tracer implementation class.");
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -17,13 +17,11 @@

package io.shardingsphere.opentracing;

import io.shardingsphere.opentracing.config.ConfigurationLoaderTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        ConfigurationLoaderTest.class,
        ExecuteEventListenerTest.class,
        SqlRoutingEventListenerTest.class,
        MergeEventListenerTest.class,
+2 −1
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ public final class ShardingJDBCTracerTest {
    
    @Test
    public void assertTracer() {
        ShardingTracer.init();
        assertThat((GlobalTracer) ShardingTracer.get(), isA(GlobalTracer.class));
        assertTrue(GlobalTracer.isRegistered());
        assertThat(ShardingTracer.get(), is(ShardingTracer.get()));
@@ -72,7 +73,7 @@ public final class ShardingJDBCTracerTest {
    @Test(expected = ShardingException.class)
    public void assertTracerClassError() {
        System.setProperty("shardingsphere.opentracing.tracer.class", "com.foo.FooTracer");
        ShardingTracer.get();
        ShardingTracer.init();
    }
    
    private static void clearGlobalTracer() throws NoSuchFieldException, IllegalAccessException {
+0 −45
Original line number Diff line number Diff line
/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.opentracing.config;

import io.shardingsphere.opentracing.fixture.FooTracer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public final class ConfigurationLoaderTest {
    
    @Before
    public void setUp() {
        System.setProperty("shardingsphere.opentracing.tracer.class", FooTracer.class.getName());
    }
    
    @After
    public void tearDown() {
        System.getProperties().remove("shardingsphere.opentracing.tracer.class");
    }
    
    @Test
    public void assertLoadConfigFromProperty() {
        assertThat(new ConfigurationLoader().getTracerClassName(), is(FooTracer.class.getName()));
    }
    
}
Loading