Skip to content

Commit 1dd64d0

Browse files
cindy-pengcindy-penggcf-owl-bot[bot]
authored
feat: Add Cloud Run Jobs support (#1574)
* feat: Add Cloud Run Jobs support * fixing format * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add cloud run task attempt * fix lint error * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: cindy-peng <cindypeng@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 483c5e0 commit 1dd64d0

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ 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.CloudRunJobName, this::getCloudRunJobName)
44+
.put(Label.CloudRunJobExecutionName, this::getCloudRunJobExecutionName)
45+
.put(Label.CloudRunJobTaskIndex, this::getCloudRunJobTaskIndex)
46+
.put(Label.CloudRunJobTaskAttempt, this::getCloudRunJobTaskAttempt)
4347
.put(Label.CloudRunLocation, this::getCloudRunLocation)
4448
.put(Label.GKELocation, this::getGKELocation)
4549
.put(Label.ModuleId, this::getModuleId)
@@ -84,6 +88,7 @@ private String getConfigName() {
8488
private String getContainerName() {
8589
return getter.getEnv("CONTAINER_NAME");
8690
}
91+
8792
/**
8893
* Distinguish between Standard and Flexible GAE environments. There is no indicator of the
8994
* environment. The path to the startup-script in the metadata attribute was selected as one of
@@ -121,6 +126,22 @@ private String getInstanceName() {
121126
return getter.getAttribute("instance/name");
122127
}
123128

129+
private String getCloudRunJobName() {
130+
return getter.getEnv("CLOUD_RUN_JOB");
131+
}
132+
133+
private String getCloudRunJobExecutionName() {
134+
return getter.getEnv("CLOUD_RUN_EXECUTION");
135+
}
136+
137+
private String getCloudRunJobTaskIndex() {
138+
return getter.getEnv("CLOUD_RUN_TASK_INDEX");
139+
}
140+
141+
private String getCloudRunJobTaskAttempt() {
142+
return getter.getEnv("CLOUD_RUN_TASK_ATTEMPT");
143+
}
144+
124145
private String getCloudRunLocation() {
125146
return getRegion();
126147
}
@@ -132,6 +153,7 @@ private String getGKELocation() {
132153
private String getModuleId() {
133154
return getter.getEnv("GAE_SERVICE");
134155
}
156+
135157
/**
136158
* Heuristic to discover the namespace name of the current environment. There is no deterministic
137159
* way to discover the namespace name of the process. The name is read from the {@link
@@ -155,6 +177,7 @@ private String getNamespaceName() {
155177
}
156178
return value;
157179
}
180+
158181
// Kubernetes set hostname of the pod to the pod name by default, however hostname can be override
159182
// in manifest
160183
// allow users to provide the container name via environment variable
@@ -169,6 +192,7 @@ private String getPodName() {
169192
private String getProjectId() {
170193
return getter.getAttribute("project/project-id");
171194
}
195+
172196
/**
173197
* Retrieves a region from the qualified region of 'projects/[PROJECT_NUMBER]/regions/[REGION]'
174198
*
@@ -193,6 +217,7 @@ private String getServiceName() {
193217
private String getVersionId() {
194218
return getter.getEnv("GAE_VERSION");
195219
}
220+
196221
/**
197222
* Retrieves a zone from the qualified zone of 'projects/[PROJECT_NUMBER]/zones/[ZONE]'
198223
*

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

+21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
public class MonitoredResourceUtil {
3333

3434
private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/";
35+
private static final String CLOUD_RUN_JOB_LABEL_PREFIX = "run.googleapis.com/";
3536
protected static final String PORJECTID_LABEL = Label.ProjectId.getKey();
3637

3738
protected enum Label {
@@ -42,6 +43,10 @@ protected enum Label {
4243
FunctionName("function_name"),
4344
InstanceId("instance_id"),
4445
InstanceName("instance_name"),
46+
CloudRunJobName("job_name"),
47+
CloudRunJobExecutionName("execution_name"),
48+
CloudRunJobTaskIndex("task_index"),
49+
CloudRunJobTaskAttempt("task_attempt"),
4550
CloudRunLocation("location"),
4651
GKELocation("location"),
4752
ModuleId("module_id"),
@@ -67,6 +72,7 @@ String getKey() {
6772

6873
private enum Resource {
6974
CLOUD_RUN("cloud_run_revision"),
75+
CLOUD_RUN_JOB("cloud_run_job"),
7076
CLOUD_FUNCTION("cloud_function"),
7177
APP_ENGINE("gae_app"),
7278
GCE_INSTANCE("gce_instance"),
@@ -93,6 +99,7 @@ String getKey() {
9399
Label.ServiceName,
94100
Label.CloudRunLocation,
95101
Label.ConfigurationName)
102+
.putAll(Resource.CLOUD_RUN_JOB.getKey(), Label.CloudRunJobName, Label.CloudRunLocation)
96103
.putAll(
97104
Resource.APP_ENGINE.getKey(), Label.ModuleId, Label.VersionId, Label.Zone, Label.Env)
98105
.putAll(Resource.GCE_INSTANCE.getKey(), Label.InstanceId, Label.Zone)
@@ -177,6 +184,12 @@ private static Resource detectResourceType() {
177184
&& getter.getEnv("K_CONFIGURATION") != null) {
178185
return Resource.CLOUD_RUN;
179186
}
187+
if (getter.getEnv("CLOUD_RUN_JOB") != null
188+
&& getter.getEnv("CLOUD_RUN_EXECUTION") != null
189+
&& getter.getEnv("CLOUD_RUN_TASK_INDEX") != null
190+
&& getter.getEnv("CLOUD_RUN_TASK_ATTEMPT") != null) {
191+
return Resource.CLOUD_RUN_JOB;
192+
}
180193
if (getter.getEnv("GAE_INSTANCE") != null
181194
&& getter.getEnv("GAE_SERVICE") != null
182195
&& getter.getEnv("GAE_VERSION") != null) {
@@ -212,6 +225,14 @@ private static List<LoggingEnhancer> createEnhancers(Resource resourceType) {
212225
enhancers.add(
213226
new LabelLoggingEnhancer(APPENGINE_LABEL_PREFIX, ImmutableList.of(Label.InstanceName)));
214227
}
228+
} else if (resourceType == Resource.CLOUD_RUN_JOB) {
229+
enhancers.add(
230+
new LabelLoggingEnhancer(
231+
CLOUD_RUN_JOB_LABEL_PREFIX,
232+
ImmutableList.of(
233+
Label.CloudRunJobExecutionName,
234+
Label.CloudRunJobTaskIndex,
235+
Label.CloudRunJobTaskAttempt)));
215236
}
216237
return enhancers;
217238
}

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

+28
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,32 @@ 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("CLOUD_RUN_EXECUTION")).andReturn(mockedJobName + "_1").times(1);
307+
expect(getterMock.getEnv("CLOUD_RUN_TASK_INDEX")).andReturn("0").times(1);
308+
expect(getterMock.getEnv("CLOUD_RUN_TASK_ATTEMPT")).andReturn("0").times(1);
309+
expect(getterMock.getEnv(anyString())).andReturn(null).anyTimes();
310+
replay(getterMock);
311+
// exercise
312+
MonitoredResource response = MonitoredResourceUtil.getResource("", "");
313+
// verify
314+
assertEquals("cloud_run_job", response.getType());
315+
assertEquals(expectedLabels, response.getLabels());
316+
}
289317
}

0 commit comments

Comments
 (0)