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 3b83a4aa3..7a36600fb 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,12 @@ 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_CLOUD_RUN_INSTANCE: + addGcrInstanceAttributes(attrBuilder, detectedPlatform.getAttributes(), projectId); + break; case GOOGLE_APP_ENGINE: addGaeAttributes(attrBuilder, detectedPlatform.getAttributes()); break; @@ -283,6 +289,50 @@ 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 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 c668e8e12..10cd7fa9b 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,19 @@ 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("CLOUD_RUN_INSTANCE") != null) { + return SupportedPlatform.GOOGLE_CLOUD_RUN_INSTANCE; } else if (environmentVariables.get("GAE_SERVICE") != null) { return SupportedPlatform.GOOGLE_APP_ENGINE; } @@ -70,6 +76,12 @@ 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_CLOUD_RUN_INSTANCE: + detectedPlatform = new GoogleCloudRunInstance(environmentVariables, metadataConfig); + break; case GOOGLE_APP_ENGINE: detectedPlatform = new GoogleAppEngine(environmentVariables, metadataConfig); break; @@ -97,6 +109,10 @@ 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 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 000000000..a49124844 --- /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/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 000000000..5039b5c92 --- /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 e83aa6b9f..4205ee7ff 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,37 @@ 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 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<>( @@ -383,6 +414,75 @@ 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); + } + + @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( @@ -391,10 +491,21 @@ 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; + case GOOGLE_CLOUD_RUN_INSTANCE: + mockPlatform = generateMockGcrInstancePlatform(); + break; + default: + mockPlatform = generateMockServerlessPlatform(platform); + break; + } when(mockPlatform.getProjectId()).thenReturn(projectId); if (region == null) { mockPlatform.getAttributes().remove(SERVERLESS_COMPUTE_CLOUD_REGION); @@ -447,6 +558,26 @@ 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_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 b3c5e75cd..951997094 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,65 @@ 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 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")