Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for k8s_container resource type #207

Merged
merged 1 commit into from Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand All @@ -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"),
Expand All @@ -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;
Expand Down Expand Up @@ -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() {}
Expand All @@ -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);
}
Expand All @@ -134,7 +148,7 @@ public static List<LoggingEnhancer> getResourceEnhancers() {
return createEnhancers(resourceType);
}

private static String getValue(Label label) {
private static String getValue(Label label, String resourceType) {
String value;
switch (label) {
case AppId:
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Expand Up @@ -307,6 +307,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();
Expand Down