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 693
Sets trace ID in log entries correctly #683
Merged
Merged
Changes from 2 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
54 changes: 54 additions & 0 deletions
54
...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,54 @@ | ||
/* | ||
* 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; | ||
|
||
import org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider; | ||
|
||
/** | ||
* 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 id) { | ||
if (id == null) { | ||
traceId.remove(); | ||
} | ||
else { | ||
traceId.set(id); | ||
} | ||
} | ||
|
||
public static String getTraceId() { | ||
return traceId.get(); | ||
} | ||
|
||
@Override | ||
public void enhanceLogEntry(LogEntry.Builder builder) { | ||
String projectId = new DefaultGcpProjectIdProvider().getProjectId(); | ||
String traceId = getTraceId(); | ||
if (traceId != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like if Just suggestion for minor optimization... |
||
builder.setTrace("projects/" + projectId + "/traces/" + 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,16 @@ | ||
= Spring Cloud GCP Trace Starter Example | ||
|
||
This sample application demonstrates using the | ||
link:../../spring-cloud-gcp-starters/spring-cloud-gcp-starter-trace[Spring Cloud GCP Trace Starter] in your code. | ||
This sample application demonstrates using the link:../../spring-cloud-gcp-starters/spring-cloud-gcp-starter-trace[Spring Cloud GCP Trace Starter] in your code. | ||
|
||
You can read about the Spring Boot Starter for Stackdriver Trace in more detail at the | ||
link:../../spring-cloud-gcp-docs/src/main/asciidoc/trace.adoc[Spring Cloud GCP Reference Document | ||
Stackdriver Trace section]. | ||
You can read about the Spring Boot Starter for Stackdriver Trace in more detail at the link:../../spring-cloud-gcp-docs/src/main/asciidoc/trace.adoc[Spring Cloud GCP Reference Document Stackdriver Trace section]. | ||
|
||
== Setup & Configuration | ||
1. Create a Google Cloud Platform Project | ||
1. https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account[Create a service account] | ||
with Trace Append permission. Furnish a new JSON key and then set the credentials using the | ||
`GOOGLE_APPLICATION_CREDENTIALS` environment variable or | ||
link:../../spring-cloud-gcp-starters/spring-cloud-gcp-starter/README.adoc[using GCP Starter Core properties]. | ||
2. https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account[Create a service account] with Trace Append permission. Furnish a new JSON key and then set the credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable or link:../../spring-cloud-gcp-starters/spring-cloud-gcp-starter/README.adoc[using GCP Starter Core properties]. | ||
+ | ||
Alternatively, if you have the https://cloud.google.com/sdk/[Google Cloud SDK] installed and | ||
initialized and are logged in with | ||
https://developers.google.com/identity/protocols/application-default-credentials[application | ||
default credentials], you can skip this step since Spring Cloud GCP will auto-discover those | ||
settings for you. | ||
Alternatively, if you have the https://cloud.google.com/sdk/[Google Cloud SDK] installed and initialized and are logged in with https://developers.google.com/identity/protocols/application-default-credentials[application default credentials], you can skip this step since Spring Cloud GCP will auto-discover those settings for you. | ||
|
||
1. Enable the https://console.cloud.google.com/apis/api/cloudtrace.googleapis.com/overview[Stackdriver Trace API] | ||
3. Enable the https://console.cloud.google.com/apis/api/cloudtrace.googleapis.com/overview[Stackdriver Trace API] | ||
|
||
== Run the Example | ||
Run the example from Maven: | ||
|
@@ -30,14 +20,12 @@ $ mvn spring-boot:run | |
---- | ||
|
||
Browse to `http://localhost:8080`. This will trigger the `ExampleController.work()` method, | ||
which in turn will call other services, and also a remote RESTful call to `ExampleController.meet()` | ||
method. | ||
which in turn will call other services, and also a remote RESTful call to `ExampleController.meet()` method. | ||
|
||
To see the traces, navigate to Stackdriver Trace console's | ||
https://console.cloud.google.com/traces/traces[Trace List] view. | ||
To see the traces, navigate to Stackdriver Trace console's https://console.cloud.google.com/traces/traces[Trace List] view. | ||
You should see the trace information in detail. | ||
Additionally, if you logged in with the Google Cloud SDK or have the `GOOGLE_CLOUD_PROJECT` environment variable set to your GCP project ID, you can also click the `View` link in front of the `Details` -> `Log` to view the log entries related to that trace ID. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
Note that the trace transmission delay default value is 10 seconds, so it can take a little | ||
while for the traces to show up in the Trace List page. | ||
Note that the trace transmission delay default value is 10 seconds, so it can take a little while for the traces to show up in the Trace List page. | ||
You can shorten this delay using the `spring.cloud.gcp.trace.scheduled-delay-seconds` property. | ||
|
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.
If
projectId
is empty ornull
, there is really no suitable alternative, so it's safe if the final result isprojects//traces/traceId
orprojects/null/traces/traceId
.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.
Is it really safe to call here
new DefaultGcpProjectIdProvider()
? What if I use fully differentprojectId
strategy in my project?May we somehow workaround with the system property or make that
setCurrentTraceId
aware about theprojectId
as well?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.
Totally agree we should be doing this because the devsite mentions this format (https://cloud.google.com/trace/docs/viewing-details#log_entries) . But it makes me wonder why that other TraceLoggingEnhancer we were using even exists? the "trace_id" label that other one uses isn't mentioned in that devsite link at all... Should we set both ?
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.
If there's no harm in doing so, I think we should do both. In that case I think you could even extend TraceLoggingEnhancer from the client lib and just override that single method.
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.
I opened a new issue in
google-cloud-java
so they fix their current implementation. googleapis/google-cloud-java#3280There 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.
We really only need the trace ID in one place, it's not being used for anything in
labels.trace_id
.Probably better not to add any more complexity where it's not necessary.
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.
sounds good to me