Skip to content
Merged
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
1 change: 1 addition & 0 deletions gcp-resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -203,9 +206,17 @@ private static void addGkeAttributes(
* necessary keys.
*/
private static void addGcrAttributes(
AttributesBuilder attrBuilder, Map<String, String> attributesMap) {
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/services/%s",
projectId,
attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION),
attributesMap.get(SERVERLESS_COMPUTE_NAME));
}

/**
Expand All @@ -216,9 +227,17 @@ private static void addGcrAttributes(
* necessary keys.
*/
private static void addGcfAttributes(
AttributesBuilder attrBuilder, Map<String, String> attributesMap) {
AttributesBuilder attrBuilder,
Map<String, String> 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));
}

/**
Expand All @@ -229,7 +248,9 @@ private static void addGcfAttributes(
* necessary keys.
*/
private static void addGcrJobAttributes(
AttributesBuilder attrBuilder, Map<String, String> attributesMap) {
AttributesBuilder attrBuilder,
Map<String, String> 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));
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class IncubatingAttributes {
public static final AttributeKey<String> CLOUD_PROVIDER =
AttributeKey.stringKey("cloud.provider");
public static final AttributeKey<String> CLOUD_REGION = AttributeKey.stringKey("cloud.region");
public static final AttributeKey<String> CLOUD_RESOURCE_ID =
AttributeKey.stringKey("cloud.resource_id");

public static final class CloudPlatformIncubatingValues {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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))
Expand All @@ -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<Arguments> 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);
Expand Down Expand Up @@ -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
Expand Down
Loading