Skip to content

Commit d98051c

Browse files
Simon Zeltseryoshi-code-bot
andauthored
feat: adds support to group logs of the same http request (#257)
* feat: adds support to group logs of the same http request With TraceLoggingEventEnhancer users can set the traceId of the incoming request and then all the logs that will be created on the same thread will have the traceId property set to that traceId. This allows Cloud Logging to group log entries from the same request in the UI for easier troubleshooting * feat: adds support to group logs of the same http request With TraceLoggingEventEnhancer users can set the traceId of the incoming request and then all the logs that will be created on the same thread will have the traceId property set to that traceId. This allows Cloud Logging to group log entries from the same request in the UI for easier troubleshooting * Fix comment typo * Update src/main/java/com/google/cloud/logging/logback/TraceLoggingEventEnhancer.java Co-authored-by: yoshi-code-bot <[email protected]> Co-authored-by: yoshi-code-bot <[email protected]>
1 parent d9856f6 commit d98051c

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging.logback;
18+
19+
import ch.qos.logback.classic.spi.ILoggingEvent;
20+
import com.google.cloud.logging.LogEntry;
21+
import org.slf4j.MDC;
22+
23+
/** Adds support for grouping logs by incoming http request */
24+
public class TraceLoggingEventEnhancer implements LoggingEventEnhancer {
25+
26+
// A key used by Cloud Logging for trace Id
27+
private static final String TRACE_ID = "logging.googleapis.trace";
28+
29+
/**
30+
* Set the Trace ID associated with any logging done by the current thread.
31+
*
32+
* @param id The traceID, in the form projects/[PROJECT_ID]/traces/[TRACE_ID]
33+
*/
34+
public static void setCurrentTraceId(String id) {
35+
MDC.put(TRACE_ID, id);
36+
}
37+
38+
/**
39+
* Get the Trace ID associated with any logging done by the current thread.
40+
*
41+
* @return id The traceID
42+
*/
43+
public static String getCurrentTraceId() {
44+
return MDC.get(TRACE_ID);
45+
}
46+
47+
@Override
48+
public void enhanceLogEntry(LogEntry.Builder builder, ILoggingEvent e) {
49+
Object value = e.getMDCPropertyMap().get(TRACE_ID);
50+
String traceId = value != null ? value.toString() : null;
51+
if (traceId != null) {
52+
builder.setTrace(traceId);
53+
}
54+
}
55+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging.logback;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import ch.qos.logback.classic.spi.LoggingEvent;
22+
import com.google.cloud.logging.LogEntry;
23+
import com.google.cloud.logging.Payload.StringPayload;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class TraceLoggingEventEnhancerTest {
28+
private TraceLoggingEventEnhancer classUnderTest;
29+
30+
@Before
31+
public void setUp() {
32+
classUnderTest = new TraceLoggingEventEnhancer();
33+
}
34+
35+
@Test
36+
public void testEnhanceLogEntry() {
37+
// setup
38+
String traceId = "abc";
39+
TraceLoggingEventEnhancer.setCurrentTraceId(traceId);
40+
LoggingEvent loggingEvent = new LoggingEvent();
41+
loggingEvent.setMessage("this is a test");
42+
LogEntry.Builder builder = LogEntry.newBuilder(StringPayload.of("this is a test"));
43+
44+
// act
45+
classUnderTest.enhanceLogEntry(builder, loggingEvent);
46+
LogEntry logEntry = builder.build();
47+
48+
// assert - Trace Id should be recorded as explicit Trace field, not as a label
49+
assertThat(traceId.equalsIgnoreCase(logEntry.getTrace()));
50+
}
51+
52+
@Test
53+
public void testGetCurrentTraceId() {
54+
// setup
55+
String traceId = "abc";
56+
TraceLoggingEventEnhancer.setCurrentTraceId(traceId);
57+
58+
// act
59+
String currentTraceId = TraceLoggingEventEnhancer.getCurrentTraceId();
60+
61+
// assert
62+
assertThat(traceId.equalsIgnoreCase(currentTraceId));
63+
}
64+
}

0 commit comments

Comments
 (0)