-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initialize the default MonitoredResource from a GAE environment #1535
Merged
garrettjonesgoogle
merged 12 commits into
googleapis:master
from
jetty-project:flexMonitoredResource
Jan 19, 2017
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
646ee05
Initialize the default MonitoredResource from a GAE environment
gregw 4801395
Logging Enhancer
gregw e7daf1f
Fixed codacy bot issues
gregw a0132cf
Fixed review points
gregw 0676de0
fixed codacy-bot recommendation
gregw 0aa59f0
Do not create default field value if non null valued passed
gregw e174007
Added unit test
gregw c64eb42
fixed codacy-bot recommendation
gregw c93532a
improved javadoc
gregw 3185bc2
deprecate enhanceLogEntry
gregw 30c0019
Merge branch 'master' into flexMonitoredResource
gregw 823dda7
fixed test after merge
gregw 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
88 changes: 88 additions & 0 deletions
88
google-cloud-logging/src/main/java/com/google/cloud/logging/GaeFlexLoggingEnhancer.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,88 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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 com.google.cloud.logging; | ||
|
||
import java.util.logging.LogRecord; | ||
|
||
import com.google.cloud.MonitoredResource.Builder; | ||
|
||
/** | ||
* A Logging {@link Enhancer} that enhances the logging for the | ||
* GAE Flex environment. This enhancer can | ||
* be configured in a logging.properties file with: | ||
* | ||
* <pre> | ||
* handlers=com.google.cloud.logging.LoggingHandler | ||
* com.google.cloud.logging.LoggingHandler.log=gaeflex.log | ||
* com.google.cloud.logging.LoggingHandler.resourceType=gae_app | ||
* com.google.cloud.logging.LoggingHandler.enhancers=com.google.cloud.logging.GaeFlexLoggingEnhancer | ||
* com.google.cloud.logging.LoggingHandler.formatter = java.util.logging.SimpleFormatter | ||
* java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s | ||
* </pre> | ||
* | ||
*/ | ||
public class GaeFlexLoggingEnhancer implements LoggingHandler.Enhancer { | ||
|
||
private static final ThreadLocal<String> traceId = new ThreadLocal<>(); | ||
|
||
private final String gaeInstanceId; | ||
|
||
/** | ||
* Set the Trace ID associated with any logging done by the current thread. | ||
* | ||
* @param id The traceID | ||
*/ | ||
public static void setCurrentTraceId(String id) { | ||
traceId.set(id); | ||
} | ||
|
||
/** | ||
* Get the Trace ID associated with any logging done by the current thread. | ||
* | ||
* @return id The traceID | ||
*/ | ||
public static String getCurrentTraceId() { | ||
return traceId.get(); | ||
} | ||
|
||
public GaeFlexLoggingEnhancer() { | ||
gaeInstanceId = System.getenv("GAE_INSTANCE"); // Are we running on a GAE instance? | ||
} | ||
|
||
@Override | ||
public void enhanceMonitoredResource(Builder builder) { | ||
if (gaeInstanceId != null) { | ||
if (System.getenv("GAE_SERVICE") != null) { | ||
builder.addLabel("module_id", System.getenv("GAE_SERVICE")); | ||
} | ||
if (System.getenv("GAE_VERSION") != null) { | ||
builder.addLabel("version_id", System.getenv("GAE_VERSION")); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder, LogRecord record) { | ||
if (gaeInstanceId != null) { | ||
builder.addLabel("appengine.googleapis.com/instance_name", gaeInstanceId); | ||
} | ||
String traceId = getCurrentTraceId(); | ||
if (traceId != null) { | ||
builder.addLabel("appengine.googleapis.com/trace_id", 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
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.
This comment was marked as spam.
Sorry, something went wrong.