Skip to content

Commit 897b803

Browse files
committed
feat: Cloud Run Jobs support
1 parent b40e846 commit 897b803

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/MetadataLoader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class MetadataLoader {
4040
.put(Label.FunctionName, this::getFunctionName)
4141
.put(Label.InstanceId, this::getInstanceId)
4242
.put(Label.InstanceName, this::getInstanceName)
43+
.put(Label.JobName, this::getCloudRunJobName)
4344
.put(Label.CloudRunLocation, this::getCloudRunLocation)
4445
.put(Label.GKELocation, this::getGKELocation)
4546
.put(Label.ModuleId, this::getModuleId)
@@ -182,6 +183,10 @@ private String getRegion() {
182183
return null;
183184
}
184185

186+
private String getCloudRunJobName() {
187+
return getter.getEnv("CLOUD_RUN_JOB");
188+
}
189+
185190
private String getRevisionName() {
186191
return getter.getEnv("K_REVISION");
187192
}

google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected enum Label {
4242
FunctionName("function_name"),
4343
InstanceId("instance_id"),
4444
InstanceName("instance_name"),
45+
JobName("job_name"),
4546
CloudRunLocation("location"),
4647
GKELocation("location"),
4748
ModuleId("module_id"),
@@ -67,6 +68,7 @@ String getKey() {
6768

6869
private enum Resource {
6970
CLOUD_RUN("cloud_run_revision"),
71+
CLOUD_RUN_JOB("cloud_run_job"),
7072
CLOUD_FUNCTION("cloud_function"),
7173
APP_ENGINE("gae_app"),
7274
GCE_INSTANCE("gce_instance"),
@@ -93,6 +95,9 @@ String getKey() {
9395
Label.ServiceName,
9496
Label.CloudRunLocation,
9597
Label.ConfigurationName)
98+
.putAll(Resource.CLOUD_RUN_JOB.getKey(),
99+
Label.JobName,
100+
Label.CloudRunLocation)
96101
.putAll(
97102
Resource.APP_ENGINE.getKey(), Label.ModuleId, Label.VersionId, Label.Zone, Label.Env)
98103
.putAll(Resource.GCE_INSTANCE.getKey(), Label.InstanceId, Label.Zone)
@@ -177,6 +182,9 @@ private static Resource detectResourceType() {
177182
&& getter.getEnv("K_CONFIGURATION") != null) {
178183
return Resource.CLOUD_RUN;
179184
}
185+
if (getter.getEnv("CLOUD_RUN_JOB") != null) {
186+
return Resource.CLOUD_RUN_JOB;
187+
}
180188
if (getter.getEnv("GAE_INSTANCE") != null
181189
&& getter.getEnv("GAE_SERVICE") != null
182190
&& getter.getEnv("GAE_VERSION") != null) {

google-cloud-logging/src/test/java/com/google/cloud/logging/MonitoredResourceUtilTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,29 @@ public void testResourceTypeCloudRun() {
286286
assertEquals("cloud_run_revision", response.getType());
287287
assertEquals(expectedLabels, response.getLabels());
288288
}
289+
290+
@Test
291+
public void testResourceTypeCloudRunJob() {
292+
final String mockedJobName = "mocked-job-name";
293+
final ImmutableMap<String, String> expectedLabels =
294+
ImmutableMap.of(
295+
"project_id",
296+
MonitoredResourceUtilTest.MOCKED_PROJECT_ID,
297+
"job_name",
298+
mockedJobName,
299+
"location",
300+
MOCKED_REGION);
301+
302+
// setup
303+
expect(getterMock.getAttribute("instance/region")).andReturn(MOCKED_QUALIFIED_REGION).once();
304+
expect(getterMock.getAttribute(anyString())).andReturn(null).anyTimes();
305+
expect(getterMock.getEnv("CLOUD_RUN_JOB")).andReturn(mockedJobName).times(2);
306+
expect(getterMock.getEnv(anyString())).andReturn(null).anyTimes();
307+
replay(getterMock);
308+
// exercise
309+
MonitoredResource response = MonitoredResourceUtil.getResource("", "");
310+
// verify
311+
assertEquals("cloud_run_job", response.getType());
312+
assertEquals(expectedLabels, response.getLabels());
313+
}
289314
}

0 commit comments

Comments
 (0)