Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.34.0')
implementation platform('com.google.cloud:libraries-bom:26.35.0')

implementation 'com.google.cloud:google-cloud-logging'
```
Expand Down
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 @@ -84,6 +87,7 @@ private String getConfigName() {
private String getContainerName() {
return getter.getEnv("CONTAINER_NAME");
}

/**
* Distinguish between Standard and Flexible GAE environments. There is no indicator of the
* environment. The path to the startup-script in the metadata attribute was selected as one of
Expand Down Expand Up @@ -121,6 +125,18 @@ private String getInstanceName() {
return getter.getAttribute("instance/name");
}

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 getCloudRunLocation() {
return getRegion();
}
Expand All @@ -132,6 +148,7 @@ private String getGKELocation() {
private String getModuleId() {
return getter.getEnv("GAE_SERVICE");
}

/**
* Heuristic to discover the namespace name of the current environment. There is no deterministic
* way to discover the namespace name of the process. The name is read from the {@link
Expand All @@ -155,6 +172,7 @@ private String getNamespaceName() {
}
return value;
}

// Kubernetes set hostname of the pod to the pod name by default, however hostname can be override
// in manifest
// allow users to provide the container name via environment variable
Expand All @@ -169,6 +187,7 @@ private String getPodName() {
private String getProjectId() {
return getter.getAttribute("project/project-id");
}

/**
* Retrieves a region from the qualified region of 'projects/[PROJECT_NUMBER]/regions/[REGION]'
*
Expand All @@ -193,6 +212,7 @@ private String getServiceName() {
private String getVersionId() {
return getter.getEnv("GAE_VERSION");
}

/**
* Retrieves a zone from the qualified zone of 'projects/[PROJECT_NUMBER]/zones/[ZONE]'
*
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,7 @@ 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 +183,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 +223,11 @@ 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)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this (https://cloud.google.com/logging/docs/api/platform-logs#cloud_run) you're missing a label for task attempt.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added cloud run task attempt label into enhancer.

}
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());
}
}