From 233efcce7d08cc66088d367b7fb6c104cb2f3a46 Mon Sep 17 00:00:00 2001 From: Suraj Dhamecha <48670070+suraj-qlogic@users.noreply.github.com> Date: Wed, 9 Dec 2020 12:21:43 +0530 Subject: [PATCH] feat: add support for k8s_container resource type (#207) --- .../cloud/logging/MonitoredResourceUtil.java | 43 ++++++++++++++++--- .../cloud/logging/LoggingHandlerTest.java | 35 +++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java index 5b9e15247..85e0f37f9 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java @@ -22,6 +22,10 @@ import com.google.cloud.logging.LogEntry.Builder; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMultimap; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -43,7 +47,9 @@ private enum Label { Location("location"), ModuleId("module_id"), NamespaceId("namespace_id"), + NamespaceName("namespace_name"), PodId("pod_id"), + PodName("pod_name"), ProjectId("project_id"), RevisionName("revision_name"), ServiceName("service_name"), @@ -67,6 +73,7 @@ private enum Resource { GaeAppFlex("gae_app_flex"), GaeAppStandard("gae_app_standard"), GceInstance("gce_instance"), + K8sContainer("k8s_container"), Global("global"); private final String key; @@ -96,6 +103,13 @@ String getKey() { .putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone) .putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId) .putAll(Resource.GceInstance.getKey(), Label.InstanceId, Label.Zone) + .putAll( + Resource.K8sContainer.getKey(), + Label.Location, + Label.ClusterName, + Label.NamespaceName, + Label.PodName, + Label.ContainerName) .build(); private MonitoredResourceUtil() {} @@ -116,7 +130,7 @@ public static MonitoredResource getResource(String projectId, String resourceTyp MonitoredResource.newBuilder(resourceName).addLabel(Label.ProjectId.getKey(), projectId); for (Label label : resourceTypeWithLabels.get(resourceType)) { - String value = getValue(label); + String value = getValue(label, resourceType); if (value != null) { builder.addLabel(label.getKey(), value); } @@ -134,7 +148,7 @@ public static List getResourceEnhancers() { return createEnhancers(resourceType); } - private static String getValue(Label label) { + private static String getValue(Label label, String resourceType) { String value; switch (label) { case AppId: @@ -144,7 +158,12 @@ private static String getValue(Label label) { value = MetadataConfig.getClusterName(); break; case ContainerName: - value = MetadataConfig.getContainerName(); + if (resourceType.equals("k8s_container")) { + String hostName = System.getenv("HOSTNAME"); + value = hostName.substring(0, hostName.indexOf("-")); + } else { + value = MetadataConfig.getContainerName(); + } break; case InstanceId: value = MetadataConfig.getInstanceId(); @@ -161,6 +180,18 @@ private static String getValue(Label label) { case NamespaceId: value = MetadataConfig.getNamespaceId(); break; + case NamespaceName: + String filePath = System.getenv("KUBERNETES_NAMESPACE_FILE"); + if (filePath == null) { + filePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"; + } + try { + value = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8); + } catch (IOException e) { + throw new LoggingException(e, true); + } + break; + case PodName: case PodId: value = System.getenv("HOSTNAME"); break; @@ -261,10 +292,10 @@ private static class LabelLoggingEnhancer implements LoggingEnhancer { labels = new HashMap<>(); if (labelNames != null) { for (Label labelName : labelNames) { - String labelValue = MonitoredResourceUtil.getValue(labelName); + String fullLabelName = + (prefix != null) ? prefix + labelName.getKey() : labelName.getKey(); + String labelValue = MonitoredResourceUtil.getValue(labelName, fullLabelName); if (labelValue != null) { - String fullLabelName = - (prefix != null) ? prefix + labelName.getKey() : labelName.getKey(); labels.put(fullLabelName, labelValue); } } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java index 91a215fde..5d5285cb8 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java @@ -310,6 +310,41 @@ public void testPublishCustomResource() { handler.publish(newLogRecord(Level.FINEST, MESSAGE)); } + @Test + public void testPublishKubernetesContainerResource() { + expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); + expect(options.getService()).andReturn(logging); + logging.setFlushSeverity(Severity.ERROR); + expectLastCall().once(); + logging.setWriteSynchronicity(Synchronicity.ASYNC); + expectLastCall().once(); + MonitoredResource resource = + MonitoredResource.of( + "k8s_container", + ImmutableMap.of( + "project_id", + PROJECT, + "container_name", + "metadata-agent", + "location", + "us-central1-c", + "namespace_name", + "test-system", + "pod_name", + "metadata-agent-cluster")); + logging.write( + ImmutableList.of(FINEST_ENTRY), + WriteOption.logName(LOG_NAME), + WriteOption.resource(resource), + WriteOption.labels(BASE_SEVERITY_MAP)); + expectLastCall().once(); + replay(options, logging); + Handler handler = new LoggingHandler(LOG_NAME, options, resource); + handler.setLevel(Level.ALL); + handler.setFormatter(new TestFormatter()); + handler.publish(newLogRecord(Level.FINEST, MESSAGE)); + } + @Test public void testEnhancedLogEntry() { expect(options.getProjectId()).andReturn(PROJECT).anyTimes();