This repository was archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 691
Sets trace ID in log entries correctly #683
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3a0476
Sets trace ID in log entries correctly
joaoandremartins d5dfd82
Fixes integration tests
joaoandremartins 5d3159e
Makes TraceIdLoggingEnhancer GcpProjectIdProvider aware
joaoandremartins 9d68180
Fixes Javadoc
joaoandremartins fe02f12
Merge branch 'master' into gh-664-trace-id
joaoandremartins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really the only addition here. The rest is just putting sentences in the same line.