Skip to content

Commit 246c1f5

Browse files
author
cindy-peng
committed
Add logging handler test for traceEnhancer
1 parent ab65496 commit 246c1f5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public TraceLoggingEnhancer(String prefix) {}
2525

2626
private static final ThreadLocal<String> traceId = new ThreadLocal<>();
2727
private static final ThreadLocal<String> spanId = new ThreadLocal<>();
28+
private static final ThreadLocal<Boolean> traceSampled = new ThreadLocal<Boolean>();
2829

2930
/**
3031
* Set the Trace ID associated with any logging done by the current thread.
@@ -52,6 +53,19 @@ public static void setCurrentSpanId(String id) {
5253
}
5354
}
5455

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+
5569
/**
5670
* Get the Trace ID associated with any logging done by the current thread.
5771
*
@@ -70,6 +84,15 @@ public static String getCurrentSpanId() {
7084
return spanId.get();
7185
}
7286

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+
7396
@Override
7497
public void enhanceLogEntry(LogEntry.Builder builder) {
7598
String traceId = getCurrentTraceId();
@@ -80,5 +103,9 @@ public void enhanceLogEntry(LogEntry.Builder builder) {
80103
if (spanId != null) {
81104
builder.setSpanId(spanId);
82105
}
106+
Boolean isTraceSampled = getCurrentTraceSampled();
107+
if (isTraceSampled != null) {
108+
builder.setTraceSampled(isTraceSampled);
109+
}
83110
}
84111
}

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public class LoggingHandlerTest {
165165
.addLabel("levelName", "FINEST")
166166
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
167167
.setTrace("projects/projectId/traces/traceId")
168+
.setSpanId("test_span_id")
169+
.setTraceSampled(true)
168170
.setTimestamp(123456789L)
169171
.build();
170172
private static final LogEntry DIAGNOSTIC_ENTRY =
@@ -454,6 +456,8 @@ public void testTraceEnhancedLogEntry() {
454456
replay(options, logging);
455457
LoggingEnhancer enhancer = new TraceLoggingEnhancer();
456458
TraceLoggingEnhancer.setCurrentTraceId("projects/projectId/traces/traceId");
459+
TraceLoggingEnhancer.setCurrentSpanId("test_span_id");
460+
TraceLoggingEnhancer.setCurrentTraceSampled(true);
457461
Handler handler =
458462
new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE, ImmutableList.of(enhancer));
459463
handler.setLevel(Level.ALL);

0 commit comments

Comments
 (0)