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

ai.platon.pulsar.skeleton.common.metrics.EnumCounterReporter.kt Maven / Gradle / Ivy

/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package ai.platon.pulsar.skeleton.common.metrics

import ai.platon.pulsar.common.AppContext
import ai.platon.pulsar.common.DateTimes
import ai.platon.pulsar.common.concurrent.GracefulScheduledExecutor
import ai.platon.pulsar.common.config.CapabilityTypes
import ai.platon.pulsar.common.config.ImmutableConfig
import ai.platon.pulsar.common.readableClassName
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.time.Duration
import java.util.concurrent.atomic.AtomicInteger

class EnumCounterReporter(
    private val counter: EnumCounterRegistry,
    initialDelay: Duration = Duration.ofMinutes(3),
    watchInterval: Duration = Duration.ofSeconds(10),
    private val conf: ImmutableConfig
): GracefulScheduledExecutor(initialDelay, watchInterval) {
    private var logger = LoggerFactory.getLogger(EnumCounterReporter::class.java)
    private var lastStatus = ""
    private val tick = AtomicInteger()

    fun outputTo(logger: Logger) {
        this.logger = logger
    }

    override fun run() {
        if (!AppContext.isActive) {
            close()
            return
        }

        if (tick.getAndIncrement() == 0) {
            reportStarted()
        }

        val status = counter.getStatus(true)
        if (status.isNotEmpty() && status != lastStatus) {
            logger.info(status)
            lastStatus = status
        }
    }

    private fun reportStarted() {
        logger.info("Counter reporter is started at {}", DateTimes.now())
        report()
    }

    private fun report() {
        val counterNames = counter.registeredCounters.map { readableClassName(it) }
        if (counterNames.isNotEmpty()) {
            logger.info(counterNames.joinToString(", ", "Registered counters : "))
        } else {
            logger.info("No counter is registered")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy