1919import com .google .cloud .MetadataConfig ;
2020import com .google .cloud .MonitoredResource ;
2121import com .google .cloud .ServiceOptions ;
22+ import com .google .cloud .logging .LogEntry .Builder ;
2223import com .google .common .base .Strings ;
2324import com .google .common .collect .ImmutableMap ;
25+
2426import java .util .ArrayList ;
2527import java .util .Collections ;
28+ import java .util .HashMap ;
2629import java .util .List ;
2730import java .util .Map ;
2831
@@ -72,6 +75,8 @@ String getKey() {
7275 }
7376 }
7477
78+ private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/" ;
79+
7580 private static Map <String , Label []> resourceTypeWithLabels ;
7681
7782 static {
@@ -80,15 +85,13 @@ String getKey() {
8085 .put (
8186 Resource .GaeAppFlex .getKey (),
8287 new Label [] {
83- Label .InstanceName ,
84- Label .ModuleId ,
85- Label .VersionId ,
86- Label .InstanceId ,
87- Label .Zone
88+ Label .ModuleId ,
89+ Label .VersionId ,
90+ Label .Zone
8891 })
8992 .put (
9093 Resource .GaeAppStandard .getKey (),
91- new Label [] {Label .AppId , Label . ModuleId , Label .VersionId })
94+ new Label [] {Label .ModuleId , Label .VersionId })
9295 .put (Resource .Container .getKey (), new Label [] {Label .ClusterName , Label .Zone })
9396 .put (Resource .GceInstance .getKey (), new Label [] {Label .InstanceId , Label .Zone })
9497 .build ();
@@ -124,11 +127,12 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
124127
125128 /**
126129 * Returns custom log entry enhancers (if available) for resource type.
130+ *
127131 * @return custom long entry enhancers
128132 */
129- public static List <LoggingEnhancer > getResourceEnhancers () {
133+ public static List <LoggingEnhancer > createResourceEnhancers () {
130134 Resource resourceType = getAutoDetectedResourceType ();
131- return getEnhancers (resourceType );
135+ return createEnhancers (resourceType );
132136 }
133137
134138 private static String getValue (Label label ) {
@@ -192,19 +196,54 @@ private static String getAppEngineInstanceName() {
192196 return System .getenv ("GAE_INSTANCE" );
193197 }
194198
195- private static List <LoggingEnhancer > getEnhancers (Resource resourceType ) {
196- List <LoggingEnhancer > enhancers ;
199+ private static List <LoggingEnhancer > createEnhancers (Resource resourceType ) {
200+ List <LoggingEnhancer > enhancers = new ArrayList <>( 2 ) ;
197201 switch (resourceType ) {
198202 // Trace logging enhancer is supported on GAE Flex and Standard.
199- case GaeAppStandard :
200203 case GaeAppFlex :
201- enhancers = new ArrayList <>();
202- enhancers .add (new TraceLoggingEnhancer ());
204+ enhancers .add (new LabelLoggingEnhancer (
205+ APPENGINE_LABEL_PREFIX , Collections .singletonList (Label .InstanceName )));
206+ enhancers .add (new TraceLoggingEnhancer (APPENGINE_LABEL_PREFIX ));
207+ break ;
208+ case GaeAppStandard :
209+ enhancers .add (new TraceLoggingEnhancer (APPENGINE_LABEL_PREFIX ));
203210 break ;
204211 default :
205- enhancers = Collections .emptyList ();
206212 break ;
207213 }
208214 return enhancers ;
209215 }
216+
217+ /**
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>
223+ */
224+ private static class LabelLoggingEnhancer implements LoggingEnhancer {
225+
226+ private final 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+ }
240+ }
241+
242+ @ Override
243+ public void enhanceLogEntry (Builder logEntry ) {
244+ for (Map .Entry <String , String > label : labels .entrySet ()) {
245+ logEntry .addLabel (label .getKey (), label .getValue ());
246+ }
247+ }
248+ }
210249}
0 commit comments