Commit a8d43fa2 authored by terrymanu's avatar terrymanu
Browse files

remove SamplingService, use apm sampling directly

parent 2ab5fc5f
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import io.shardingsphere.opentracing.listener.execution.DQLExecuteEventListener;
import io.shardingsphere.opentracing.listener.execution.OverallExecuteEventListener;
import io.shardingsphere.opentracing.listener.merger.MergeEventListener;
import io.shardingsphere.opentracing.listener.routing.RouteEventListener;
import io.shardingsphere.opentracing.sampling.SamplingService;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@@ -76,7 +75,6 @@ public final class ShardingTracer {
            return;
        }
        GlobalTracer.register(tracer);
        SamplingService.getInstance().init(samplingRatePerMinute);
        new OverallExecuteEventListener().register();
        new DQLExecuteEventListener().register();
        new DMLExecuteEventListener().register();
+0 −4
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package io.shardingsphere.opentracing.listener;

import io.shardingsphere.core.event.ShardingEvent;
import io.shardingsphere.core.event.ShardingEventBusInstance;
import io.shardingsphere.opentracing.sampling.SamplingService;

import java.util.HashMap;
import java.util.Map;
@@ -41,9 +40,6 @@ public abstract class TracingListener<T extends ShardingEvent> {
    }
    
    protected final void tracing(final T event) {
        if (!SamplingService.getInstance().trySampling()) {
            return;
        }
        switch (event.getEventType()) {
            case BEFORE_EXECUTE:
                beforeExecute(event);
+0 −2
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import io.shardingsphere.core.executor.event.OverallExecutionEvent;
import io.shardingsphere.core.executor.threadlocal.ExecutorDataMap;
import io.shardingsphere.opentracing.ShardingTracer;
import io.shardingsphere.opentracing.listener.TracingListener;
import io.shardingsphere.opentracing.sampling.SamplingService;
import io.shardingsphere.opentracing.tag.LocalTags;

/**
@@ -69,7 +68,6 @@ public final class OverallExecuteEventListener extends TracingListener<OverallEx
    protected void tracingFinish() {
        trunkContainer.get().deactivate();
        trunkContainer.remove();
        SamplingService.getInstance().increaseSampling();
    }
    
    @Override
+0 −94
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.sampling;

import io.shardingsphere.core.executor.ShardingThreadFactoryBuilder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Sampling control service.
 *
 * @author chenqingyang
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SamplingService {
    
    private static final SamplingService INSTANCE = new SamplingService();
    
    private int samplingRatePerMinute;
    
    private volatile AtomicInteger samplingCount = new AtomicInteger(0);
    
    private volatile ScheduledFuture<?> scheduledFuture;
    
    /**
     * Get sampling service instance.
     *
     * @return sampling service instance
     */
    public static SamplingService getInstance() {
        return INSTANCE;
    }
    
    /**
     * sampling service init.
     *
     * @param samplingRatePerMinute sampling rate in one minute
     */
    public void init(final int samplingRatePerMinute) {
        this.samplingRatePerMinute = samplingRatePerMinute;
        if (null != scheduledFuture) {
            scheduledFuture.cancel(true);
        }
        if (samplingRatePerMinute > 0) {
            ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(ShardingThreadFactoryBuilder.build("Opentracing-Sampling-Cleaner"));
            scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
                
                @Override
                public void run() {
                    samplingCount.set(0);
                }
            }, 0, 1, TimeUnit.MINUTES);
        }
    }
    
    /**
     * Judge sampling is allowed or not.
     *
     * @return sampling is allowed or not
     */
    public boolean trySampling() {
        return samplingCount.get() < samplingRatePerMinute;
    }
    
    /**
     * Increase sampling count.
     */
    public void increaseSampling() {
        if (samplingRatePerMinute > 0) {
            samplingCount.getAndIncrement();
        }
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -18,14 +18,12 @@
package io.shardingsphere.opentracing;

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

@RunWith(Suite.class)
@Suite.SuiteClasses({
        ConfigurationLoaderTest.class,
        SamplingServiceTest.class,
        ExecuteEventListenerTest.class,
        SqlRoutingEventListenerTest.class,
        MergeEventListenerTest.class,
Loading