From 7d7ff6d4abc4e66a1aa46166abb48661e1282f16 Mon Sep 17 00:00:00 2001 From: Dylan Russell Date: Thu, 19 Nov 2026 20:01:51 +0000 Subject: [PATCH 1/2] gcp-resources: detect Cloud Run functions as Cloud Run and add worker pool support --- .../gcp/resource/GCPResourceProvider.java | 25 ++++++ .../gcp/resource/GcpPlatformDetector.java | 13 ++- .../resource/GoogleCloudRunWorkerPool.java | 51 +++++++++++ .../gcp/resource/GCPResourceProviderTest.java | 86 ++++++++++++++++++- .../gcp/resource/GcpPlatformDetectorTest.java | 48 +++++++++-- 5 files changed, 209 insertions(+), 14 deletions(-) create mode 100644 gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunWorkerPool.java 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 3b83a4aa3b..28844e43bc 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 @@ -116,6 +116,9 @@ public Attributes getAttributes() { case GOOGLE_CLOUD_RUN_JOB: addGcrJobAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); break; + case GOOGLE_CLOUD_RUN_WORKER_POOL: + addGcrWorkerPoolAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); + break; case GOOGLE_APP_ENGINE: addGaeAttributes(attrBuilder, detectedPlatform.getAttributes()); break; @@ -283,6 +286,28 @@ private static void addGcrJobAttributes( attributesMap.get(SERVERLESS_COMPUTE_NAME)); } + /** + * Updates the attributes with the required keys for a GCR (Google Cloud Run) Worker Pool + * environment. The attributes are not updated in case the environment is not deemed to be a GCR + * worker pool environment. + * + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the + * necessary keys. + */ + private static void addGcrWorkerPoolAttributes( + 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/workerPools/%s", + projectId, + attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION), + attributesMap.get(SERVERLESS_COMPUTE_NAME)); + } + /** * Updates the attributes with the required keys for a GAE (Google App Engine) environment. The * attributes are not updated in case the environment is not deemed to be GAE. diff --git a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java index c668e8e122..d9a711255d 100644 --- a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java +++ b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java @@ -38,13 +38,17 @@ private SupportedPlatform detectSupportedPlatform() { // Note: Order of detection matters here if (environmentVariables.get("KUBERNETES_SERVICE_HOST") != null) { return SupportedPlatform.GOOGLE_KUBERNETES_ENGINE; - } else if (environmentVariables.get("K_CONFIGURATION") != null - && environmentVariables.get("FUNCTION_TARGET") == null) { + } else if (environmentVariables.get("K_CONFIGURATION") != null) { + // Check Cloud Run before Cloud Functions because Cloud Run functions (including + // Cloud Functions 2nd gen) set both K_CONFIGURATION and FUNCTION_TARGET, whereas + // Cloud Functions 1st gen only sets FUNCTION_TARGET. return SupportedPlatform.GOOGLE_CLOUD_RUN; } else if (environmentVariables.get("FUNCTION_TARGET") != null) { return SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS; } else if (environmentVariables.get("CLOUD_RUN_JOB") != null) { return SupportedPlatform.GOOGLE_CLOUD_RUN_JOB; + } else if (environmentVariables.get("CLOUD_RUN_WORKER_POOL") != null) { + return SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL; } else if (environmentVariables.get("GAE_SERVICE") != null) { return SupportedPlatform.GOOGLE_APP_ENGINE; } @@ -70,6 +74,9 @@ private DetectedPlatform generateDetectedPlatform(SupportedPlatform platform) { case GOOGLE_CLOUD_RUN_JOB: detectedPlatform = new GoogleCloudRunJob(environmentVariables, metadataConfig); break; + case GOOGLE_CLOUD_RUN_WORKER_POOL: + detectedPlatform = new GoogleCloudRunWorkerPool(environmentVariables, metadataConfig); + break; case GOOGLE_APP_ENGINE: detectedPlatform = new GoogleAppEngine(environmentVariables, metadataConfig); break; @@ -97,6 +104,8 @@ enum SupportedPlatform { GOOGLE_CLOUD_RUN, /** Represents the Google Cloud Run platform (Jobs). */ GOOGLE_CLOUD_RUN_JOB, + /** Represents the Google Cloud Run platform (Worker Pools). */ + GOOGLE_CLOUD_RUN_WORKER_POOL, /** Represents the Google Cloud Functions platform. */ GOOGLE_CLOUD_FUNCTIONS, /** Represents the case when the application is not running on GCP. */ diff --git a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunWorkerPool.java b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunWorkerPool.java new file mode 100644 index 0000000000..5039b5c92e --- /dev/null +++ b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunWorkerPool.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.contrib.gcp.resource; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +final class GoogleCloudRunWorkerPool implements DetectedPlatform { + private final GcpMetadataConfig metadataConfig; + private final EnvironmentVariables environmentVariables; + private final Map availableAttributes; + + GoogleCloudRunWorkerPool( + EnvironmentVariables environmentVariables, GcpMetadataConfig metadataConfig) { + this.metadataConfig = metadataConfig; + this.environmentVariables = environmentVariables; + this.availableAttributes = prepareAttributes(); + } + + private Map prepareAttributes() { + Map map = new HashMap<>(); + map.put( + AttributeKeys.SERVERLESS_COMPUTE_NAME, + this.environmentVariables.get("CLOUD_RUN_WORKER_POOL")); + map.put( + AttributeKeys.SERVERLESS_COMPUTE_REVISION, + this.environmentVariables.get("CLOUD_RUN_REVISION")); + map.put(AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID, this.metadataConfig.getInstanceId()); + map.put(AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION, this.metadataConfig.getRegion()); + return Collections.unmodifiableMap(map); + } + + @Override + public GcpPlatformDetector.SupportedPlatform getSupportedPlatform() { + return GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL; + } + + @Override + public String getProjectId() { + return this.metadataConfig.getProjectId(); + } + + @Override + public Map getAttributes() { + return this.availableAttributes; + } +} 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 e83aa6b9f1..9ad06bc0e6 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 @@ -159,6 +159,22 @@ private static DetectedPlatform generateMockGcrJobPlatform() { return mockServerlessPlatform; } + private static DetectedPlatform generateMockGcrWorkerPoolPlatform() { + Map mockAttributes = + new HashMap<>( + ImmutableMap.of( + SERVERLESS_COMPUTE_NAME, "serverless-worker-pool", + SERVERLESS_COMPUTE_REVISION, "serverless-worker-pool-00001", + SERVERLESS_COMPUTE_INSTANCE_ID, "serverless-instance-id", + SERVERLESS_COMPUTE_CLOUD_REGION, "us-central1")); + DetectedPlatform mockServerlessPlatform = mock(DetectedPlatform.class); + when(mockServerlessPlatform.getSupportedPlatform()) + .thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL); + when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes); + when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID); + return mockServerlessPlatform; + } + private static DetectedPlatform generateMockGaePlatform() { Map mockAttributes = new HashMap<>( @@ -383,6 +399,50 @@ void testGcrJobResourceAttributesMapping() { .containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)); } + @Test + void testGcrWorkerPoolResourceAttributesMapping() { + GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class); + DetectedPlatform mockPlatform = generateMockGcrWorkerPoolPlatform(); + when(mockDetector.detectPlatform()).thenReturn(mockPlatform); + Map detectedAttributes = mockPlatform.getAttributes(); + + Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps); + verify(mockPlatform, times(1)).getProjectId(); + + assertThat(gotResource.getAttributes()) + .hasSize(8) + .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/workerPools/serverless-worker-pool") + .containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME)) + .containsEntry(FAAS_VERSION, detectedAttributes.get(SERVERLESS_COMPUTE_REVISION)) + .containsEntry(FAAS_INSTANCE, detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)) + .containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)) + .doesNotContainKey(CLOUD_AVAILABILITY_ZONE); + } + + @Test + void testGcrWorkerPoolResourceAttributesMappingWithoutRevision() { + GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class); + DetectedPlatform mockPlatform = generateMockGcrWorkerPoolPlatform(); + mockPlatform.getAttributes().remove(SERVERLESS_COMPUTE_REVISION); + when(mockDetector.detectPlatform()).thenReturn(mockPlatform); + + Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps); + + assertThat(gotResource.getAttributes()) + .hasSize(7) + .containsEntry(CLOUD_PROVIDER, GCP) + .containsEntry(CLOUD_PLATFORM, GCP_CLOUD_RUN) + .containsEntry( + CLOUD_RESOURCE_ID, + "//run.googleapis.com/projects/google-pid/locations/us-central1/workerPools/serverless-worker-pool") + .doesNotContainKey(FAAS_VERSION); + } + @ParameterizedTest @MethodSource("provideMissingCloudResourceIdCases") void testServerlessOmitsCloudResourceIdWhenComponentMissing( @@ -391,10 +451,18 @@ void testServerlessOmitsCloudResourceIdWhenComponentMissing( String region, String name) { GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class); - DetectedPlatform mockPlatform = - platform == GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB - ? generateMockGcrJobPlatform() - : generateMockServerlessPlatform(platform); + DetectedPlatform mockPlatform; + switch (platform) { + case GOOGLE_CLOUD_RUN_JOB: + mockPlatform = generateMockGcrJobPlatform(); + break; + case GOOGLE_CLOUD_RUN_WORKER_POOL: + mockPlatform = generateMockGcrWorkerPoolPlatform(); + break; + default: + mockPlatform = generateMockServerlessPlatform(platform); + break; + } when(mockPlatform.getProjectId()).thenReturn(projectId); if (region == null) { mockPlatform.getAttributes().remove(SERVERLESS_COMPUTE_CLOUD_REGION); @@ -447,6 +515,16 @@ private static Stream provideMissingCloudResourceIdCases() { DUMMY_PROJECT_ID, "us-central1", ""), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL, + DUMMY_PROJECT_ID, + null, + "serverless-worker-pool"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL, + DUMMY_PROJECT_ID, + "us-central1", + ""), Arguments.of( GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS, DUMMY_PROJECT_ID, diff --git a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java index b3c5e75cd3..5353c3acff 100644 --- a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java +++ b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java @@ -263,27 +263,27 @@ void testGCFResourceWithCloudFunctionAttributesSucceeds() { } @Test - void testGCFDetectionWhenGCRAttributesPresent() { - // Setup GCF required env vars + void testCloudRunDetectionWhenFunctionTargetPresent() { + // Cloud Run functions (including Cloud Functions 2nd gen) set both K_CONFIGURATION + // and FUNCTION_TARGET; they should be detected as Cloud Run. envVars.put("K_SERVICE", "cloud-function-hello"); envVars.put("K_REVISION", "cloud-function-hello.1"); envVars.put("FUNCTION_TARGET", "cloud-function-hello"); - // This should be ignored and detected platform should still be GCF envVars.put("K_CONFIGURATION", "cloud-run-hello"); - TestUtils.stubEndpoint("/project/project-id", "GCF-pid"); + TestUtils.stubEndpoint("/project/project-id", "GCR-pid"); TestUtils.stubEndpoint("/instance/zone", "country-region-zone"); - TestUtils.stubEndpoint("/instance/id", "GCF-instance-id"); + TestUtils.stubEndpoint("/instance/id", "GCR-instance-id"); EnvironmentVariables mockEnv = new EnvVarMock(envVars); GcpPlatformDetector detector = new GcpPlatformDetector(mockMetadataConfig, mockEnv); assertEquals( - GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS, + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN, detector.detectPlatform().getSupportedPlatform()); - assertEquals("GCF-pid", detector.detectPlatform().getProjectId()); + assertEquals("GCR-pid", detector.detectPlatform().getProjectId()); assertEquals( - new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(), + new GoogleCloudRun(mockEnv, mockMetadataConfig).getAttributes(), detector.detectPlatform().getAttributes()); } @@ -350,6 +350,38 @@ void testCloudRunJobResourceWithAttributesSucceeds() { assertEquals("GCR-job-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)); } + /** Google Cloud Run Tests (Worker Pools) * */ + @Test + void testCloudRunWorkerPoolResourceWithAttributesSucceeds() { + // Setup GCR Worker Pool required env vars + envVars.put("CLOUD_RUN_WORKER_POOL", "cloud-run-hello-worker-pool"); + envVars.put("CLOUD_RUN_REVISION", "cloud-run-hello-worker-pool-00001"); + + TestUtils.stubEndpoint("/project/project-id", "GCR-pid"); + TestUtils.stubEndpoint("/instance/region", "projects/123456789/regions/us-central1"); + TestUtils.stubEndpoint("/instance/id", "GCR-worker-pool-instance-id"); + + EnvironmentVariables mockEnv = new EnvVarMock(envVars); + GcpPlatformDetector detector = new GcpPlatformDetector(mockMetadataConfig, mockEnv); + + Map detectedAttributes = detector.detectPlatform().getAttributes(); + assertEquals( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL, + detector.detectPlatform().getSupportedPlatform()); + assertEquals( + new GoogleCloudRunWorkerPool(mockEnv, mockMetadataConfig).getAttributes(), + detectedAttributes); + assertEquals("GCR-pid", detector.detectPlatform().getProjectId()); + assertEquals(4, detectedAttributes.size()); + + assertEquals("cloud-run-hello-worker-pool", detectedAttributes.get(SERVERLESS_COMPUTE_NAME)); + assertEquals( + "cloud-run-hello-worker-pool-00001", detectedAttributes.get(SERVERLESS_COMPUTE_REVISION)); + assertEquals("us-central1", detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)); + assertEquals( + "GCR-worker-pool-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)); + } + /** Google App Engine Tests * */ @ParameterizedTest @MethodSource("provideGAEVariantEnvironmentVariable") From cf1beb6a560118b81b3176c39bde9a02296543e5 Mon Sep 17 00:00:00 2001 From: Dylan Russell Date: Thu, 19 Nov 2026 21:22:57 +0000 Subject: [PATCH 2/2] gcp-resources: detect Cloud Run instances --- .../gcp/resource/GCPResourceProvider.java | 25 +++++++++ .../gcp/resource/GcpPlatformDetector.java | 7 +++ .../gcp/resource/GoogleCloudRunInstance.java | 47 ++++++++++++++++ .../gcp/resource/GCPResourceProviderTest.java | 53 +++++++++++++++++++ .../gcp/resource/GcpPlatformDetectorTest.java | 27 ++++++++++ 5 files changed, 159 insertions(+) create mode 100644 gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunInstance.java 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 28844e43bc..7a36600fb0 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 @@ -119,6 +119,9 @@ public Attributes getAttributes() { case GOOGLE_CLOUD_RUN_WORKER_POOL: addGcrWorkerPoolAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); break; + case GOOGLE_CLOUD_RUN_INSTANCE: + addGcrInstanceAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); + break; case GOOGLE_APP_ENGINE: addGaeAttributes(attrBuilder, detectedPlatform.getAttributes()); break; @@ -308,6 +311,28 @@ private static void addGcrWorkerPoolAttributes( attributesMap.get(SERVERLESS_COMPUTE_NAME)); } + /** + * Updates the attributes with the required keys for a GCR (Google Cloud Run) Instance + * environment. The attributes are not updated in case the environment is not deemed to be a GCR + * instance environment. + * + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the + * necessary keys. + */ + private static void addGcrInstanceAttributes( + 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/instances/%s", + projectId, + attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION), + attributesMap.get(SERVERLESS_COMPUTE_NAME)); + } + /** * Updates the attributes with the required keys for a GAE (Google App Engine) environment. The * attributes are not updated in case the environment is not deemed to be GAE. diff --git a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java index d9a711255d..10cd7fa9bc 100644 --- a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java +++ b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetector.java @@ -49,6 +49,8 @@ private SupportedPlatform detectSupportedPlatform() { return SupportedPlatform.GOOGLE_CLOUD_RUN_JOB; } else if (environmentVariables.get("CLOUD_RUN_WORKER_POOL") != null) { return SupportedPlatform.GOOGLE_CLOUD_RUN_WORKER_POOL; + } else if (environmentVariables.get("CLOUD_RUN_INSTANCE") != null) { + return SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE; } else if (environmentVariables.get("GAE_SERVICE") != null) { return SupportedPlatform.GOOGLE_APP_ENGINE; } @@ -77,6 +79,9 @@ private DetectedPlatform generateDetectedPlatform(SupportedPlatform platform) { case GOOGLE_CLOUD_RUN_WORKER_POOL: detectedPlatform = new GoogleCloudRunWorkerPool(environmentVariables, metadataConfig); break; + case GOOGLE_CLOUD_RUN_INSTANCE: + detectedPlatform = new GoogleCloudRunInstance(environmentVariables, metadataConfig); + break; case GOOGLE_APP_ENGINE: detectedPlatform = new GoogleAppEngine(environmentVariables, metadataConfig); break; @@ -106,6 +111,8 @@ enum SupportedPlatform { GOOGLE_CLOUD_RUN_JOB, /** Represents the Google Cloud Run platform (Worker Pools). */ GOOGLE_CLOUD_RUN_WORKER_POOL, + /** Represents the Google Cloud Run platform (Instances). */ + GOOGLE_CLOUD_RUN_INSTANCE, /** Represents the Google Cloud Functions platform. */ GOOGLE_CLOUD_FUNCTIONS, /** Represents the case when the application is not running on GCP. */ diff --git a/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunInstance.java b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunInstance.java new file mode 100644 index 0000000000..a491248448 --- /dev/null +++ b/gcp-resources/src/main/java/io/opentelemetry/contrib/gcp/resource/GoogleCloudRunInstance.java @@ -0,0 +1,47 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.contrib.gcp.resource; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +final class GoogleCloudRunInstance implements DetectedPlatform { + private final GcpMetadataConfig metadataConfig; + private final EnvironmentVariables environmentVariables; + private final Map availableAttributes; + + GoogleCloudRunInstance( + EnvironmentVariables environmentVariables, GcpMetadataConfig metadataConfig) { + this.metadataConfig = metadataConfig; + this.environmentVariables = environmentVariables; + this.availableAttributes = prepareAttributes(); + } + + private Map prepareAttributes() { + Map map = new HashMap<>(); + map.put( + AttributeKeys.SERVERLESS_COMPUTE_NAME, this.environmentVariables.get("CLOUD_RUN_INSTANCE")); + map.put(AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID, this.metadataConfig.getInstanceId()); + map.put(AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION, this.metadataConfig.getRegion()); + return Collections.unmodifiableMap(map); + } + + @Override + public GcpPlatformDetector.SupportedPlatform getSupportedPlatform() { + return GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE; + } + + @Override + public String getProjectId() { + return this.metadataConfig.getProjectId(); + } + + @Override + public Map getAttributes() { + return this.availableAttributes; + } +} 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 9ad06bc0e6..4205ee7ff0 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 @@ -175,6 +175,21 @@ private static DetectedPlatform generateMockGcrWorkerPoolPlatform() { return mockServerlessPlatform; } + private static DetectedPlatform generateMockGcrInstancePlatform() { + Map mockAttributes = + new HashMap<>( + ImmutableMap.of( + SERVERLESS_COMPUTE_NAME, "serverless-instance", + SERVERLESS_COMPUTE_INSTANCE_ID, "serverless-instance-id", + SERVERLESS_COMPUTE_CLOUD_REGION, "us-west1")); + DetectedPlatform mockInstancePlatform = mock(DetectedPlatform.class); + when(mockInstancePlatform.getSupportedPlatform()) + .thenReturn(GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE); + when(mockInstancePlatform.getAttributes()).thenReturn(mockAttributes); + when(mockInstancePlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID); + return mockInstancePlatform; + } + private static DetectedPlatform generateMockGaePlatform() { Map mockAttributes = new HashMap<>( @@ -443,6 +458,31 @@ void testGcrWorkerPoolResourceAttributesMappingWithoutRevision() { .doesNotContainKey(FAAS_VERSION); } + @Test + void testGcrInstanceResourceAttributesMapping() { + GcpPlatformDetector mockDetector = mock(GcpPlatformDetector.class); + DetectedPlatform mockPlatform = generateMockGcrInstancePlatform(); + when(mockDetector.detectPlatform()).thenReturn(mockPlatform); + Map detectedAttributes = mockPlatform.getAttributes(); + + Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps); + verify(mockPlatform, times(1)).getProjectId(); + + assertThat(gotResource.getAttributes()) + .hasSize(7) + .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-west1/instances/serverless-instance") + .containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME)) + .containsEntry(FAAS_INSTANCE, detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)) + .containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)) + .doesNotContainKey(FAAS_VERSION) + .doesNotContainKey(CLOUD_AVAILABILITY_ZONE); + } + @ParameterizedTest @MethodSource("provideMissingCloudResourceIdCases") void testServerlessOmitsCloudResourceIdWhenComponentMissing( @@ -459,6 +499,9 @@ void testServerlessOmitsCloudResourceIdWhenComponentMissing( case GOOGLE_CLOUD_RUN_WORKER_POOL: mockPlatform = generateMockGcrWorkerPoolPlatform(); break; + case GOOGLE_CLOUD_RUN_INSTANCE: + mockPlatform = generateMockGcrInstancePlatform(); + break; default: mockPlatform = generateMockServerlessPlatform(platform); break; @@ -525,6 +568,16 @@ private static Stream provideMissingCloudResourceIdCases() { DUMMY_PROJECT_ID, "us-central1", ""), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE, + DUMMY_PROJECT_ID, + null, + "serverless-instance"), + Arguments.of( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE, + DUMMY_PROJECT_ID, + "us-west1", + ""), Arguments.of( GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS, DUMMY_PROJECT_ID, diff --git a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java index 5353c3acff..951997094c 100644 --- a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java +++ b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GcpPlatformDetectorTest.java @@ -382,6 +382,33 @@ void testCloudRunWorkerPoolResourceWithAttributesSucceeds() { "GCR-worker-pool-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)); } + /** Google Cloud Run Tests (Instances) * */ + @Test + void testCloudRunInstanceResourceWithAttributesSucceeds() { + envVars.put("CLOUD_RUN_INSTANCE", "cloud-run-hello-instance"); + + TestUtils.stubEndpoint("/project/project-id", "GCR-pid"); + TestUtils.stubEndpoint("/instance/region", "projects/123456789/regions/us-west1"); + TestUtils.stubEndpoint("/instance/id", "GCR-instance-id"); + + EnvironmentVariables mockEnv = new EnvVarMock(envVars); + GcpPlatformDetector detector = new GcpPlatformDetector(mockMetadataConfig, mockEnv); + + Map detectedAttributes = detector.detectPlatform().getAttributes(); + assertEquals( + GcpPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE, + detector.detectPlatform().getSupportedPlatform()); + assertEquals( + new GoogleCloudRunInstance(mockEnv, mockMetadataConfig).getAttributes(), + detectedAttributes); + assertEquals("GCR-pid", detector.detectPlatform().getProjectId()); + assertEquals(3, detectedAttributes.size()); + + assertEquals("cloud-run-hello-instance", detectedAttributes.get(SERVERLESS_COMPUTE_NAME)); + assertEquals("us-west1", detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)); + assertEquals("GCR-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)); + } + /** Google App Engine Tests * */ @ParameterizedTest @MethodSource("provideGAEVariantEnvironmentVariable")