@@ -95,6 +95,7 @@ public class LoggingHandler extends Handler {
9595
9696 private final LoggingOptions options ;
9797 private final WriteOption [] writeOptions ;
98+ private final String gaeInstanceId ;
9899 private List <LogEntry > buffer = new LinkedList <>();
99100 private volatile Logging logging ;
100101 private Level flushLevel ;
@@ -131,7 +132,9 @@ public LoggingHandler(String log, LoggingOptions options) {
131132 *
132133 * @param log the name of the log to which log entries are written
133134 * @param options options for the Stackdriver Logging service
134- * @param monitoredResource the monitored resource to which log entries refer
135+ * @param monitoredResource the monitored resource to which log entries refer. If null a default
136+ * resource is created based on the project ID. If a Google App Engine environment is detected
137+ * then a more comprehensive default resource may be created.
135138 */
136139 public LoggingHandler (String log , LoggingOptions options , MonitoredResource monitoredResource ) {
137140 LogConfigHelper helper = new LogConfigHelper ();
@@ -143,6 +146,7 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
143146 setFilter (helper .getFilterProperty (className + ".filter" , null ));
144147 setFormatter (helper .getFormatterProperty (className + ".formatter" , new SimpleFormatter ()));
145148 String logName = firstNonNull (log , helper .getProperty (className + ".log" , "java.log" ));
149+ gaeInstanceId = System .getenv ("GAE_INSTANCE" );
146150 MonitoredResource resource = firstNonNull (monitoredResource , getDefaultResource ());
147151 writeOptions = new WriteOption []{WriteOption .logName (logName ), WriteOption .resource (resource )};
148152 }
@@ -179,6 +183,19 @@ private static boolean hasLoggingHandler(Logger logger) {
179183 }
180184
181185 private MonitoredResource getDefaultResource () {
186+ // Are we running on a GAE instance?
187+ if (gaeInstanceId !=null && options .getProjectId ()!=null ) {
188+ MonitoredResource .Builder builder = MonitoredResource .newBuilder ("gae_app" )
189+ .addLabel ("project_id" , options .getProjectId ());
190+ if (System .getenv ("GAE_SERVICE" )!=null ) {
191+ builder .addLabel ("module_id" , System .getenv ("GAE_SERVICE" ));
192+ }
193+ if (System .getenv ("GAE_VERSION" )!=null ) {
194+ builder .addLabel ("version_id" , System .getenv ("GAE_VERSION" ));
195+ }
196+ return builder .build ();
197+ }
198+
182199 return MonitoredResource .of ("global" , ImmutableMap .of ("project_id" , options .getProjectId ()));
183200 }
184201
@@ -310,6 +327,10 @@ private LogEntry entryFor(LogRecord record) {
310327 .addLabel ("levelName" , level .getName ())
311328 .addLabel ("levelValue" , String .valueOf (level .intValue ()))
312329 .setSeverity (severityFor (level ));
330+ if (gaeInstanceId != null ) {
331+ builder .addLabel ("appengine.googleapis.com/instance_name" , gaeInstanceId );
332+ }
333+
313334 enhanceLogEntry (builder , record );
314335 return builder .build ();
315336 }
0 commit comments