From 4c11217ddac0aef53b5e9dd7fc6fa71a6bc9a7c6 Mon Sep 17 00:00:00 2001 From: redkale Date: Thu, 6 Jul 2023 08:11:52 +0800 Subject: [PATCH] =?UTF-8?q?Traceid=E7=94=9F=E6=88=90=E8=A7=84=E5=88=99?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/META-INF/application-template.xml | 1 - .../java/org/redkale/boot/Application.java | 5 +- src/main/java/org/redkale/util/Traces.java | 46 ++++++++++++++++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/java/META-INF/application-template.xml b/src/main/java/META-INF/application-template.xml index 09c81072e..2dc0aabd3 100644 --- a/src/main/java/META-INF/application-template.xml +++ b/src/main/java/META-INF/application-template.xml @@ -116,7 +116,6 @@ 默认置入的system.property.的有: System.setProperty("redkale.convert.pool.size", "128"); System.setProperty("redkale.convert.writer.buffer.defsize", "4096"); - System.setProperty("redkale.trace.enable", "false"); 节点下也可包含非节点. 非其节点可以通过@Resource(name="properties.xxxxxx")进行注入, 被注解的字段类型只能是AnyValue、AnyValue[] diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java index 708101e31..cff2e89d7 100644 --- a/src/main/java/org/redkale/boot/Application.java +++ b/src/main/java/org/redkale/boot/Application.java @@ -310,10 +310,7 @@ public final class Application { if (System.getProperty("redkale.convert.writer.buffer.defsize") == null) { System.setProperty("redkale.convert.writer.buffer.defsize", "4096"); } - if (System.getProperty("redkale.trace.enable") == null) { - System.setProperty("redkale.trace.enable", "false"); - } - + System.setProperty("redkale.version", Redkale.getDotedVersion()); int nid = config.getIntValue("nodeid", 0); this.nodeid = nid; diff --git a/src/main/java/org/redkale/util/Traces.java b/src/main/java/org/redkale/util/Traces.java index e6553189f..781002159 100644 --- a/src/main/java/org/redkale/util/Traces.java +++ b/src/main/java/org/redkale/util/Traces.java @@ -18,9 +18,16 @@ public class Traces { private static final boolean enable = Boolean.getBoolean("redkale.trace.enable"); + private static final String PROCESS_ID = UUID.randomUUID().toString().replaceAll("-", ""); + + private static final ThreadLocal THREAD_SEQUENCE = ThreadLocal.withInitial(IdSequence::new); + private static final ThreadLocal localTrace = new ThreadLocal<>(); - private static final Supplier tidSupplier = () -> UUID.randomUUID().toString().replace("-", ""); + //借用Skywalking的GlobalIdGenerator生成ID的规则 + private static final Supplier tidSupplier = () -> PROCESS_ID + + "." + Thread.currentThread().getId() + + "." + THREAD_SEQUENCE.get().nextSeq(); public static boolean enable() { return enable; @@ -46,4 +53,41 @@ public class Traces { return requestTraceid; } + private static class IdSequence { + + private long lastTimestamp; + + private short threadSeq; + + private long lastShiftTimestamp; + + private int lastShiftValue; + + public IdSequence() { + this.lastTimestamp = System.currentTimeMillis(); + } + + public long nextSeq() { + long rs = timestamp() * 10000; + if (threadSeq == 10000) { + threadSeq = 0; + } + return rs + threadSeq++; + } + + private long timestamp() { + long currentTimeMillis = System.currentTimeMillis(); + if (currentTimeMillis < lastTimestamp) { + // Just for considering time-shift-back by Ops or OS. @hanahmily 's suggestion. + if (lastShiftTimestamp != currentTimeMillis) { + lastShiftValue++; + lastShiftTimestamp = currentTimeMillis; + } + return lastShiftValue; + } else { + lastTimestamp = currentTimeMillis; + return lastTimestamp; + } + } + } }