Skip to content

Commit

Permalink
feat: Cloud Run Jobs support
Browse files Browse the repository at this point in the history
  • Loading branch information
kberezin-nshl committed Jan 24, 2024
1 parent b40e846 commit ea392d0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -212,6 +225,10 @@ private static List<LoggingEnhancer> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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());
}
}

0 comments on commit ea392d0

Please sign in to comment.