11/*
2- * Copyright 2017 Google Inc. All Rights Reserved.
2+ * Copyright 2016 Google Inc. All Rights Reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import com .google .cloud .MonitoredResource .Builder ;
2222
2323/**
24- * A Logging {@link Enhancer} that enhances the logging for
25- * GAE Flex environment. This enhance can be configured in
26- * a logging.properties file with:
24+ * A Logging {@link Enhancer} that enhances the logging for the
25+ * GAE Flex environment. This enhancer can
26+ * be configured in a logging.properties file with:
27+ *
2728 * <pre>
2829 * handlers=com.google.cloud.logging.LoggingHandler
2930 * com.google.cloud.logging.LoggingHandler.log=gaeflex.log
3536 *
3637 */
3738public class GaeFlexLoggingEnhancer implements LoggingHandler .Enhancer {
38-
39+
3940 private static final ThreadLocal <String > traceId = new ThreadLocal <>();
40-
41+
4142 private String gaeInstanceId ;
4243
4344 /**
44- * Set the Trace ID associated with any logging done by
45- * the current thread.
45+ * Set the Trace ID associated with any logging done by the current thread.
46+ *
4647 * @param id The traceID
4748 */
4849 public static void setCurrentTraceId (String id ) {
4950 traceId .set (id );
5051 }
5152
5253 /**
53- * Get the Trace ID associated with any logging done by
54- * the current thread.
54+ * Get the Trace ID associated with any logging done by the current thread.
55+ *
5556 * @return id The traceID
5657 */
5758 public static String getCurrentTraceId () {
5859 return traceId .get ();
5960 }
60-
61+
62+ public GaeFlexLoggingEnhancer () {
63+ gaeInstanceId = System .getenv ("GAE_INSTANCE" ); // Are we running on a GAE instance?
64+ }
65+
6166 @ Override
6267 public void enhanceMonitoredResource (Builder builder ) {
63- gaeInstanceId = System .getenv ("GAE_INSTANCE" ); // Are we running on a GAE instance?
64- if (gaeInstanceId !=null ) {
65- if (System .getenv ("GAE_SERVICE" )!=null ) {
68+ if (gaeInstanceId != null ) {
69+ if (System .getenv ("GAE_SERVICE" ) != null ) {
6670 builder .addLabel ("module_id" , System .getenv ("GAE_SERVICE" ));
6771 }
68- if (System .getenv ("GAE_VERSION" )!= null ) {
72+ if (System .getenv ("GAE_VERSION" ) != null ) {
6973 builder .addLabel ("version_id" , System .getenv ("GAE_VERSION" ));
7074 }
7175 }
@@ -75,10 +79,10 @@ public void enhanceMonitoredResource(Builder builder) {
7579 public void enhanceLogEntry (com .google .cloud .logging .LogEntry .Builder builder , LogRecord record ) {
7680 if (gaeInstanceId != null ) {
7781 builder .addLabel ("appengine.googleapis.com/instance_name" , gaeInstanceId );
78- }
82+ }
7983 String traceId = getCurrentTraceId ();
8084 if (traceId != null ) {
8185 builder .addLabel ("appengine.googleapis.com/trace_id" , traceId );
82- }
86+ }
8387 }
8488}
0 commit comments