@@ -25,6 +25,7 @@ public TraceLoggingEnhancer(String prefix) {}
25
25
26
26
private static final ThreadLocal <String > traceId = new ThreadLocal <>();
27
27
private static final ThreadLocal <String > spanId = new ThreadLocal <>();
28
+ private static final ThreadLocal <Boolean > traceSampled = new ThreadLocal <Boolean >();
28
29
29
30
/**
30
31
* Set the Trace ID associated with any logging done by the current thread.
@@ -52,6 +53,19 @@ public static void setCurrentSpanId(String id) {
52
53
}
53
54
}
54
55
56
+ /**
57
+ * Set the trace sampled flag associated with any logging done by the current thread.
58
+ *
59
+ * @param isTraceSampled The traceSampled flag
60
+ */
61
+ public static void setCurrentTraceSampled (Boolean isTraceSampled ) {
62
+ if (isTraceSampled == null ) {
63
+ traceSampled .remove ();
64
+ } else {
65
+ traceSampled .set (isTraceSampled );
66
+ }
67
+ }
68
+
55
69
/**
56
70
* Get the Trace ID associated with any logging done by the current thread.
57
71
*
@@ -70,6 +84,15 @@ public static String getCurrentSpanId() {
70
84
return spanId .get ();
71
85
}
72
86
87
+ /**
88
+ * Get the trace sampled flag associated with any logging done by the current thread.
89
+ *
90
+ * @return traceSampled The traceSampled flag
91
+ */
92
+ public static Boolean getCurrentTraceSampled () {
93
+ return traceSampled .get ();
94
+ }
95
+
73
96
@ Override
74
97
public void enhanceLogEntry (LogEntry .Builder builder ) {
75
98
String traceId = getCurrentTraceId ();
@@ -80,5 +103,9 @@ public void enhanceLogEntry(LogEntry.Builder builder) {
80
103
if (spanId != null ) {
81
104
builder .setSpanId (spanId );
82
105
}
106
+ Boolean isTraceSampled = getCurrentTraceSampled ();
107
+ if (isTraceSampled != null ) {
108
+ builder .setTraceSampled (isTraceSampled );
109
+ }
83
110
}
84
111
}
0 commit comments