This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sets trace ID in log entries correctly (#683)
Adds a new logging enhancer that sets the right field for the log entries to be linkable from a trace, in the Google Cloud Console. Also updates Trace code sample to show this capability. Fixes #664
- Loading branch information
1 parent
72d891b
commit 0175ff6
Showing
12 changed files
with
119 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...main/java/org/springframework/cloud/gcp/autoconfigure/logging/TraceIdLoggingEnhancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2018 original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.gcp.autoconfigure.logging; | ||
|
||
import com.google.cloud.logging.LogEntry; | ||
import com.google.cloud.logging.LoggingEnhancer; | ||
|
||
/** | ||
* Adds the trace ID to the logging entry, in its correct format to be displayed in the Logs viewer. | ||
* | ||
* @author João André Martins | ||
*/ | ||
public class TraceIdLoggingEnhancer implements LoggingEnhancer { | ||
|
||
private static final ThreadLocal<String> traceId = new ThreadLocal<>(); | ||
|
||
public static void setCurrentTraceId(String projectId, String id) { | ||
if (id == null) { | ||
traceId.remove(); | ||
} | ||
else { | ||
traceId.set("projects/" + projectId + "/traces/" + id); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the trace ID in the "projects/[MY_PROJECT_ID]/traces/[MY_TRACE_ID]". | ||
* @return the trace ID in the "projects/[MY_PROJECT_ID]/traces/[MY_TRACE_ID]" | ||
*/ | ||
public static String getCurrentTraceId() { | ||
return traceId.get(); | ||
} | ||
|
||
@Override | ||
public void enhanceLogEntry(LogEntry.Builder builder) { | ||
String traceId = getCurrentTraceId(); | ||
if (traceId != null) { | ||
builder.setTrace(traceId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
spring-cloud-gcp-samples/spring-cloud-gcp-trace-sample/src/main/resources/logback-spring.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" /> | ||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> | ||
<include resource="org/springframework/boot/logging/logback/console-appender.xml" /> | ||
|
||
<root level="INFO"> | ||
<!-- If running in GCP, remove the CONSOLE appender otherwise logs will be duplicated. --> | ||
<appender-ref ref="CONSOLE"/> | ||
<appender-ref ref="STACKDRIVER" /> | ||
</root> | ||
</configuration> |