From d295ff96ce430eb1db065eb29e362a72729e4dfb Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Tue, 22 May 2018 13:47:18 -0700 Subject: [PATCH 1/3] set trace in trace instead of label --- .../java/com/google/cloud/logging/TraceLoggingEnhancer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java b/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java index d659fefff8ef..3d59ef5a4d52 100644 --- a/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java +++ b/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java @@ -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); } } } From 2d1860c0a9ea7d051248264918b4fac72c99dd9d Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Tue, 22 May 2018 15:08:44 -0700 Subject: [PATCH 2/3] add a line to the logging test --- .../java/com/google/cloud/logging/LoggingHandlerTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 16985a4881df..8a3d9cbcf036 100644 --- a/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -191,6 +191,13 @@ public void enhanceLogEntry(LogEntry.Builder builder) { } } + static final class TestLoggingEnhancerTrace implements LoggingEnhancer { + @Override + public void enhanceLogEntry(LogEntry.Builder builder) { + builder.setTrace("projects/projectId/traces/traceId"); + } + } + @Before public void setUp() { logging = EasyMock.createMock(Logging.class); @@ -305,6 +312,7 @@ public void testEnhancedLogEntry() { @Override public void enhanceLogEntry(Builder builder) { builder.addLabel("enhanced", "true"); + builder.setTrace("projects/projectId/traces/traceId"); } }; Handler handler = From da8452d3e7259b0ac6e450c52f025432f518fee3 Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Tue, 22 May 2018 17:03:16 -0700 Subject: [PATCH 3/3] made test for logging trace --- .../cloud/logging/TraceLoggingEnhancer.java | 2 +- .../cloud/logging/LoggingHandlerTest.java | 40 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java b/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java index 3d59ef5a4d52..c7b7308fa5e8 100644 --- a/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java +++ b/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java @@ -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) { diff --git a/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 8a3d9cbcf036..638e8bf28468 100644 --- a/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-clients/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -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 CONFIG_MAP = ImmutableMap.builder() @@ -191,13 +198,6 @@ public void enhanceLogEntry(LogEntry.Builder builder) { } } - static final class TestLoggingEnhancerTrace implements LoggingEnhancer { - @Override - public void enhanceLogEntry(LogEntry.Builder builder) { - builder.setTrace("projects/projectId/traces/traceId"); - } - } - @Before public void setUp() { logging = EasyMock.createMock(Logging.class); @@ -312,7 +312,6 @@ public void testEnhancedLogEntry() { @Override public void enhanceLogEntry(Builder builder) { builder.addLabel("enhanced", "true"); - builder.setTrace("projects/projectId/traces/traceId"); } }; Handler handler = @@ -322,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.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();