1 package org.openecomp.sdc.common.ecomplog;
3 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_BEGIN_TIMESTAMP;
4 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_ELAPSED_TIME;
5 import static org.openecomp.sdc.common.ecomplog.api.IEcompLogConfiguration.MDC_END_TIMESTAMP;
7 import java.time.Clock;
8 import java.time.Duration;
9 import java.time.LocalDateTime;
11 import org.openecomp.sdc.common.ecomplog.api.IStopWatch;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
17 * Created by dd4296 on 12/13/2017.
18 * this is local implementation of the stopwatch class from EELF standard with the same interface
19 * can be replaced if needed with EELF lib
21 public class Stopwatch implements IStopWatch {
23 private static Logger log = LoggerFactory.getLogger(Stopwatch.class.getName());
29 if (MDC.get(MDC_BEGIN_TIMESTAMP) == null || MDC.get(MDC_BEGIN_TIMESTAMP).trim().length() == 0)
30 MDC.put(MDC_BEGIN_TIMESTAMP, generatedTimeNow());
34 if (MDC.get(MDC_BEGIN_TIMESTAMP) == null) {
35 log.error("call to stop without calling start first, this is not compliant with EELF format");
37 MDC.put(MDC_END_TIMESTAMP, generatedTimeNow());
41 private void setElapsedTime() {
45 final LocalDateTime startTime = LocalDateTime.parse(MDC.get(MDC_BEGIN_TIMESTAMP));
46 final LocalDateTime endTime = LocalDateTime.parse(MDC.get(MDC_END_TIMESTAMP));
48 final Duration timeDifference = Duration.between(startTime, endTime);
50 MDC.put(MDC_ELAPSED_TIME, String.valueOf(timeDifference.toMillis()));
52 } catch(Exception ex) {
53 log.error("failed to calculate elapsed time",ex);
57 private String generatedTimeNow() {
58 return String.valueOf(LocalDateTime.now(Clock.systemUTC()));