Skip to content

Commit

Permalink
Logging: set Trace in trace instead of label (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamlin authored Jun 5, 2018
1 parent 3db8085 commit 5c41d41
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TraceLoggingEnhancer(String prefix) {
/**
* Set the Trace ID associated with any logging done by the current thread.
*
* @param id The traceID
* @param id The traceID, in the form projects/[PROJECT_ID]/traces/[TRACE_ID]
*/
public static void setCurrentTraceId(String id) {
if (id == null) {
Expand All @@ -58,7 +58,7 @@ public static String getCurrentTraceId() {
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) {
String traceId = getCurrentTraceId();
if (traceId != null) {
builder.addLabel(traceIdLabel, traceId);
builder.setTrace(traceId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ public class LoggingHandlerTest {
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
.setTimestamp(123456789L)
.build();
private static final LogEntry TRACE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
.setSeverity(Severity.DEBUG)
.addLabel("levelName", "FINEST")
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
.setTrace("projects/projectId/traces/traceId")
.setTimestamp(123456789L)
.build();
private static final String CONFIG_NAMESPACE = "com.google.cloud.logging.LoggingHandler";
private static final ImmutableMap<String, String> CONFIG_MAP =
ImmutableMap.<String, String>builder()
Expand Down Expand Up @@ -314,6 +321,31 @@ public void enhanceLogEntry(Builder builder) {
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
}

@Test
public void testTraceEnhancedLogEntry() {
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
expect(options.getService()).andReturn(logging);
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
logging.setFlushSeverity(Severity.ERROR);
expectLastCall().once();
logging.setWriteSynchronicity(Synchronicity.ASYNC);
expectLastCall().once();
logging.write(
ImmutableList.of(TRACE_ENTRY),
WriteOption.logName(LOG_NAME),
WriteOption.resource(resource),
WriteOption.labels(BASE_SEVERITY_MAP));
expectLastCall().once();
replay(options, logging);
LoggingEnhancer enhancer = new TraceLoggingEnhancer();
TraceLoggingEnhancer.setCurrentTraceId("projects/projectId/traces/traceId");
Handler handler =
new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
handler.setLevel(Level.ALL);
handler.setFormatter(new TestFormatter());
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
}

@Test
public void testReportWriteError() {
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
Expand Down

0 comments on commit 5c41d41

Please sign in to comment.