1b5840353SAdam Hornáček /* 2b5840353SAdam Hornáček * CDDL HEADER START 3b5840353SAdam Hornáček * 4b5840353SAdam Hornáček * The contents of this file are subject to the terms of the 5b5840353SAdam Hornáček * Common Development and Distribution License (the "License"). 6b5840353SAdam Hornáček * You may not use this file except in compliance with the License. 7b5840353SAdam Hornáček * 8b5840353SAdam Hornáček * See LICENSE.txt included in this distribution for the specific 9b5840353SAdam Hornáček * language governing permissions and limitations under the License. 10b5840353SAdam Hornáček * 11b5840353SAdam Hornáček * When distributing Covered Code, include this CDDL HEADER in each 12b5840353SAdam Hornáček * file and include the License file at LICENSE.txt. 13b5840353SAdam Hornáček * If applicable, add the following below this CDDL HEADER, with the 14b5840353SAdam Hornáček * fields enclosed by brackets "[]" replaced with your own identifying 15b5840353SAdam Hornáček * information: Portions Copyright [yyyy] [name of copyright owner] 16b5840353SAdam Hornáček * 17b5840353SAdam Hornáček * CDDL HEADER END 18b5840353SAdam Hornáček */ 19b5840353SAdam Hornáček 20b5840353SAdam Hornáček /* 21*ef6b5de2SVladimir Kotal * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved. 22b5840353SAdam Hornáček */ 239805b761SAdam Hornáček package org.opengrok.indexer.util; 24b5840353SAdam Hornáček 250d7ace53SVladimir Kotal import io.micrometer.core.instrument.MeterRegistry; 260d7ace53SVladimir Kotal import io.micrometer.core.instrument.Timer; 270d7ace53SVladimir Kotal import org.opengrok.indexer.Metrics; 280d7ace53SVladimir Kotal 2920c879e0SVladimir Kotal import java.time.Duration; 3020c879e0SVladimir Kotal import java.time.Instant; 31b5840353SAdam Hornáček import java.util.logging.Level; 32b5840353SAdam Hornáček import java.util.logging.Logger; 33b5840353SAdam Hornáček 345752d8baSVladimir Kotal /** 355752d8baSVladimir Kotal * This class is handy for logging messages (and updating metrics) 365752d8baSVladimir Kotal * about duration of a task. 375752d8baSVladimir Kotal */ 38b5840353SAdam Hornáček public class Statistics { 39b5840353SAdam Hornáček 4020c879e0SVladimir Kotal private final Instant startTime; 41b5840353SAdam Hornáček Statistics()42b5840353SAdam Hornáček public Statistics() { 4320c879e0SVladimir Kotal startTime = Instant.now(); 44b5840353SAdam Hornáček } 45b5840353SAdam Hornáček logIt(Logger logger, Level logLevel, String msg, Duration duration)460d7ace53SVladimir Kotal private void logIt(Logger logger, Level logLevel, String msg, Duration duration) { 470d7ace53SVladimir Kotal String timeStr = StringUtils.getReadableTime(duration.toMillis()); 480d7ace53SVladimir Kotal logger.log(logLevel, msg + " (took {0})", timeStr); 490d7ace53SVladimir Kotal } 500d7ace53SVladimir Kotal 51eb6a09ccSVladimir Kotal /** 520d7ace53SVladimir Kotal * Log a message along with how much time it took since the constructor was called. 53eb6a09ccSVladimir Kotal * @param logger logger instance 54eb6a09ccSVladimir Kotal * @param logLevel log level 55eb6a09ccSVladimir Kotal * @param msg message string 56eb6a09ccSVladimir Kotal */ report(Logger logger, Level logLevel, String msg)57eb6a09ccSVladimir Kotal public void report(Logger logger, Level logLevel, String msg) { 580d7ace53SVladimir Kotal logIt(logger, logLevel, msg, Duration.between(startTime, Instant.now())); 590d7ace53SVladimir Kotal } 600d7ace53SVladimir Kotal 610d7ace53SVladimir Kotal /** 625752d8baSVladimir Kotal * Log a message along with how much time it took since the constructor was called. 635752d8baSVladimir Kotal * If there is a metrics registry, it will update the timer specified by the meter name. 640d7ace53SVladimir Kotal * @param logger logger instance 650d7ace53SVladimir Kotal * @param logLevel log level 660d7ace53SVladimir Kotal * @param msg message string 670d7ace53SVladimir Kotal * @param meterName name of the meter 685752d8baSVladimir Kotal * @see Metrics#getRegistry() 690d7ace53SVladimir Kotal */ report(Logger logger, Level logLevel, String msg, String meterName)700d7ace53SVladimir Kotal public void report(Logger logger, Level logLevel, String msg, String meterName) { 71*ef6b5de2SVladimir Kotal report(logger, logLevel, msg, meterName, new String[]{}); 72*ef6b5de2SVladimir Kotal } 73*ef6b5de2SVladimir Kotal 74*ef6b5de2SVladimir Kotal /** 75*ef6b5de2SVladimir Kotal * Log a message along with how much time it took since the constructor was called. 76*ef6b5de2SVladimir Kotal * If there is a metrics registry, it will update the timer specified by the meter name. 77*ef6b5de2SVladimir Kotal * @param logger logger instance 78*ef6b5de2SVladimir Kotal * @param logLevel log level 79*ef6b5de2SVladimir Kotal * @param msg message string 80*ef6b5de2SVladimir Kotal * @param meterName name of the meter 81*ef6b5de2SVladimir Kotal * @param tags array of tags for the meter 82*ef6b5de2SVladimir Kotal * @see Metrics#getRegistry() 83*ef6b5de2SVladimir Kotal */ report(Logger logger, Level logLevel, String msg, String meterName, String[] tags)84*ef6b5de2SVladimir Kotal public void report(Logger logger, Level logLevel, String msg, String meterName, String[] tags) { 850d7ace53SVladimir Kotal Duration duration = Duration.between(startTime, Instant.now()); 860d7ace53SVladimir Kotal 870d7ace53SVladimir Kotal logIt(logger, logLevel, msg, duration); 880d7ace53SVladimir Kotal 890d7ace53SVladimir Kotal MeterRegistry registry = Metrics.getRegistry(); 900d7ace53SVladimir Kotal if (registry != null) { 910d7ace53SVladimir Kotal Timer.builder(meterName). 92*ef6b5de2SVladimir Kotal tags(tags). 930d7ace53SVladimir Kotal register(registry). 940d7ace53SVladimir Kotal record(duration); 950d7ace53SVladimir Kotal } 960d7ace53SVladimir Kotal } 970d7ace53SVladimir Kotal 980d7ace53SVladimir Kotal /** 990d7ace53SVladimir Kotal * log a message along with how much time it took since the constructor was called. 1005752d8baSVladimir Kotal * If there is a metrics registry, it will update the timer specified by the meter name. 1010d7ace53SVladimir Kotal * The log level is {@code INFO}. 1020d7ace53SVladimir Kotal * @param logger logger instance 1030d7ace53SVladimir Kotal * @param msg message string 1040d7ace53SVladimir Kotal * @param meterName name of the meter 1050d7ace53SVladimir Kotal */ report(Logger logger, String msg, String meterName)1060d7ace53SVladimir Kotal public void report(Logger logger, String msg, String meterName) { 1070d7ace53SVladimir Kotal report(logger, Level.INFO, msg, meterName); 108b5840353SAdam Hornáček } 109b5840353SAdam Hornáček 110eb6a09ccSVladimir Kotal /** 111eb6a09ccSVladimir Kotal * log a message along with how much time it took since the constructor was called. 11220c879e0SVladimir Kotal * The log level is {@code INFO}. 113eb6a09ccSVladimir Kotal * @param logger logger instance 114eb6a09ccSVladimir Kotal * @param msg message string 115eb6a09ccSVladimir Kotal */ report(Logger logger, String msg)116eb6a09ccSVladimir Kotal public void report(Logger logger, String msg) { 117eb6a09ccSVladimir Kotal report(logger, Level.INFO, msg); 118eb6a09ccSVladimir Kotal } 119b5840353SAdam Hornáček } 120