Skip to content

Commit 233efcc

Browse files
authored
feat: add support for k8s_container resource type (#207)
1 parent 70242d5 commit 233efcc

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
import com.google.cloud.logging.LogEntry.Builder;
2323
import com.google.common.base.Strings;
2424
import com.google.common.collect.ImmutableMultimap;
25+
import java.io.IOException;
26+
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
2529
import java.util.ArrayList;
2630
import java.util.Collections;
2731
import java.util.HashMap;
@@ -43,7 +47,9 @@ private enum Label {
4347
Location("location"),
4448
ModuleId("module_id"),
4549
NamespaceId("namespace_id"),
50+
NamespaceName("namespace_name"),
4651
PodId("pod_id"),
52+
PodName("pod_name"),
4753
ProjectId("project_id"),
4854
RevisionName("revision_name"),
4955
ServiceName("service_name"),
@@ -67,6 +73,7 @@ private enum Resource {
6773
GaeAppFlex("gae_app_flex"),
6874
GaeAppStandard("gae_app_standard"),
6975
GceInstance("gce_instance"),
76+
K8sContainer("k8s_container"),
7077
Global("global");
7178

7279
private final String key;
@@ -96,6 +103,13 @@ String getKey() {
96103
.putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone)
97104
.putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId)
98105
.putAll(Resource.GceInstance.getKey(), Label.InstanceId, Label.Zone)
106+
.putAll(
107+
Resource.K8sContainer.getKey(),
108+
Label.Location,
109+
Label.ClusterName,
110+
Label.NamespaceName,
111+
Label.PodName,
112+
Label.ContainerName)
99113
.build();
100114

101115
private MonitoredResourceUtil() {}
@@ -116,7 +130,7 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
116130
MonitoredResource.newBuilder(resourceName).addLabel(Label.ProjectId.getKey(), projectId);
117131

118132
for (Label label : resourceTypeWithLabels.get(resourceType)) {
119-
String value = getValue(label);
133+
String value = getValue(label, resourceType);
120134
if (value != null) {
121135
builder.addLabel(label.getKey(), value);
122136
}
@@ -134,7 +148,7 @@ public static List<LoggingEnhancer> getResourceEnhancers() {
134148
return createEnhancers(resourceType);
135149
}
136150

137-
private static String getValue(Label label) {
151+
private static String getValue(Label label, String resourceType) {
138152
String value;
139153
switch (label) {
140154
case AppId:
@@ -144,7 +158,12 @@ private static String getValue(Label label) {
144158
value = MetadataConfig.getClusterName();
145159
break;
146160
case ContainerName:
147-
value = MetadataConfig.getContainerName();
161+
if (resourceType.equals("k8s_container")) {
162+
String hostName = System.getenv("HOSTNAME");
163+
value = hostName.substring(0, hostName.indexOf("-"));
164+
} else {
165+
value = MetadataConfig.getContainerName();
166+
}
148167
break;
149168
case InstanceId:
150169
value = MetadataConfig.getInstanceId();
@@ -161,6 +180,18 @@ private static String getValue(Label label) {
161180
case NamespaceId:
162181
value = MetadataConfig.getNamespaceId();
163182
break;
183+
case NamespaceName:
184+
String filePath = System.getenv("KUBERNETES_NAMESPACE_FILE");
185+
if (filePath == null) {
186+
filePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace";
187+
}
188+
try {
189+
value = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
190+
} catch (IOException e) {
191+
throw new LoggingException(e, true);
192+
}
193+
break;
194+
case PodName:
164195
case PodId:
165196
value = System.getenv("HOSTNAME");
166197
break;
@@ -261,10 +292,10 @@ private static class LabelLoggingEnhancer implements LoggingEnhancer {
261292
labels = new HashMap<>();
262293
if (labelNames != null) {
263294
for (Label labelName : labelNames) {
264-
String labelValue = MonitoredResourceUtil.getValue(labelName);
295+
String fullLabelName =
296+
(prefix != null) ? prefix + labelName.getKey() : labelName.getKey();
297+
String labelValue = MonitoredResourceUtil.getValue(labelName, fullLabelName);
265298
if (labelValue != null) {
266-
String fullLabelName =
267-
(prefix != null) ? prefix + labelName.getKey() : labelName.getKey();
268299
labels.put(fullLabelName, labelValue);
269300
}
270301
}

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,41 @@ public void testPublishCustomResource() {
310310
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
311311
}
312312

313+
@Test
314+
public void testPublishKubernetesContainerResource() {
315+
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
316+
expect(options.getService()).andReturn(logging);
317+
logging.setFlushSeverity(Severity.ERROR);
318+
expectLastCall().once();
319+
logging.setWriteSynchronicity(Synchronicity.ASYNC);
320+
expectLastCall().once();
321+
MonitoredResource resource =
322+
MonitoredResource.of(
323+
"k8s_container",
324+
ImmutableMap.of(
325+
"project_id",
326+
PROJECT,
327+
"container_name",
328+
"metadata-agent",
329+
"location",
330+
"us-central1-c",
331+
"namespace_name",
332+
"test-system",
333+
"pod_name",
334+
"metadata-agent-cluster"));
335+
logging.write(
336+
ImmutableList.of(FINEST_ENTRY),
337+
WriteOption.logName(LOG_NAME),
338+
WriteOption.resource(resource),
339+
WriteOption.labels(BASE_SEVERITY_MAP));
340+
expectLastCall().once();
341+
replay(options, logging);
342+
Handler handler = new LoggingHandler(LOG_NAME, options, resource);
343+
handler.setLevel(Level.ALL);
344+
handler.setFormatter(new TestFormatter());
345+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
346+
}
347+
313348
@Test
314349
public void testEnhancedLogEntry() {
315350
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();

0 commit comments

Comments
 (0)