Skip to content

Commit

Permalink
Fixed review points
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Jan 17, 2017
1 parent e7daf1f commit a0132cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Google Inc. All Rights Reserved.
* 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.
Expand All @@ -21,9 +21,10 @@
import com.google.cloud.MonitoredResource.Builder;

/**
* A Logging {@link Enhancer} that enhances the logging for
* GAE Flex environment. This enhance can be configured in
* a logging.properties file with:
* 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
Expand All @@ -35,37 +36,40 @@
*
*/
public class GaeFlexLoggingEnhancer implements LoggingHandler.Enhancer {

private static final ThreadLocal<String> traceId = new ThreadLocal<>();

private String gaeInstanceId;

/**
* Set the Trace ID associated with any logging done by
* the current thread.
* 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.
* 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) {
gaeInstanceId = System.getenv("GAE_INSTANCE"); // Are we running on a GAE instance?
if (gaeInstanceId!=null) {
if (System.getenv("GAE_SERVICE")!=null) {
if (gaeInstanceId != null) {
if (System.getenv("GAE_SERVICE") != null) {
builder.addLabel("module_id", System.getenv("GAE_SERVICE"));
}
if (System.getenv("GAE_VERSION")!=null) {
if (System.getenv("GAE_VERSION") != null) {
builder.addLabel("version_id", System.getenv("GAE_VERSION"));
}
}
Expand All @@ -75,10 +79,10 @@ public void enhanceMonitoredResource(Builder builder) {
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public LoggingHandler(String log, LoggingOptions options) {
*
* @param log the name of the log to which log entries are written
* @param options options for the Stackdriver Logging service
* @param monitoredResource the monitored resource to which log entries refer. If null a default
* resource is created based on the project ID. If a Google App Engine environment is detected
* @param monitoredResource the monitored resource to which log entries refer. If it is null
* then a default resource is created based on the project ID. If a Google App Engine environment is detected
* then a more comprehensive default resource may be created.
*/
public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource) {
Expand All @@ -147,10 +147,10 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
*
* @param log the name of the log to which log entries are written
* @param options options for the Stackdriver Logging service
* @param monitoredResource the monitored resource to which log entries refer. If null a default
* @param enhancers List of {@link Enhancer} instances.
* resource is created based on the project ID. If a Google App Engine environment is detected
* @param monitoredResource the monitored resource to which log entries refer. If it is null
* then a default resource is created based on the project ID. If a Google App Engine environment is detected
* then a more comprehensive default resource may be created.
* @param enhancers List of {@link Enhancer} instances.
*/
public LoggingHandler(String log, LoggingOptions options, MonitoredResource monitoredResource, List<Enhancer> enhancers) {
LogConfigHelper helper = new LogConfigHelper();
Expand Down Expand Up @@ -479,7 +479,7 @@ public static void addHandler(Logger logger, LoggingHandler handler) {

/**
* A Log Enhancer.
* May be used to enhanced the {@link MonitoredResource} and/or the {@link LogEntry}
* May be used to enhance the {@link MonitoredResource} and/or the {@link LogEntry}
*/
interface Enhancer {
void enhanceMonitoredResource(MonitoredResource.Builder builder);
Expand Down

0 comments on commit a0132cf

Please sign in to comment.