xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/util/Statistics.java (revision ef6b5de24264d979d0f4e0ef5515d20f1309837b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.util;
24 
25 import io.micrometer.core.instrument.MeterRegistry;
26 import io.micrometer.core.instrument.Timer;
27 import org.opengrok.indexer.Metrics;
28 
29 import java.time.Duration;
30 import java.time.Instant;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 
34 /**
35  * This class is handy for logging messages (and updating metrics)
36  * about duration of a task.
37  */
38 public class Statistics {
39 
40     private final Instant startTime;
41 
Statistics()42     public Statistics() {
43       startTime = Instant.now();
44   }
45 
logIt(Logger logger, Level logLevel, String msg, Duration duration)46     private void logIt(Logger logger, Level logLevel, String msg, Duration duration) {
47         String timeStr = StringUtils.getReadableTime(duration.toMillis());
48         logger.log(logLevel, msg + " (took {0})", timeStr);
49     }
50 
51     /**
52      * Log a message along with how much time it took since the constructor was called.
53      * @param logger logger instance
54      * @param logLevel log level
55      * @param msg message string
56      */
report(Logger logger, Level logLevel, String msg)57     public void report(Logger logger, Level logLevel, String msg) {
58         logIt(logger, logLevel, msg, Duration.between(startTime, Instant.now()));
59     }
60 
61     /**
62      * Log a message along with how much time it took since the constructor was called.
63      * If there is a metrics registry, it will update the timer specified by the meter name.
64      * @param logger logger instance
65      * @param logLevel log level
66      * @param msg message string
67      * @param meterName name of the meter
68      * @see Metrics#getRegistry()
69      */
report(Logger logger, Level logLevel, String msg, String meterName)70     public void report(Logger logger, Level logLevel, String msg, String meterName) {
71         report(logger, logLevel, msg, meterName, new String[]{});
72     }
73 
74     /**
75      * Log a message along with how much time it took since the constructor was called.
76      * If there is a metrics registry, it will update the timer specified by the meter name.
77      * @param logger logger instance
78      * @param logLevel log level
79      * @param msg message string
80      * @param meterName name of the meter
81      * @param tags array of tags for the meter
82      * @see Metrics#getRegistry()
83      */
report(Logger logger, Level logLevel, String msg, String meterName, String[] tags)84     public void report(Logger logger, Level logLevel, String msg, String meterName, String[] tags) {
85         Duration duration = Duration.between(startTime, Instant.now());
86 
87         logIt(logger, logLevel, msg, duration);
88 
89         MeterRegistry registry = Metrics.getRegistry();
90         if (registry != null) {
91             Timer.builder(meterName).
92                     tags(tags).
93                     register(registry).
94                     record(duration);
95         }
96     }
97 
98     /**
99      * log a message along with how much time it took since the constructor was called.
100      * If there is a metrics registry, it will update the timer specified by the meter name.
101      * The log level is {@code INFO}.
102      * @param logger logger instance
103      * @param msg message string
104      * @param meterName name of the meter
105      */
report(Logger logger, String msg, String meterName)106     public void report(Logger logger, String msg, String meterName) {
107         report(logger, Level.INFO, msg, meterName);
108     }
109 
110     /**
111      * log a message along with how much time it took since the constructor was called.
112      * The log level is {@code INFO}.
113      * @param logger logger instance
114      * @param msg message string
115      */
report(Logger logger, String msg)116     public void report(Logger logger, String msg) {
117         report(logger, Level.INFO, msg);
118     }
119 }
120