diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java index 1a0581626..a5530974c 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java @@ -40,6 +40,9 @@ public final class MetadataLoader { .put(Label.FunctionName, this::getFunctionName) .put(Label.InstanceId, this::getInstanceId) .put(Label.InstanceName, this::getInstanceName) + .put(Label.CloudRunJobName, this::getCloudRunJobName) + .put(Label.CloudRunJobExecutionName, this::getCloudRunJobExecutionName) + .put(Label.CloudRunJobTaskIndex, this::getCloudRunJobTaskIndex) .put(Label.CloudRunLocation, this::getCloudRunLocation) .put(Label.GKELocation, this::getGKELocation) .put(Label.ModuleId, this::getModuleId) @@ -182,6 +185,18 @@ private String getRegion() { return null; } + private String getCloudRunJobName() { + return getter.getEnv("CLOUD_RUN_JOB"); + } + + private String getCloudRunJobExecutionName() { + return getter.getEnv("CLOUD_RUN_EXECUTION"); + } + + private String getCloudRunJobTaskIndex() { + return getter.getEnv("CLOUD_RUN_TASK_INDEX"); + } + private String getRevisionName() { return getter.getEnv("K_REVISION"); } 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 1f87e874f..305fe48dd 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 @@ -32,6 +32,7 @@ public class MonitoredResourceUtil { private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/"; + private static final String CLOUD_RUN_JOB_LABEL_PREFIX = "run.googleapis.com/"; protected static final String PORJECTID_LABEL = Label.ProjectId.getKey(); protected enum Label { @@ -42,6 +43,9 @@ protected enum Label { FunctionName("function_name"), InstanceId("instance_id"), InstanceName("instance_name"), + CloudRunJobName("job_name"), + CloudRunJobExecutionName("execution_name"), + CloudRunJobTaskIndex("task_index"), CloudRunLocation("location"), GKELocation("location"), ModuleId("module_id"), @@ -67,6 +71,7 @@ String getKey() { private enum Resource { CLOUD_RUN("cloud_run_revision"), + CLOUD_RUN_JOB("cloud_run_job"), CLOUD_FUNCTION("cloud_function"), APP_ENGINE("gae_app"), GCE_INSTANCE("gce_instance"), @@ -93,6 +98,9 @@ String getKey() { Label.ServiceName, Label.CloudRunLocation, Label.ConfigurationName) + .putAll(Resource.CLOUD_RUN_JOB.getKey(), + Label.CloudRunJobName, + Label.CloudRunLocation) .putAll( Resource.APP_ENGINE.getKey(), Label.ModuleId, Label.VersionId, Label.Zone, Label.Env) .putAll(Resource.GCE_INSTANCE.getKey(), Label.InstanceId, Label.Zone) @@ -177,6 +185,11 @@ private static Resource detectResourceType() { && getter.getEnv("K_CONFIGURATION") != null) { return Resource.CLOUD_RUN; } + if (getter.getEnv("CLOUD_RUN_JOB") != null + && getter.getEnv("CLOUD_RUN_EXECUTION") != null + && getter.getEnv("CLOUD_RUN_TASK_INDEX") != null) { + return Resource.CLOUD_RUN_JOB; + } if (getter.getEnv("GAE_INSTANCE") != null && getter.getEnv("GAE_SERVICE") != null && getter.getEnv("GAE_VERSION") != null) { @@ -212,6 +225,10 @@ private static List createEnhancers(Resource resourceType) { enhancers.add( new LabelLoggingEnhancer(APPENGINE_LABEL_PREFIX, ImmutableList.of(Label.InstanceName))); } + } else if (resourceType == Resource.CLOUD_RUN_JOB) { + enhancers.add( + new LabelLoggingEnhancer(CLOUD_RUN_JOB_LABEL_PREFIX, + ImmutableList.of(Label.CloudRunJobExecutionName, Label.CloudRunJobTaskIndex))); } return enhancers; } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/MonitoredResourceUtilTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/MonitoredResourceUtilTest.java index acc718a12..26f81e894 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/MonitoredResourceUtilTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/MonitoredResourceUtilTest.java @@ -286,4 +286,31 @@ public void testResourceTypeCloudRun() { assertEquals("cloud_run_revision", response.getType()); assertEquals(expectedLabels, response.getLabels()); } + + @Test + public void testResourceTypeCloudRunJob() { + final String mockedJobName = "mocked-job-name"; + final ImmutableMap expectedLabels = + ImmutableMap.of( + "project_id", + MonitoredResourceUtilTest.MOCKED_PROJECT_ID, + "job_name", + mockedJobName, + "location", + MOCKED_REGION); + + // setup + expect(getterMock.getAttribute("instance/region")).andReturn(MOCKED_QUALIFIED_REGION).once(); + expect(getterMock.getAttribute(anyString())).andReturn(null).anyTimes(); + expect(getterMock.getEnv("CLOUD_RUN_JOB")).andReturn(mockedJobName).times(2); + expect(getterMock.getEnv("CLOUD_RUN_EXECUTION")).andReturn(mockedJobName + "_1").times(1); + expect(getterMock.getEnv("CLOUD_RUN_TASK_INDEX")).andReturn("0").times(1); + expect(getterMock.getEnv(anyString())).andReturn(null).anyTimes(); + replay(getterMock); + // exercise + MonitoredResource response = MonitoredResourceUtil.getResource("", ""); + // verify + assertEquals("cloud_run_job", response.getType()); + assertEquals(expectedLabels, response.getLabels()); + } }