Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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<String, String> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> availableAttributes;

GoogleCloudRunInstance(
EnvironmentVariables environmentVariables, GcpMetadataConfig metadataConfig) {
this.metadataConfig = metadataConfig;
this.environmentVariables = environmentVariables;
this.availableAttributes = prepareAttributes();
}

private Map<String, String> prepareAttributes() {
Map<String, String> 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<String, String> getAttributes() {
return this.availableAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> availableAttributes;

GoogleCloudRunWorkerPool(
EnvironmentVariables environmentVariables, GcpMetadataConfig metadataConfig) {
this.metadataConfig = metadataConfig;
this.environmentVariables = environmentVariables;
this.availableAttributes = prepareAttributes();
}

private Map<String, String> prepareAttributes() {
Map<String, String> 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<String, String> getAttributes() {
return this.availableAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,37 @@ private static DetectedPlatform generateMockGcrJobPlatform() {
return mockServerlessPlatform;
}

private static DetectedPlatform generateMockGcrWorkerPoolPlatform() {
Map<String, String> 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<String, String> 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<String, String> mockAttributes =
new HashMap<>(
Expand Down Expand Up @@ -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<String, String> 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<String, String> 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(
Expand All @@ -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);
Expand Down Expand Up @@ -447,6 +558,26 @@ private static Stream<Arguments> 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,
Expand Down
Loading
Loading