2525
2626import java .util .ArrayList ;
2727import java .util .Collections ;
28+ import java .util .HashMap ;
2829import java .util .List ;
2930import java .util .Map ;
3031
@@ -83,7 +84,7 @@ String getKey() {
8384 new ImmutableMap .Builder <String , Label []>()
8485 .put (
8586 Resource .GaeAppFlex .getKey (),
86- new Label []{
87+ new Label [] {
8788 Label .ModuleId ,
8889 Label .VersionId ,
8990 Label .Zone
@@ -126,9 +127,10 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
126127
127128 /**
128129 * Returns custom log entry enhancers (if available) for resource type.
130+ *
129131 * @return custom long entry enhancers
130132 */
131- public static List <LoggingEnhancer > getResourceEnhancers () {
133+ public static List <LoggingEnhancer > createResourceEnhancers () {
132134 Resource resourceType = getAutoDetectedResourceType ();
133135 return getEnhancers (resourceType );
134136 }
@@ -195,7 +197,7 @@ private static String getAppEngineInstanceName() {
195197 }
196198
197199 private static List <LoggingEnhancer > getEnhancers (Resource resourceType ) {
198- List <LoggingEnhancer > enhancers = new ArrayList <>();
200+ List <LoggingEnhancer > enhancers = new ArrayList <>(2 );
199201 switch (resourceType ) {
200202 // Trace logging enhancer is supported on GAE Flex and Standard.
201203 case GaeAppFlex :
@@ -207,34 +209,40 @@ private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {
207209 enhancers .add (new TraceLoggingEnhancer (APPENGINE_LABEL_PREFIX ));
208210 break ;
209211 default :
210- enhancers = Collections .emptyList ();
211212 break ;
212213 }
213214 return enhancers ;
214215 }
215216
216217 /**
217- * Adds additional resource-based labels to log entries.
218- * Labels that can be provided with {@link MonitoredResource.Builder#addLabel(String, String)}
219- * are restricted to a supported set per resource.
220- * @see <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">Logging Labels</a>
218+ * Adds additional resource-based labels to log entries.
219+ * Labels that can be provided with {@link MonitoredResource.Builder#addLabel(String, String)}
220+ * are restricted to a supported set per resource.
221+ *
222+ * @see <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">Logging Labels</a>
221223 */
222224 private static class LabelLoggingEnhancer implements LoggingEnhancer {
223- String prefix ;
224- List <Label > labels ;
225225
226- LabelLoggingEnhancer (String prefix , List <Label > labels ) {
227- this .prefix = prefix != null ? prefix : "" ;
228- this .labels = labels ;
226+ Map <String , String > labels ;
227+
228+ LabelLoggingEnhancer (String prefix , List <Label > labelNames ) {
229+ labels = new HashMap <>();
230+ if (labelNames != null ) {
231+ for (Label labelName : labelNames ) {
232+ String labelValue = MonitoredResourceUtil .getValue (labelName );
233+ if (labelValue != null ) {
234+ String fullLabelName = (prefix != null ) ?
235+ prefix + labelName .getKey () : labelName .getKey ();
236+ labels .put (fullLabelName , labelValue );
237+ }
238+ }
239+ }
229240 }
230241
231242 @ Override
232243 public void enhanceLogEntry (Builder logEntry ) {
233- for (Label label : labels ) {
234- String labelValue = MonitoredResourceUtil .getValue (label );
235- if (labelValue != null ) {
236- logEntry .addLabel (prefix + label .getKey (), labelValue );
237- }
244+ for (Map .Entry <String , String > label : labels .entrySet ()) {
245+ logEntry .addLabel (label .getKey (), label .getValue ());
238246 }
239247 }
240248 }
0 commit comments