sviolet.slate.common.x.monitor.txtimer.TxTimer Maven / Gradle / Ivy
Show all versions of slate-common Show documentation
/*
* Copyright (C) 2015-2018 S.Violet
*
* 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.
*
* Project GitHub: https://github.com/shepherdviolet/slate
* Email: [email protected]
*/
package sviolet.slate.common.x.monitor.txtimer;
import com.github.shepherdviolet.glaciion.Glaciion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 简单的交易耗时统计, 提供默认实现, 也可以用Glaciion SPI扩展
*
* 日志前缀:TxTimer
*
* 启动参数:
* -Dslate.txtimer.enabled=true 启用统计, true开启, false关闭, 默认开启
*
*
* 默认实现 ********************************************************************************************
*
*
* 1.默认实现了交易耗时的统计, 并通过日志定时输出报告.
* 2.可以使用Glaciion SPI替换实现, 替换实现后下面的参数无效.
*
*
* 默认实现的启动参数(不可动态修改):
* -Dslate.txtimer.report.interval=5 日志报告输出间隔, 单位分钟, [2-60], 默认5
* -Dslate.txtimer.pagelines=20 日志每次输出的最大行数, 大于该行数会分页, 默认20
*
*
* 默认实现的启动参数(可动态修改):
* -Dslate.txtimer.reportall.interval=60 全量日志报告输出间隔(周期), 单位:分钟, [2-∞], 默认∞(不输出全量日志)
* -Dslate.txtimer.threshold.avg=2000 打印周期内平均耗时超过该值的交易, 单位:毫秒
* -Dslate.txtimer.threshold.max=10000 打印周期内最大耗时超过该值的交易, 单位:毫秒
* -Dslate.txtimer.threshold.min=1000 打印周期内最小耗时超过该值的交易, 单位:毫秒
*
*
* slate.txtimer.threshold系列参数均未配置, 则输出全部交易的报告. 若设置了任意一个, 则只有满足条件的交易才输出:
* avg >= thresholdAvg || max >= thresholdMax || min >= thresholdMin
*
* @author S.Violet
*/
public class TxTimer {
private static final Logger logger = LoggerFactory.getLogger(TxTimer.class);
private static final TxTimerProvider PROVIDER;
static {
//统计开关, 默认关闭
if ("true".equals(System.getProperty("slate.txtimer.enabled", "true"))) {
TxTimerProvider service = Glaciion.loadSingleService(TxTimerProvider.class).get();
//再根据provider判断是否要启用
if (service.enabled()) {
PROVIDER = service;
logger.info("TxTimer | TxTimer Enabled !!! implementation " + PROVIDER.getClass().getName());
} else {
PROVIDER = null;
}
} else {
PROVIDER = null;
}
}
/**
* 交易开始时调用
*
*
* try {
* TxTimer.start("Entrance", "TestService");
* // 交易逻辑 ......
* } finally {
* TxTimer.stop();
* }
*
*
* @param groupName 组别
* @param transactionName 交易名
*/
public static void start(String groupName, String transactionName){
if (PROVIDER != null) {
PROVIDER.start(groupName, transactionName);
}
}
/**
* 交易结束时调用
*
*
* try {
* TxTimer.start("Entrance", "TestService");
* // 交易逻辑 ......
* } finally {
* TxTimer.stop();
* }
*
*
*/
public static void stop(){
if (PROVIDER != null) {
PROVIDER.stop();
}
}
/**
* 交易结束时调用
*
*
* try {
* TxTimer.start("Entrance", "TestService");
* // 交易逻辑 ......
* } finally {
* TxTimer.stop();
* }
*
*
* @param resultCode 处理结果编码
*/
public static void stop(int resultCode){
if (PROVIDER != null) {
PROVIDER.stop(resultCode);
}
}
public static TxTimerProvider getProvider(){
if (PROVIDER != null && PROVIDER.canBeGet()) {
return PROVIDER;
}
logger.error("TxTimer | Prohibit access to get TxTimerProvider, Null or Banned by TxTimerProvider.canBeGet()");
return null;
}
}