Skip to content

Commit

Permalink
Add missing attributes in detected GCE environment
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Jan 9, 2024
1 parent 0764e4a commit 6662d71
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class AttributeKeys {
public static final String GCE_INSTANCE_ID = AttributeKeys.INSTANCE_ID;
public static final String GCE_INSTANCE_NAME = AttributeKeys.INSTANCE_NAME;
public static final String GCE_MACHINE_TYPE = AttributeKeys.MACHINE_TYPE;
public static final String GCE_INSTANCE_HOSTNAME = "instance_hostname";

// GKE Attributes
public static final String GKE_CLUSTER_NAME = "gke_cluster_name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_AVAILABILITY_ZONE;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_CLOUD_REGION;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_INSTANCE_HOSTNAME;
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;
Expand All @@ -42,6 +43,7 @@ private Map<String, String> prepareAttributes() {
map.put(GCE_CLOUD_REGION, this.metadataConfig.getRegionFromZone());
map.put(GCE_INSTANCE_ID, this.metadataConfig.getInstanceId());
map.put(GCE_INSTANCE_NAME, this.metadataConfig.getInstanceName());
map.put(GCE_INSTANCE_HOSTNAME, this.metadataConfig.getInstanceHostName());
map.put(GCE_MACHINE_TYPE, this.metadataConfig.getMachineType());
return Collections.unmodifiableMap(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,27 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GAE_CLOUD_REGION;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_AVAILABILITY_ZONE;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_CLOUD_REGION;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_INSTANCE_HOSTNAME;
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 static com.google.cloud.opentelemetry.detectors.AttributeKeys.GKE_CLUSTER_LOCATION;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GKE_CLUSTER_LOCATION_TYPE;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GKE_CLUSTER_NAME;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GKE_HOST_ID;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GKE_LOCATION_TYPE_REGION;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GKE_LOCATION_TYPE_ZONE;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.SERVERLESS_COMPUTE_AVAILABILITY_ZONE;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.SERVERLESS_COMPUTE_NAME;
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.SERVERLESS_COMPUTE_REVISION;
import static com.google.cloud.opentelemetry.detectors.TestUtils.stubEndpoint;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import java.util.Collections;
Expand Down Expand Up @@ -86,20 +102,29 @@ public void testGCPComputeResourceNonGCPEndpoint() {
@Test
public void testGCEResourceWithGCEAttributesSucceeds() {
stubEndpoint("/project/project-id", "GCE-pid");
stubEndpoint("/instance/zone", "country-region-zone");
stubEndpoint("/instance/zone", "country-gce_region-gce_zone");
stubEndpoint("/instance/id", "GCE-instance-id");
stubEndpoint("/instance/name", "GCE-instance-name");
stubEndpoint("/instance/machine-type", "GCE-instance-type");
stubEndpoint("/instance/hostname", "GCE-instance-hostname");

GCPPlatformDetector detector =
new GCPPlatformDetector(mockMetadataConfig, new EnvVarMock(envVars));

assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_COMPUTE_ENGINE,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleComputeEngine(mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(new GoogleComputeEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals(7, 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));
assertEquals("GCE-instance-name", detectedAttributes.get(GCE_INSTANCE_NAME));
assertEquals("GCE-instance-type", detectedAttributes.get(GCE_MACHINE_TYPE));
assertEquals("GCE-instance-hostname", detectedAttributes.get(GCE_INSTANCE_HOSTNAME));
}

/** Google Kubernetes Engine Tests * */
Expand All @@ -112,25 +137,28 @@ public void testGKEResourceWithGKEAttributesSucceedsLocationZone() {
envVars.put("POD_NAME", "GKE-testHostName-full-1234");
envVars.put("CONTAINER_NAME", "GKE-testContainerName");

stubEndpoint("/project/project-id", "GCE-pid");
stubEndpoint("/instance/id", "GCE-instance-id");
stubEndpoint("/instance/name", "GCE-instance-name");
stubEndpoint("/instance/machine-type", "GCE-instance-type");
stubEndpoint("/project/project-id", "GKE-pid");
stubEndpoint("/instance/id", "GKE-instance-id");
stubEndpoint("/instance/name", "instance-name");
stubEndpoint("/instance/machine-type", "instance-type");
stubEndpoint("/instance/attributes/cluster-name", "GKE-cluster-name");
stubEndpoint("/instance/attributes/cluster-location", "country-region-zone");

EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
assertEquals(
GKE_LOCATION_TYPE_ZONE,
detector.detectPlatform().getAttributes().get(GKE_CLUSTER_LOCATION_TYPE));
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals(4, detectedAttributes.size());

assertEquals(GKE_LOCATION_TYPE_ZONE, detectedAttributes.get(GKE_CLUSTER_LOCATION_TYPE));
assertEquals("country-region-zone", detectedAttributes.get(GKE_CLUSTER_LOCATION));
assertEquals("GKE-cluster-name", detectedAttributes.get(GKE_CLUSTER_NAME));
assertEquals("GKE-instance-id", detectedAttributes.get(GKE_HOST_ID));
}

@Test
Expand All @@ -142,25 +170,27 @@ public void testGKEResourceWithGKEAttributesSucceedsLocationRegion() {
envVars.put("POD_NAME", "GKE-testHostName-full-1234");
envVars.put("CONTAINER_NAME", "GKE-testContainerName");

stubEndpoint("/project/project-id", "GCE-pid");
stubEndpoint("/instance/id", "GCE-instance-id");
stubEndpoint("/project/project-id", "GKE-pid");
stubEndpoint("/instance/id", "GKE-instance-id");
stubEndpoint("/instance/name", "GCE-instance-name");
stubEndpoint("/instance/machine-type", "GCE-instance-type");
stubEndpoint("/instance/machine-type", "GKE-instance-type");
stubEndpoint("/instance/attributes/cluster-name", "GKE-cluster-name");
stubEndpoint("/instance/attributes/cluster-location", "country-region");

EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
assertEquals(
GKE_LOCATION_TYPE_REGION,
detector.detectPlatform().getAttributes().get(GKE_CLUSTER_LOCATION_TYPE));
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
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));
assertEquals("GKE-instance-id", detectedAttributes.get(GKE_HOST_ID));
}

@ParameterizedTest
Expand All @@ -174,23 +204,31 @@ public void testGKEResourceDetectionWithInvalidLocations(String clusterLocation)
envVars.put("POD_NAME", "GKE-testHostName-full-1234");
envVars.put("CONTAINER_NAME", "GKE-testContainerName");

stubEndpoint("/project/project-id", "GCE-pid");
stubEndpoint("/instance/id", "GCE-instance-id");
stubEndpoint("/instance/name", "GCE-instance-name");
stubEndpoint("/instance/machine-type", "GCE-instance-type");
stubEndpoint("/project/project-id", "GKE-pid");
stubEndpoint("/instance/id", "GKE-instance-id");
stubEndpoint("/instance/name", "GKE-instance-name");
stubEndpoint("/instance/machine-type", "GKE-instance-type");
stubEndpoint("/instance/attributes/cluster-name", "GKE-cluster-name");
stubEndpoint("/instance/attributes/cluster-location", clusterLocation);

EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
new GoogleKubernetesEngine(mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals(4, detectedAttributes.size());
assertEquals("", detector.detectPlatform().getAttributes().get(GKE_CLUSTER_LOCATION_TYPE));
if (clusterLocation == null || clusterLocation.isEmpty()) {
assertNull(detectedAttributes.get(GKE_CLUSTER_LOCATION));
} else {
assertEquals(clusterLocation, detectedAttributes.get(GKE_CLUSTER_LOCATION));
}
assertEquals("GKE-cluster-name", detectedAttributes.get(GKE_CLUSTER_NAME));
assertEquals("GKE-instance-id", detectedAttributes.get(GKE_HOST_ID));
}

/** Google Cloud Functions Tests * */
Expand All @@ -208,12 +246,19 @@ public void testGCFResourceWithCloudFunctionAttributesSucceeds() {
EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_FUNCTIONS,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals(5, detectedAttributes.size());
assertEquals("cloud-function-hello", detectedAttributes.get(SERVERLESS_COMPUTE_NAME));
assertEquals("cloud-function-hello.1", detectedAttributes.get(SERVERLESS_COMPUTE_REVISION));
assertEquals(
"country-region-zone", detectedAttributes.get(SERVERLESS_COMPUTE_AVAILABILITY_ZONE));
assertEquals("country-region", detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
assertEquals("GCF-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID));
}

@Test
Expand Down Expand Up @@ -255,12 +300,19 @@ public void testGCFResourceWithCloudRunAttributesSucceeds() {
EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

Map<String, String> detectedAttributes = detector.detectPlatform().getAttributes();
assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN,
detector.detectPlatform().getSupportedPlatform());
assertEquals(
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(),
detector.detectPlatform().getAttributes());
new GoogleCloudFunction(mockEnv, mockMetadataConfig).getAttributes(), detectedAttributes);
assertEquals(5, detectedAttributes.size());
assertEquals("cloud-run-hello", detectedAttributes.get(SERVERLESS_COMPUTE_NAME));
assertEquals("cloud-run-hello.1", detectedAttributes.get(SERVERLESS_COMPUTE_REVISION));
assertEquals(
"country-region-zone", detectedAttributes.get(SERVERLESS_COMPUTE_AVAILABILITY_ZONE));
assertEquals("country-region", detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
assertEquals("GCR-instance-id", detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID));
}

/** Google App Engine Tests * */
Expand All @@ -281,7 +333,6 @@ public void testGAEResourceWithAppEngineAttributesSucceeds(

EnvironmentVariables mockEnv = new EnvVarMock(envVars);
GCPPlatformDetector detector = new GCPPlatformDetector(mockMetadataConfig, mockEnv);

assertEquals(
GCPPlatformDetector.SupportedPlatform.GOOGLE_APP_ENGINE,
detector.detectPlatform().getSupportedPlatform());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ public Attributes getAttributes() {
addGAEAttributes(attrBuilder, detectedPlatform.getAttributes());
break;
case GOOGLE_COMPUTE_ENGINE:
default:
addGCEAttributes(attrBuilder, detectedPlatform.getAttributes());
break;
default:
// We don't support this platform yet, so just return with what we have
}

return attrBuilder.build();
Expand Down Expand Up @@ -109,7 +111,15 @@ private void addGCEAttributes(AttributesBuilder attrBuilder, Map<String, String>
Optional.ofNullable(attributesMap.get(GCE_INSTANCE_ID))
.ifPresent(instanceId -> attrBuilder.put(ResourceAttributes.HOST_ID, instanceId));
Optional.ofNullable(attributesMap.get(GCE_INSTANCE_NAME))
.ifPresent(instanceName -> attrBuilder.put(ResourceAttributes.HOST_NAME, instanceName));
.ifPresent(
instanceName -> {
attrBuilder.put(ResourceAttributes.HOST_NAME, instanceName);
attrBuilder.put(ResourceAttributes.GCP_GCE_INSTANCE_NAME, instanceName);
});
Optional.ofNullable(attributesMap.get(GCE_INSTANCE_HOSTNAME))
.ifPresent(
instanceHostname ->
attrBuilder.put(ResourceAttributes.GCP_GCE_INSTANCE_HOSTNAME, instanceHostname));
Optional.ofNullable(attributesMap.get(GCE_MACHINE_TYPE))
.ifPresent(machineType -> attrBuilder.put(ResourceAttributes.HOST_TYPE, machineType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private DetectedPlatform generateMockGCEPlatform() {
put(GCE_INSTANCE_ID, "random-id");
put(GCE_INSTANCE_NAME, "instance-name");
put(GCE_MACHINE_TYPE, "gce-m2");
put(GCE_INSTANCE_HOSTNAME, "instance-hostname");
}
};
DetectedPlatform mockGCEPlatform = Mockito.mock(DetectedPlatform.class);
Expand Down Expand Up @@ -160,6 +161,12 @@ public void testGCEResourceAttributesMapping() {
assertEquals(
mockPlatform.getAttributes().get(GCE_INSTANCE_NAME),
gotResource.getAttributes().get(ResourceAttributes.HOST_NAME));
assertEquals(
mockPlatform.getAttributes().get(GCE_INSTANCE_NAME),
gotResource.getAttributes().get(ResourceAttributes.GCP_GCE_INSTANCE_NAME));
assertEquals(
mockPlatform.getAttributes().get(GCE_INSTANCE_HOSTNAME),
gotResource.getAttributes().get(ResourceAttributes.GCP_GCE_INSTANCE_HOSTNAME));
assertEquals(
mockPlatform.getAttributes().get(GCE_MACHINE_TYPE),
gotResource.getAttributes().get(ResourceAttributes.HOST_TYPE));
Expand All @@ -169,7 +176,7 @@ public void testGCEResourceAttributesMapping() {
assertEquals(
mockPlatform.getAttributes().get(GCE_CLOUD_REGION),
gotResource.getAttributes().get(ResourceAttributes.CLOUD_REGION));
assertEquals(8, gotResource.getAttributes().size());
assertEquals(10, gotResource.getAttributes().size());
}

@Test
Expand Down

0 comments on commit 6662d71

Please sign in to comment.