blob: 3c6c0daceaa872b59e615e0548708e2233a3d802 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package org.onap.sdc.common.onaplog;
import org.onap.sdc.common.onaplog.interfaces.IStopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
/**
* this is local implementation of the stopwatch class from EELF standard with the same interface
* can be replaced if needed with EELF lib
*/
public class Stopwatch implements IStopWatch {
private static final Logger log = LoggerFactory.getLogger(Stopwatch.class.getName());
Stopwatch() {
}
public void start() {
if (MDC.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP) == null || MDC.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP).trim().length() == 0) {
MDC.put(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP, generatedTimeNow());
}
}
public void stop() {
if (MDC.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP) == null) {
log.error("call to stop without calling start first, this is not compliant with EELF format");
}
MDC.put(OnapLogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow());
setElapsedTime();
}
private void setElapsedTime() {
try {
final LocalDateTime startTime = LocalDateTime.parse(MDC.get(OnapLogConfiguration.MDC_BEGIN_TIMESTAMP));
final LocalDateTime endTime = LocalDateTime.parse(MDC.get(OnapLogConfiguration.MDC_END_TIMESTAMP));
final Duration timeDifference = Duration.between(startTime, endTime);
MDC.put(OnapLogConfiguration.MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));
} catch(Exception ex) {
log.error("failed to calculate elapsed time",ex);
}
}
private String generatedTimeNow() {
return String.valueOf(LocalDateTime.now(Clock.systemUTC()));
}
}
|