Skip to content

Commit 5c41d41

Browse files
authored
Logging: set Trace in trace instead of label (#3301)
1 parent 3db8085 commit 5c41d41

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TraceLoggingEnhancer(String prefix) {
3535
/**
3636
* Set the Trace ID associated with any logging done by the current thread.
3737
*
38-
* @param id The traceID
38+
* @param id The traceID, in the form projects/[PROJECT_ID]/traces/[TRACE_ID]
3939
*/
4040
public static void setCurrentTraceId(String id) {
4141
if (id == null) {
@@ -58,7 +58,7 @@ public static String getCurrentTraceId() {
5858
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) {
5959
String traceId = getCurrentTraceId();
6060
if (traceId != null) {
61-
builder.addLabel(traceIdLabel, traceId);
61+
builder.setTrace(traceId);
6262
}
6363
}
6464
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public class LoggingHandlerTest {
131131
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
132132
.setTimestamp(123456789L)
133133
.build();
134+
private static final LogEntry TRACE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
135+
.setSeverity(Severity.DEBUG)
136+
.addLabel("levelName", "FINEST")
137+
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
138+
.setTrace("projects/projectId/traces/traceId")
139+
.setTimestamp(123456789L)
140+
.build();
134141
private static final String CONFIG_NAMESPACE = "com.google.cloud.logging.LoggingHandler";
135142
private static final ImmutableMap<String, String> CONFIG_MAP =
136143
ImmutableMap.<String, String>builder()
@@ -314,6 +321,31 @@ public void enhanceLogEntry(Builder builder) {
314321
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
315322
}
316323

324+
@Test
325+
public void testTraceEnhancedLogEntry() {
326+
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
327+
expect(options.getService()).andReturn(logging);
328+
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
329+
logging.setFlushSeverity(Severity.ERROR);
330+
expectLastCall().once();
331+
logging.setWriteSynchronicity(Synchronicity.ASYNC);
332+
expectLastCall().once();
333+
logging.write(
334+
ImmutableList.of(TRACE_ENTRY),
335+
WriteOption.logName(LOG_NAME),
336+
WriteOption.resource(resource),
337+
WriteOption.labels(BASE_SEVERITY_MAP));
338+
expectLastCall().once();
339+
replay(options, logging);
340+
LoggingEnhancer enhancer = new TraceLoggingEnhancer();
341+
TraceLoggingEnhancer.setCurrentTraceId("projects/projectId/traces/traceId");
342+
Handler handler =
343+
new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
344+
handler.setLevel(Level.ALL);
345+
handler.setFormatter(new TestFormatter());
346+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
347+
}
348+
317349
@Test
318350
public void testReportWriteError() {
319351
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();

0 commit comments

Comments
 (0)