Skip to content

Commit

Permalink
Add CLOUD_ACCOUNT_ID to all supported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Jan 9, 2024
1 parent eb613b6 commit 4d577f0
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
public final class AttributeKeys {
// GCE Attributes
public static final String GCE_PROJECT_ID = AttributeKeys.PROJECT_ID;
public static final String GCE_AVAILABILITY_ZONE = AttributeKeys.AVAILABILITY_ZONE;
public static final String GCE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
public static final String GCE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
Expand Down Expand Up @@ -53,7 +52,6 @@ public final class AttributeKeys {
public static final String SERVERLESS_COMPUTE_CLOUD_REGION = AttributeKeys.CLOUD_REGION;
public static final String SERVERLESS_COMPUTE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;

static final String PROJECT_ID = "project_id";
static final String AVAILABILITY_ZONE = "availability_zone";
static final String CLOUD_REGION = "cloud_region";
static final String INSTANCE_ID = "instance_id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ public interface DetectedPlatform {
*/
GCPPlatformDetector.SupportedPlatform getSupportedPlatform();

/**
* Method to retrieve the GCP Project ID in which the GCP specific platform exists. Every valid
* platform must have a GCP Project ID associated with it.
*
* @return the Google Cloud project ID.
*/
String getProjectId();

/**
* Method to retrieve the attributes associated with the compute platform on which the application
* is running as key-value pairs.
* is running as key-value pairs. The valid keys to query on this {@link Map} are specified in the
* {@link AttributeKeys}.
*
* @return a {@link Map} of attributes specific to the underlying compute platform.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public GCPPlatformDetector.SupportedPlatform getSupportedPlatform() {
return GCPPlatformDetector.SupportedPlatform.GOOGLE_APP_ENGINE;
}

@Override
public String getProjectId() {
return this.metadataConfig.getProjectId();
}

@Override
public Map<String, String> getAttributes() {
return this.availableAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_INSTANCE_ID;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_INSTANCE_NAME;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_MACHINE_TYPE;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_PROJECT_ID;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -38,7 +37,6 @@ final class GoogleComputeEngine implements DetectedPlatform {

private Map<String, String> prepareAttributes() {
Map<String, String> map = new HashMap<>();
map.put(GCE_PROJECT_ID, this.metadataConfig.getProjectId());
map.put(GCE_AVAILABILITY_ZONE, this.metadataConfig.getZone());
map.put(GCE_CLOUD_REGION, this.metadataConfig.getRegionFromZone());
map.put(GCE_INSTANCE_ID, this.metadataConfig.getInstanceId());
Expand All @@ -53,6 +51,11 @@ public GCPPlatformDetector.SupportedPlatform getSupportedPlatform() {
return GCPPlatformDetector.SupportedPlatform.GOOGLE_COMPUTE_ENGINE;
}

@Override
public String getProjectId() {
return this.metadataConfig.getProjectId();
}

@Override
public Map<String, String> getAttributes() {
return this.availableAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public GCPPlatformDetector.SupportedPlatform getSupportedPlatform() {
return GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE;
}

@Override
public String getProjectId() {
return this.metadataConfig.getProjectId();
}

@Override
public Map<String, String> getAttributes() {
return this.availableAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ private Map<String, String> prepareAttributes() {
return Collections.unmodifiableMap(map);
}

@Override
public String getProjectId() {
return this.metadataConfig.getProjectId();
}

@Override
public Map<String, String> getAttributes() {
return this.availableAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public GCPPlatformDetector.SupportedPlatform getSupportedPlatform() {
return GCPPlatformDetector.SupportedPlatform.UNKNOWN_PLATFORM;
}

@Override
public String getProjectId() {
return "";
}

@Override
public Map<String, String> getAttributes() {
return Collections.emptyMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public void testGCEResourceWithGCEAttributesSucceeds() {
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_COMPUTE_ENGINE,
detector.detectPlatform().getSupportedPlatform());
assertEquals("GCE-pid", detector.detectPlatform().getProjectId());
Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(new GoogleComputeEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals(7, detectedAttributes.size());
assertEquals(6, detectedAttributes.size());

assertEquals("GCE-pid", detectedAttributes.get(GCE_PROJECT_ID));
assertEquals("country-gce_region-gce_zone", detectedAttributes.get(GCE_AVAILABILITY_ZONE));
assertEquals("country-gce_region", detectedAttributes.get(GCE_CLOUD_REGION));
assertEquals("GCE-instance-id", detectedAttributes.get(GCE_INSTANCE_ID));
Expand Down Expand Up @@ -135,6 +135,7 @@ public void testGKEResourceWithGKEAttributesSucceedsLocationZone() {
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GKE-pid", detector.detectPlatform().getProjectId());
assertEquals(4, detectedAttributes.size());

assertEquals(GKE_LOCATION_TYPE_ZONE, detectedAttributes.get(GKE_CLUSTER_LOCATION_TYPE));
Expand Down Expand Up @@ -168,7 +169,9 @@ public void testGKEResourceWithGKEAttributesSucceedsLocationRegion() {
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GKE-pid", detector.detectPlatform().getProjectId());
assertEquals(4, detectedAttributes.size());

assertEquals(GKE_LOCATION_TYPE_REGION, detectedAttributes.get(GKE_CLUSTER_LOCATION_TYPE));
assertEquals("country-region", detectedAttributes.get(GKE_CLUSTER_LOCATION));
assertEquals("GKE-cluster-name", detectedAttributes.get(GKE_CLUSTER_NAME));
Expand Down Expand Up @@ -202,7 +205,9 @@ public void testGKEResourceDetectionWithInvalidLocations(String clusterLocation)
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GKE-pid", detector.detectPlatform().getProjectId());
assertEquals(4, detectedAttributes.size());

assertEquals("", detector.detectPlatform().getAttributes().get(GKE_CLUSTER_LOCATION_TYPE));
if (clusterLocation == null || clusterLocation.isEmpty()) {
assertNull(detectedAttributes.get(GKE_CLUSTER_LOCATION));
Expand Down Expand Up @@ -234,7 +239,9 @@ public void testGCFResourceWithCloudFunctionAttributesSucceeds() {
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GCF-pid", detector.detectPlatform().getProjectId());
assertEquals(5, detectedAttributes.size());

assertEquals("cloud-function-hello", detectedAttributes.get(SERVERLESS_COMPUTE_NAME));
assertEquals("cloud-function-hello.1", detectedAttributes.get(SERVERLESS_COMPUTE_REVISION));
assertEquals(
Expand Down Expand Up @@ -262,6 +269,7 @@ public void testGCFDetectionWhenGCRAttributesPresent() {
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS,
detector.detectPlatform().getSupportedPlatform());
assertEquals("GCF-pid", detector.detectPlatform().getProjectId());
assertEquals(
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
Expand All @@ -288,7 +296,9 @@ public void testGCFResourceWithCloudRunAttributesSucceeds() {
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GCR-pid", detector.detectPlatform().getProjectId());
assertEquals(5, detectedAttributes.size());

assertEquals("cloud-run-hello", detectedAttributes.get(SERVERLESS_COMPUTE_NAME));
assertEquals("cloud-run-hello.1", detectedAttributes.get(SERVERLESS_COMPUTE_REVISION));
assertEquals(
Expand Down Expand Up @@ -321,7 +331,9 @@ public void testGAEResourceWithAppEngineAttributesSucceeds(String gaeEnvironment
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleAppEngine(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals("GAE-pid", detector.detectPlatform().getProjectId());
assertEquals(5, detectedAttributes.size());

if (gaeEnvironmentVar != null && gaeEnvironmentVar.equals("standard")) {
assertEquals(
"country-region1", detector.detectPlatform().getAttributes().get(GAE_CLOUD_REGION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public Attributes getAttributes() {
// This is running on some sort of GCPCompute - figure out the platform
AttributesBuilder attrBuilder = Attributes.builder();
attrBuilder.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.GCP);
attrBuilder.put(ResourceAttributes.CLOUD_ACCOUNT_ID, detectedPlatform.getProjectId());

switch (detectedPlatform.getSupportedPlatform()) {
case GOOGLE_KUBERNETES_ENGINE:
Expand Down Expand Up @@ -102,8 +103,6 @@ private void addGCEAttributes(AttributesBuilder attrBuilder, Map<String, String>
ResourceAttributes.CLOUD_PLATFORM,
ResourceAttributes.CloudPlatformValues.GCP_COMPUTE_ENGINE);

Optional.ofNullable(attributesMap.get(GCE_PROJECT_ID))
.ifPresent(projectId -> attrBuilder.put(ResourceAttributes.CLOUD_ACCOUNT_ID, projectId));
Optional.ofNullable(attributesMap.get(GCE_AVAILABILITY_ZONE))
.ifPresent(zone -> attrBuilder.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone));
Optional.ofNullable(attributesMap.get(GCE_CLOUD_REGION))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.mockito.Mockito;

public class GCPResourceProviderTest {

private static final String DUMMY_PROJECT_ID = "google-pid";
private final ConfigProperties mockConfigProps = Mockito.mock(ConfigProperties.class);
private final Map<String, String> mockGKECommonAttributes =
new HashMap<>() {
Expand All @@ -47,7 +47,6 @@ private DetectedPlatform generateMockGCEPlatform() {
Map<String, String> mockAttributes =
new HashMap<>() {
{
put(GCE_PROJECT_ID, "test-project-id");
put(GCE_CLOUD_REGION, "australia-southeast1");
put(GCE_AVAILABILITY_ZONE, "australia-southeast1-b");
put(GCE_INSTANCE_ID, "random-id");
Expand All @@ -60,6 +59,7 @@ private DetectedPlatform generateMockGCEPlatform() {
Mockito.when(mockGCEPlatform.getSupportedPlatform())
.thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_COMPUTE_ENGINE);
Mockito.when(mockGCEPlatform.getAttributes()).thenReturn(mockAttributes);
Mockito.when(mockGCEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
return mockGCEPlatform;
}

Expand All @@ -76,6 +76,7 @@ private DetectedPlatform generateMockGKEPlatform(String gkeClusterLocationType)
Mockito.when(mockGKEPlatform.getSupportedPlatform())
.thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE);
Mockito.when(mockGKEPlatform.getAttributes()).thenReturn(mockAttributes);
Mockito.when(mockGKEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
return mockGKEPlatform;
}

Expand All @@ -101,6 +102,7 @@ private DetectedPlatform generateMockServerlessPlatform(
DetectedPlatform mockServerlessPlatform = Mockito.mock(DetectedPlatform.class);
Mockito.when(mockServerlessPlatform.getSupportedPlatform()).thenReturn(platform);
Mockito.when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
Mockito.when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
return mockServerlessPlatform;
}

Expand All @@ -119,6 +121,7 @@ private DetectedPlatform generateMockGAEPlatform() {
Mockito.when(mockGAEPlatform.getSupportedPlatform())
.thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_APP_ENGINE);
Mockito.when(mockGAEPlatform.getAttributes()).thenReturn(mockAttributes);
Mockito.when(mockGAEPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
return mockGAEPlatform;
}

Expand Down Expand Up @@ -146,15 +149,15 @@ public void testGCEResourceAttributesMapping() {

Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);

assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertEquals(
ResourceAttributes.CloudPlatformValues.GCP_COMPUTE_ENGINE,
gotResource.getAttributes().get(ResourceAttributes.CLOUD_PLATFORM));
assertEquals(
ResourceAttributes.CloudProviderValues.GCP,
gotResource.getAttributes().get(ResourceAttributes.CLOUD_PROVIDER));
assertEquals(
mockPlatform.getAttributes().get(GCE_PROJECT_ID),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertEquals(
mockPlatform.getAttributes().get(GCE_INSTANCE_ID),
gotResource.getAttributes().get(ResourceAttributes.HOST_ID));
Expand Down Expand Up @@ -188,11 +191,14 @@ public void testGKEResourceAttributesMapping_LocationTypeRegion() {
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);

verifyGKEMapping(gotResource, mockPlatform);
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertNull(gotResource.getAttributes().get(ResourceAttributes.CLOUD_AVAILABILITY_ZONE));
assertEquals(
mockPlatform.getAttributes().get(GKE_CLUSTER_LOCATION),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_REGION));
assertEquals(5, gotResource.getAttributes().size());
assertEquals(6, gotResource.getAttributes().size());
}

@Test
Expand All @@ -204,11 +210,14 @@ public void testGKEResourceAttributesMapping_LocationTypeZone() {
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);

verifyGKEMapping(gotResource, mockPlatform);
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertNull(gotResource.getAttributes().get(ResourceAttributes.CLOUD_REGION));
assertEquals(
mockPlatform.getAttributes().get(GKE_CLUSTER_LOCATION),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_AVAILABILITY_ZONE));
assertEquals(5, gotResource.getAttributes().size());
assertEquals(6, gotResource.getAttributes().size());
}

@Test
Expand All @@ -221,15 +230,19 @@ public void testGKEResourceAttributesMapping_LocationTypeInvalid() {
DetectedPlatform mockPlatform = Mockito.mock(DetectedPlatform.class);
Mockito.when(mockPlatform.getSupportedPlatform())
.thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE);
Mockito.when(mockPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
Mockito.when(mockPlatform.getAttributes()).thenReturn(mockGKEAttributes);
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);

Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);

verifyGKEMapping(gotResource, mockPlatform);
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertNull(gotResource.getAttributes().get(ResourceAttributes.CLOUD_REGION));
assertNull(gotResource.getAttributes().get(ResourceAttributes.CLOUD_AVAILABILITY_ZONE));
assertEquals(4, gotResource.getAttributes().size());
assertEquals(5, gotResource.getAttributes().size());
}

@Test
Expand All @@ -241,9 +254,12 @@ public void testGKEResourceAttributesMapping_LocationMissing() {
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);

verifyGKEMapping(gotResource, mockPlatform);
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertNull(gotResource.getAttributes().get(ResourceAttributes.CLOUD_REGION));
assertNull(gotResource.getAttributes().get(ResourceAttributes.CLOUD_AVAILABILITY_ZONE));
assertEquals(4, gotResource.getAttributes().size());
assertEquals(5, gotResource.getAttributes().size());
}

private void verifyGKEMapping(Resource gotResource, DetectedPlatform detectedPlatform) {
Expand Down Expand Up @@ -272,8 +288,11 @@ public void testGCRResourceAttributesMapping() {
assertEquals(
ResourceAttributes.CloudPlatformValues.GCP_CLOUD_RUN,
gotResource.getAttributes().get(ResourceAttributes.CLOUD_PLATFORM));
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
verifyServerlessMapping(gotResource, mockPlatform);
assertEquals(7, gotResource.getAttributes().size());
assertEquals(8, gotResource.getAttributes().size());
}

@Test
Expand All @@ -288,8 +307,11 @@ public void testGCFResourceAttributeMapping() {
assertEquals(
ResourceAttributes.CloudPlatformValues.GCP_CLOUD_FUNCTIONS,
gotResource.getAttributes().get(ResourceAttributes.CLOUD_PLATFORM));
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
verifyServerlessMapping(gotResource, mockPlatform);
assertEquals(7, gotResource.getAttributes().size());
assertEquals(8, gotResource.getAttributes().size());
}

private void verifyServerlessMapping(Resource gotResource, DetectedPlatform detectedPlatform) {
Expand Down Expand Up @@ -326,6 +348,9 @@ public void testGAEResourceAttributeMapping() {
assertEquals(
ResourceAttributes.CloudProviderValues.GCP,
gotResource.getAttributes().get(ResourceAttributes.CLOUD_PROVIDER));
assertEquals(
mockPlatform.getProjectId(),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_ACCOUNT_ID));
assertEquals(
mockPlatform.getAttributes().get(GAE_MODULE_NAME),
gotResource.getAttributes().get(ResourceAttributes.FAAS_NAME));
Expand All @@ -341,7 +366,7 @@ public void testGAEResourceAttributeMapping() {
assertEquals(
mockPlatform.getAttributes().get(GAE_CLOUD_REGION),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_REGION));
assertEquals(7, gotResource.getAttributes().size());
assertEquals(8, gotResource.getAttributes().size());
}

@Test
Expand Down

0 comments on commit 4d577f0

Please sign in to comment.