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..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 @@ -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,12 @@ 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) { + return Resource.CloudRun; + } if (System.getenv("GAE_INSTANCE") != null) { return Resource.GaeAppFlex; } @@ -199,6 +219,14 @@ 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) {