From 2bdd22362430c6aa925a10637e76ca56f0a3a2be Mon Sep 17 00:00:00 2001 From: Mike Eltsufin Date: Wed, 22 Jan 2020 11:39:12 -0500 Subject: [PATCH 1/3] fix: support for Cloud Run monitored resource Fixes: #71. --- .../cloud/logging/MonitoredResourceUtil.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 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 77133fda4..00bbee704 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 @@ -40,10 +40,13 @@ private enum Label { ContainerName("container_name"), InstanceId("instance_id"), InstanceName("instance_name"), + Location("location"), ModuleId("module_id"), NamespaceId("namespace_id"), PodId("pod_id"), ProjectId("project_id"), + RevisionName("revision_name"), + ServiceName("service_name"), VersionId("version_id"), Zone("zone"); @@ -59,6 +62,7 @@ String getKey() { } private enum Resource { + CloudRun("cloud_run_revision"), Container("container"), GaeAppFlex("gae_app_flex"), GaeAppStandard("gae_app_standard"), @@ -80,8 +84,6 @@ String getKey() { private static ImmutableMultimap resourceTypeWithLabels = ImmutableMultimap.builder() - .putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone) - .putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId) .putAll( Resource.Container.getKey(), Label.ClusterName, @@ -90,6 +92,9 @@ String getKey() { Label.NamespaceId, Label.PodId, Label.Zone) + .putAll(Resource.CloudRun.getKey(), Label.RevisionName, Label.ServiceName, Label.Location) + .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) .build(); @@ -147,6 +152,9 @@ private static String getValue(Label label) { case InstanceName: value = getAppEngineInstanceName(); break; + case Location: + value = getCloudRunLocation(); + break; case ModuleId: value = getAppEngineModuleId(); break; @@ -156,6 +164,12 @@ private static String getValue(Label label) { case PodId: value = System.getenv("HOSTNAME"); break; + case RevisionName: + value = System.getenv("K_REVISION"); + break; + case ServiceName: + value = System.getenv("K_SERVICE"); + break; case VersionId: value = getAppEngineVersionId(); break; @@ -171,6 +185,11 @@ private static String getValue(Label label) { /* Detect monitored Resource type using environment variables, else return global as default. */ private static Resource getAutoDetectedResourceType() { + if (System.getenv("K_SERVICE") != null && + System.getenv("K_REVISION") != null && + System.getenv("K_CONFIGURATION") != null) { + return Resource.CloudRun; + } if (System.getenv("GAE_INSTANCE") != null) { return Resource.GaeAppFlex; } @@ -199,6 +218,16 @@ private static String getAppEngineInstanceName() { return System.getenv("GAE_INSTANCE"); } + private static String getCloudRunLocation() { + String zone = MetadataConfig.getZone(); + // for Cloud Run managed, the zone is "REGION-1" + // So, we need to strip the "-1" to set location to just the region + if (zone.endsWith("-1")) + return zone.substring(0, zone.length() - 2); + else + return zone; + } + private static List createEnhancers(Resource resourceType) { List enhancers = new ArrayList<>(2); switch (resourceType) { From edb1bed82641d349b514ff7f8ff4363c68e9722f Mon Sep 17 00:00:00 2001 From: Mike Eltsufin Date: Wed, 22 Jan 2020 13:09:48 -0500 Subject: [PATCH 2/3] add check for not k8s --- .../java/com/google/cloud/logging/MonitoredResourceUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 00bbee704..3bc2aabf1 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 @@ -187,7 +187,8 @@ private static String getValue(Label label) { private static Resource getAutoDetectedResourceType() { if (System.getenv("K_SERVICE") != null && System.getenv("K_REVISION") != null && - System.getenv("K_CONFIGURATION") != null) { + System.getenv("K_CONFIGURATION") != null && + System.getenv("KUBERNETES_SERVICE_HOST") == null) { return Resource.CloudRun; } if (System.getenv("GAE_INSTANCE") != null) { From 0af03307fcb8a73a92c54cc7e4b7213df22fe7ec Mon Sep 17 00:00:00 2001 From: Mike Eltsufin Date: Wed, 22 Jan 2020 15:46:58 -0500 Subject: [PATCH 3/3] format --- .../cloud/logging/MonitoredResourceUtil.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 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 3bc2aabf1..5b9e15247 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 @@ -185,10 +185,10 @@ private static String getValue(Label label) { /* Detect monitored Resource type using environment variables, else return global as default. */ private static Resource getAutoDetectedResourceType() { - if (System.getenv("K_SERVICE") != null && - System.getenv("K_REVISION") != null && - System.getenv("K_CONFIGURATION") != null && - System.getenv("KUBERNETES_SERVICE_HOST") == null) { + if (System.getenv("K_SERVICE") != null + && System.getenv("K_REVISION") != null + && System.getenv("K_CONFIGURATION") != null + && System.getenv("KUBERNETES_SERVICE_HOST") == null) { return Resource.CloudRun; } if (System.getenv("GAE_INSTANCE") != null) { @@ -223,10 +223,8 @@ private static String getCloudRunLocation() { String zone = MetadataConfig.getZone(); // for Cloud Run managed, the zone is "REGION-1" // So, we need to strip the "-1" to set location to just the region - if (zone.endsWith("-1")) - return zone.substring(0, zone.length() - 2); - else - return zone; + if (zone.endsWith("-1")) return zone.substring(0, zone.length() - 2); + else return zone; } private static List createEnhancers(Resource resourceType) {