Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging: set Trace in trace instead of label #3301

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

*/
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