From f2164f7c9b3dbef1097df1d55a104bedcdf79c93 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Tue, 17 Nov 2026 20:32:23 +0000 Subject: [PATCH] gcp-resources: populate cloud.resource_id on Cloud Run and Cloud Functions Populate the incubating cloud.resource_id resource attribute for Cloud Run services, Cloud Run jobs, and Cloud Functions when project ID, region, and service/job/function name are all non-empty. --- gcp-resources/README.md | 1 + .../gcp/resource/GCPResourceProvider.java | 57 +++++++-- .../gcp/resource/IncubatingAttributes.java | 2 + .../gcp/resource/GCPResourceProviderTest.java | 109 ++++++++++++++++-- 4 files changed, 154 insertions(+), 15 deletions(-) diff --git a/gcp-resources/README.md b/gcp-resources/README.md index 7ee66e5529..da300db95f 100644 --- a/gcp-resources/README.md +++ b/gcp-resources/README.md @@ -13,6 +13,7 @@ The following OpenTelemetry semantic conventions will be detected: | cloud.account.id | auto | auto | auto | auto | auto | | cloud.availability_zone | auto | auto | auto | auto | auto | | cloud.region | auto | auto | auto | auto | auto | +| cloud.resource_id | | | auto | auto | | | host.id | auto | auto | | | | | host.name | auto | auto | | | | | host.type | auto | auto | | | | diff --git a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProvider.java b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProvider.java index db33961d1c..3b83a4aa3b 100644 --- a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProvider.java +++ b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProvider.java @@ -34,6 +34,7 @@ import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CLOUD_PLATFORM; import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CLOUD_PROVIDER; import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CLOUD_REGION; +import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CLOUD_RESOURCE_ID; import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CloudPlatformIncubatingValues.GCP; import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CloudPlatformIncubatingValues.GCP_APP_ENGINE; import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.CloudPlatformIncubatingValues.GCP_CLOUD_FUNCTIONS; @@ -59,6 +60,7 @@ import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; +import javax.annotation.Nullable; /** Resource provider for the detected Google Cloud Platform environment. */ @SuppressWarnings("MemberName") @@ -97,21 +99,22 @@ public Attributes getAttributes() { // This is running on some sort of GCPCompute - figure out the platform AttributesBuilder attrBuilder = Attributes.builder(); + String projectId = detectedPlatform.getProjectId(); attrBuilder.put(CLOUD_PROVIDER, GCP); - attrBuilder.put(CLOUD_ACCOUNT_ID, detectedPlatform.getProjectId()); + attrBuilder.put(CLOUD_ACCOUNT_ID, projectId); switch (detectedPlatform.getSupportedPlatform()) { case GOOGLE_KUBERNETES_ENGINE: addGkeAttributes(attrBuilder, detectedPlatform.getAttributes()); break; case GOOGLE_CLOUD_RUN: - addGcrAttributes(attrBuilder, detectedPlatform.getAttributes()); + addGcrAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); break; case GOOGLE_CLOUD_FUNCTIONS: - addGcfAttributes(attrBuilder, detectedPlatform.getAttributes()); + addGcfAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); break; case GOOGLE_CLOUD_RUN_JOB: - addGcrJobAttributes(attrBuilder, detectedPlatform.getAttributes()); + addGcrJobAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); break; case GOOGLE_APP_ENGINE: addGaeAttributes(attrBuilder, detectedPlatform.getAttributes()); @@ -203,9 +206,17 @@ private static void addGkeAttributes( * necessary keys. */ private static void addGcrAttributes( - AttributesBuilder attrBuilder, Map attributesMap) { + AttributesBuilder attrBuilder, + Map attributesMap, + @Nullable String projectId) { attrBuilder.put(CLOUD_PLATFORM, GCP_CLOUD_RUN); addCommonAttributesForServerlessCompute(attrBuilder, attributesMap); + addCloudResourceId( + attrBuilder, + "//run.googleapis.com/projects/%s/locations/%s/services/%s", + projectId, + attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION), + attributesMap.get(SERVERLESS_COMPUTE_NAME)); } /** @@ -216,9 +227,17 @@ private static void addGcrAttributes( * necessary keys. */ private static void addGcfAttributes( - AttributesBuilder attrBuilder, Map attributesMap) { + AttributesBuilder attrBuilder, + Map attributesMap, + @Nullable String projectId) { attrBuilder.put(CLOUD_PLATFORM, GCP_CLOUD_FUNCTIONS); addCommonAttributesForServerlessCompute(attrBuilder, attributesMap); + addCloudResourceId( + attrBuilder, + "//cloudfunctions.googleapis.com/projects/%s/locations/%s/functions/%s", + projectId, + attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION), + attributesMap.get(SERVERLESS_COMPUTE_NAME)); } /** @@ -229,7 +248,9 @@ private static void addGcfAttributes( * necessary keys. */ private static void addGcrJobAttributes( - AttributesBuilder attrBuilder, Map attributesMap) { + AttributesBuilder attrBuilder, + Map attributesMap, + @Nullable String projectId) { attrBuilder.put(CLOUD_PLATFORM, GCP_CLOUD_RUN); Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_NAME)) .ifPresent(name -> attrBuilder.put(FAAS_NAME, name)); @@ -254,6 +275,12 @@ private static void addGcrJobAttributes( e); } }); + addCloudResourceId( + attrBuilder, + "//run.googleapis.com/projects/%s/locations/%s/jobs/%s", + projectId, + attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION), + attributesMap.get(SERVERLESS_COMPUTE_NAME)); } /** @@ -299,4 +326,20 @@ private static void addCommonAttributesForServerlessCompute( Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION)) .ifPresent(region -> attrBuilder.put(CLOUD_REGION, region)); } + + private static void addCloudResourceId( + AttributesBuilder attrBuilder, + String format, + @Nullable String projectId, + @Nullable String region, + @Nullable String name) { + if (projectId != null + && !projectId.isEmpty() + && region != null + && !region.isEmpty() + && name != null + && !name.isEmpty()) { + attrBuilder.put(CLOUD_RESOURCE_ID, String.format(format, projectId, region, name)); + } + } } diff --git a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/IncubatingAttributes.java b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/IncubatingAttributes.java index 2f0f937f03..b11064870c 100644 --- a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/IncubatingAttributes.java +++ b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/IncubatingAttributes.java @@ -22,6 +22,8 @@ class IncubatingAttributes { public static final AttributeKey CLOUD_PROVIDER = AttributeKey.stringKey("cloud.provider"); public static final AttributeKey CLOUD_REGION = AttributeKey.stringKey("cloud.region"); + public static final AttributeKey CLOUD_RESOURCE_ID = + AttributeKey.stringKey("cloud.resource_id"); public static final class CloudPlatformIncubatingValues { diff --git a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java index 42630b3ddd..e83aa6b9f1 100644 --- a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java +++ b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java @@ -37,6 +37,7 @@ import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_PLATFORM; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_PROVIDER; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_REGION; +import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_RESOURCE_ID; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CloudPlatformIncubatingValues.GCP_APP_ENGINE; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CloudPlatformIncubatingValues.GCP_CLOUD_FUNCTIONS; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CloudPlatformIncubatingValues.GCP_CLOUD_RUN; @@ -66,7 +67,11 @@ import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class GCPResourceProviderTest { private static final String DUMMY_PROJECT_ID = "google-pid"; @@ -206,7 +211,8 @@ void testGceResourceAttributesMapping() { .containsEntry(GCP_GCE_INSTANCE_HOSTNAME, detectedAttributes.get(GCE_INSTANCE_HOSTNAME)) .containsEntry(HOST_TYPE, detectedAttributes.get(GCE_MACHINE_TYPE)) .containsEntry(CLOUD_AVAILABILITY_ZONE, detectedAttributes.get(GCE_AVAILABILITY_ZONE)) - .containsEntry(CLOUD_REGION, detectedAttributes.get(GCE_CLOUD_REGION)); + .containsEntry(CLOUD_REGION, detectedAttributes.get(GCE_CLOUD_REGION)) + .doesNotContainKey(CLOUD_RESOURCE_ID); } @Test @@ -223,7 +229,8 @@ void testGkeResourceAttributesMapping_LocationTypeRegion() { .hasSize(6) .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID) .containsEntry(CLOUD_REGION, mockPlatform.getAttributes().get(GKE_CLUSTER_LOCATION)) - .doesNotContainKey(CLOUD_AVAILABILITY_ZONE); + .doesNotContainKey(CLOUD_AVAILABILITY_ZONE) + .doesNotContainKey(CLOUD_RESOURCE_ID); } @Test @@ -306,9 +313,12 @@ void testGcrServiceResourceAttributesMapping() { verifyServerlessMapping(gotResource, mockPlatform); assertThat(gotResource.getAttributes()) - .hasSize(8) + .hasSize(9) .containsEntry(CLOUD_PLATFORM, GCP_CLOUD_RUN) - .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID); + .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID) + .containsEntry( + CLOUD_RESOURCE_ID, + "//run.googleapis.com/projects/google-pid/locations/us-central1/services/serverless-app"); } @Test @@ -324,9 +334,12 @@ void testGcfResourceAttributeMapping() { verifyServerlessMapping(gotResource, mockPlatform); assertThat(gotResource.getAttributes()) - .hasSize(8) + .hasSize(9) .containsEntry(CLOUD_PLATFORM, GCP_CLOUD_FUNCTIONS) - .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID); + .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID) + .containsEntry( + CLOUD_RESOURCE_ID, + "//cloudfunctions.googleapis.com/projects/google-pid/locations/us-central1/functions/serverless-app"); } private static void verifyServerlessMapping( @@ -353,10 +366,13 @@ void testGcrJobResourceAttributesMapping() { verify(mockPlatform, times(1)).getProjectId(); assertThat(gotResource.getAttributes()) - .hasSize(8) + .hasSize(9) .containsEntry(CLOUD_PROVIDER, GCP) .containsEntry(CLOUD_PLATFORM, GCP_CLOUD_RUN) .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID) + .containsEntry( + CLOUD_RESOURCE_ID, + "//run.googleapis.com/projects/google-pid/locations/us-central1/jobs/serverless-job") .containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME)) .containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME)) .containsEntry(FAAS_INSTANCE, detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)) @@ -367,6 +383,82 @@ void testGcrJobResourceAttributesMapping() { .containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)); } + @ParameterizedTest + @MethodSource("provideMissingCloudResourceIdCases") + void testServerlessOmitsCloudResourceIdWhenComponentMissing( + GcpPlatformDetector.SupportedPlatform platform, + String projectId, + String region, + String name) { + GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class); + DetectedPlatform mockPlatform = + platform == GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB + ? generateMockGcrJobPlatform() + : generateMockServerlessPlatform(platform); + when(mockPlatform.getProjectId()).thenReturn(projectId); + if (region == null) { + mockPlatform.getAttributes().remove(SERVERLESS_COMPUTE_CLOUD_REGION); + } else { + mockPlatform.getAttributes().put(SERVERLESS_COMPUTE_CLOUD_REGION, region); + } + if (name == null) { + mockPlatform.getAttributes().remove(SERVERLESS_COMPUTE_NAME); + } else { + mockPlatform.getAttributes().put(SERVERLESS_COMPUTE_NAME, name); + } + when(mockDetector.detectPlatform()).thenReturn(mockPlatform); + + Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps); + + assertThat(gotResource.getAttributes()) + .containsEntry(CLOUD_PROVIDER, GCP) + .doesNotContainKey(CLOUD_RESOURCE_ID); + } + + private static Stream provideMissingCloudResourceIdCases() { + return Stream.of( + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN, + DUMMY_PROJECT_ID, + null, + "serverless-app"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN, + DUMMY_PROJECT_ID, + "", + "serverless-app"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN, + DUMMY_PROJECT_ID, + "us-central1", + null), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN, + "", + "us-central1", + "serverless-app"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB, + DUMMY_PROJECT_ID, + null, + "serverless-job"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB, + DUMMY_PROJECT_ID, + "us-central1", + ""), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS, + DUMMY_PROJECT_ID, + null, + "serverless-app"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS, + null, + "us-central1", + "serverless-app")); + } + @Test void testGcrJobResourceAttributesMappingWithMalformedTaskIndex() { GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class); @@ -400,7 +492,8 @@ void testGaeResourceAttributeMapping() { .containsEntry(FAAS_VERSION, detectedAttributes.get(GAE_APP_VERSION)) .containsEntry(FAAS_INSTANCE, detectedAttributes.get(GAE_INSTANCE_ID)) .containsEntry(CLOUD_AVAILABILITY_ZONE, detectedAttributes.get(GAE_AVAILABILITY_ZONE)) - .containsEntry(CLOUD_REGION, detectedAttributes.get(GAE_CLOUD_REGION)); + .containsEntry(CLOUD_REGION, detectedAttributes.get(GAE_CLOUD_REGION)) + .doesNotContainKey(CLOUD_RESOURCE_ID); } @Test