Skip to content

Commit 6c773d2

Browse files
authored
logging: log more kubernetes properties (#2937)
Updates #2912.
1 parent 8bb2a59 commit 6c773d2

2 files changed

Lines changed: 23 additions & 29 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/MetadataConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static String getInstanceId() {
5454
}
5555

5656
public static String getClusterName() {
57-
return getAttribute("instance/cluster-name");
57+
return getAttribute("instance/attributes/cluster-name");
5858
}
5959

6060
private static String getAttribute(String attributeName) {

google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import com.google.cloud.ServiceOptions;
2222
import com.google.cloud.logging.LogEntry.Builder;
2323
import com.google.common.base.Strings;
24-
import com.google.common.collect.ImmutableMap;
25-
24+
import com.google.common.collect.ImmutableMultimap;
2625
import java.util.ArrayList;
2726
import java.util.Collections;
2827
import 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

Comments
 (0)