2121import com .google .cloud .ServiceOptions ;
2222import com .google .cloud .logging .LogEntry .Builder ;
2323import com .google .common .base .Strings ;
24- import com .google .common .collect .ImmutableMap ;
25-
24+ import com .google .common .collect .ImmutableMultimap ;
2625import java .util .ArrayList ;
2726import java .util .Collections ;
2827import java .util .HashMap ;
@@ -42,6 +41,7 @@ private enum Label {
4241 InstanceId ("instance_id" ),
4342 InstanceName ("instance_name" ),
4443 ModuleId ("module_id" ),
44+ PodId ("pod_id" ),
4545 ProjectId ("project_id" ),
4646 VersionId ("version_id" ),
4747 Zone ("zone" );
@@ -77,25 +77,18 @@ String getKey() {
7777
7878 private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/" ;
7979
80- private static Map <String , Label []> resourceTypeWithLabels ;
81-
82- static {
83- resourceTypeWithLabels =
84- new ImmutableMap .Builder <String , Label []>()
85- .put (
86- Resource .GaeAppFlex .getKey (),
87- new Label [] {
88- Label .ModuleId ,
89- Label .VersionId ,
90- Label .Zone
91- })
92- .put (
93- Resource .GaeAppStandard .getKey (),
94- new Label [] {Label .ModuleId , Label .VersionId })
95- .put (Resource .Container .getKey (), new Label [] {Label .ClusterName , Label .Zone })
96- .put (Resource .GceInstance .getKey (), new Label [] {Label .InstanceId , Label .Zone })
97- .build ();
98- }
80+ private static ImmutableMultimap <String , Label > resourceTypeWithLabels =
81+ ImmutableMultimap .<String , Label >builder ()
82+ .putAll (Resource .GaeAppFlex .getKey (), Label .ModuleId , Label .VersionId , Label .Zone )
83+ .putAll (Resource .GaeAppStandard .getKey (), Label .ModuleId , Label .VersionId )
84+ .putAll (
85+ Resource .Container .getKey (),
86+ Label .ClusterName ,
87+ Label .InstanceId ,
88+ Label .PodId ,
89+ Label .Zone )
90+ .putAll (Resource .GceInstance .getKey (), Label .InstanceId , Label .Zone )
91+ .build ();
9992
10093 private MonitoredResourceUtil () {
10194 }
@@ -113,13 +106,11 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
113106 String resourceName = resourceType .startsWith ("gae_app" ) ? "gae_app" : resourceType ;
114107 MonitoredResource .Builder builder =
115108 MonitoredResource .newBuilder (resourceName ).addLabel (Label .ProjectId .getKey (), projectId );
116- Label [] resourceLabels = resourceTypeWithLabels .get (resourceType );
117- if (resourceLabels != null ) {
118- for (Label label : resourceLabels ) {
119- String value = getValue (label );
120- if (value != null ) {
121- builder .addLabel (label .getKey (), value );
122- }
109+
110+ for (Label label : resourceTypeWithLabels .get (resourceType )) {
111+ String value = getValue (label );
112+ if (value != null ) {
113+ builder .addLabel (label .getKey (), value );
123114 }
124115 }
125116 return builder .build ();
@@ -153,6 +144,9 @@ private static String getValue(Label label) {
153144 case ModuleId :
154145 value = getAppEngineModuleId ();
155146 break ;
147+ case PodId :
148+ value = System .getenv ("HOSTNAME" );
149+ break ;
156150 case VersionId :
157151 value = getAppEngineVersionId ();
158152 break ;
0 commit comments