All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.monitor.collect.task.LogStatisticCollectTask Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * 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
 *
 *      https://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.
 */

package com.taotao.boot.monitor.collect.task;

import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.core.support.Collector;
import com.taotao.boot.monitor.annotation.FieldReport;
import com.taotao.boot.monitor.collect.AbstractCollectTask;
import com.taotao.boot.monitor.collect.CollectInfo;
import com.taotao.boot.monitor.properties.CollectTaskProperties;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 收集日志记录情况:最近一分钟增长日志条数及错误日志条数
 *
 * @version 2022.03
 */
public class LogStatisticCollectTask extends AbstractCollectTask {

    private static final String TASK_NAME = "ttc.monitor.collect.log.statistic";

    private final CollectTaskProperties properties;

    public LogStatisticCollectTask(CollectTaskProperties properties) {
        this.properties = properties;
    }

    @Override
    public int getTimeSpan() {
        return properties.getLogStatisticTimeSpan();
    }

    @Override
    public boolean getEnabled() {
        return properties.isLogStatisticEnabled();
    }

    @Override
    public String getDesc() {
        return this.getClass().getName();
    }

    @Override
    public String getName() {
        return TASK_NAME;
    }

    @Override
    protected CollectInfo getData() {
        try {
            LogErrorInfo info = new LogErrorInfo();
            Collector collector = Collector.getCollector();
            if (Objects.nonNull(collector)) {
                info.logerrorCount = collector
                                        .value("ttc.monitor.collect.log.error.count")
                                        .get()
                                == null
                        ? 0
                        : ((AtomicLong) (collector
                                        .value("ttc.monitor.collect.log.error.count")
                                        .get()))
                                .intValue();
                info.logIncreCount = collector
                                        .value("ttc.monitor.collect.log.incre.count")
                                        .get()
                                == null
                        ? 0
                        : ((AtomicLong) (collector
                                        .value("ttc.monitor.collect.log.incre.count")
                                        .get()))
                                .intValue();
                return info;
            }
        } catch (Exception e) {
            if (LogUtils.isErrorEnabled()) {
                LogUtils.error(e);
            }
        }
        return null;
    }

    private static class LogErrorInfo implements CollectInfo {

        @FieldReport(name = TASK_NAME + ".error.count", desc = "最近1分钟错误日志数量")
        private Integer logerrorCount = 0;

        @FieldReport(name = TASK_NAME + ".incre.count", desc = "最近1分钟日志条数增量")
        private Integer logIncreCount = 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy