diff options
author | Stone, Avi (as206k) <as206k@att.com> | 2018-04-12 15:12:44 +0300 |
---|---|---|
committer | Stone, Avi (as206k) <as206k@att.com> | 2018-04-12 15:16:30 +0300 |
commit | 438bdef0d2473a4db83aac3d71d4596b223879d7 (patch) | |
tree | c6cd021d4b64d83c435ae07143ee4aa1ae0cfd6b /src/main/java/org/onap/sdc/common/onaplog/Stopwatch.java | |
parent | a5e73f1e0597834ba46754aaae4683f7acf06e47 (diff) |
DCAE-D property initial commit
DCAE-D property initial commit
Change-Id: I5ba79eddaa1732524e30652b679e4dd5dfed56b5
Issue-ID: SDC-1218
Signed-off-by: Stone, Avi (as206k) <as206k@att.com>
Diffstat (limited to 'src/main/java/org/onap/sdc/common/onaplog/Stopwatch.java')
-rw-r--r-- | src/main/java/org/onap/sdc/common/onaplog/Stopwatch.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/org/onap/sdc/common/onaplog/Stopwatch.java b/src/main/java/org/onap/sdc/common/onaplog/Stopwatch.java new file mode 100644 index 0000000..de56853 --- /dev/null +++ b/src/main/java/org/onap/sdc/common/onaplog/Stopwatch.java @@ -0,0 +1,59 @@ +package org.onap.sdc.common.onaplog; + +import org.onap.sdc.common.onaplog.interfaces.IOnapLogConfiguration; +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; + +/** + * Created by dd4296 on 12/13/2017. + * 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(IOnapLogConfiguration.MDC_BEGIN_TIMESTAMP) == null || MDC.get(IOnapLogConfiguration.MDC_BEGIN_TIMESTAMP).trim().length() == 0) { + MDC.put(IOnapLogConfiguration.MDC_BEGIN_TIMESTAMP, generatedTimeNow()); + } + } + + public void stop() { + if (MDC.get(IOnapLogConfiguration.MDC_BEGIN_TIMESTAMP) == null) { + log.error("call to stop without calling start first, this is not compliant with EELF format"); + } + MDC.put(IOnapLogConfiguration.MDC_END_TIMESTAMP, generatedTimeNow()); + setElapsedTime(); + } + + private void setElapsedTime() { + + try { + + final LocalDateTime startTime = LocalDateTime.parse(MDC.get(IOnapLogConfiguration.MDC_BEGIN_TIMESTAMP)); + final LocalDateTime endTime = LocalDateTime.parse(MDC.get(IOnapLogConfiguration.MDC_END_TIMESTAMP)); + + final Duration timeDifference = Duration.between(startTime, endTime); + + MDC.put(IOnapLogConfiguration.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())); + } + +}
\ No newline at end of file |