|
| 1 | +package tech.ydb.jdbc; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.ThreadLocalRandom; |
| 7 | +import java.util.logging.Level; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +/** |
| 13 | + * |
| 14 | + * @author Aleksandr Gorshenin |
| 15 | + */ |
| 16 | +public class YdbTracer { |
| 17 | + private static final Logger LOGGER = Logger.getLogger(YdbTracer.class.getName()); |
| 18 | + private static final ThreadLocal<YdbTracer> LOCAL = new ThreadLocal<>(); |
| 19 | + |
| 20 | + private final Date startDate = new Date(); |
| 21 | + private final long startedAt = System.currentTimeMillis(); |
| 22 | + private final List<Record> records = new ArrayList<>(); |
| 23 | + |
| 24 | + private String txID = null; |
| 25 | + private boolean isMarked = false; |
| 26 | + private boolean isClosed = false; |
| 27 | + |
| 28 | + private class Record { |
| 29 | + private final long executedAt = System.currentTimeMillis(); |
| 30 | + private final String type; |
| 31 | + private final String comment; |
| 32 | + |
| 33 | + public Record(String type, String comment) { |
| 34 | + this.type = type; |
| 35 | + this.comment = comment; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static void clear() { |
| 40 | + LOCAL.remove(); |
| 41 | + } |
| 42 | + |
| 43 | + public static YdbTracer current() { |
| 44 | + YdbTracer tracer = LOCAL.get(); |
| 45 | + if (tracer == null || tracer.isClosed) { |
| 46 | + tracer = new YdbTracer(); |
| 47 | + LOCAL.set(tracer); |
| 48 | + } |
| 49 | + |
| 50 | + return tracer; |
| 51 | + } |
| 52 | + |
| 53 | + public void trace(String type, String comment) { |
| 54 | + records.add(new Record(type, comment)); |
| 55 | + } |
| 56 | + |
| 57 | + public void setId(String id) { |
| 58 | + this.txID = id; |
| 59 | + trace("SET ID", id); |
| 60 | + } |
| 61 | + |
| 62 | + public void markToPrint() { |
| 63 | + this.isMarked = true; |
| 64 | + trace("MARK TO PRINT", ""); |
| 65 | + } |
| 66 | + |
| 67 | + public void close() { |
| 68 | + isClosed = true; |
| 69 | + |
| 70 | + LOCAL.remove(); |
| 71 | + |
| 72 | + final Level level = isMarked ? Level.INFO : Level.FINE; |
| 73 | + if (!LOGGER.isLoggable(level) || records.isEmpty()) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + long finishedAt = System.currentTimeMillis(); |
| 78 | + |
| 79 | + final String id = txID != null ? txID : "UKNOWN-" + ThreadLocalRandom.current().nextLong(); |
| 80 | + LOGGER.log(level, "Trace[{0}] started at {1}", new Object[] { id, startDate }); |
| 81 | + long last = startedAt; |
| 82 | + for (Record record: records) { |
| 83 | + long ms = record.executedAt - last; |
| 84 | + LOGGER.log(level, "Trace[{0}] {1} ms {2}: {3}", new Object[] { id, ms, record.type, record.comment }); |
| 85 | + last = record.executedAt; |
| 86 | + } |
| 87 | + LOGGER.log(level, "Trace[{0}] finished in {1} ms", new Object[] { id, finishedAt - startedAt }); |
| 88 | + } |
| 89 | +} |
0 commit comments