diff --git a/detectors/resources-support/build.gradle b/detectors/resources-support/build.gradle index 351c5972..ee842d0c 100644 --- a/detectors/resources-support/build.gradle +++ b/detectors/resources-support/build.gradle @@ -24,4 +24,4 @@ afterEvaluate { tasks.named("compileJava"){ options.release = 8 } -} \ No newline at end of file +} diff --git a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/CloudLocationUtil.java b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/CloudLocationUtil.java deleted file mode 100644 index 797f1e02..00000000 --- a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/CloudLocationUtil.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.cloud.opentelemetry.detectors; - -import java.util.Optional; - -/** - * Contains utility functions to retrieve appropriate zone and regions representing the location of - * certain cloud resources. - */ -final class CloudLocationUtil { - /** - * Utility method to extract cloud availability zone from passed {@link GCPMetadataConfig}. The - * method modifies the passed attributesBuilder by adding the extracted property to it. If the - * zone cannot be found, calling this method has no effect. - * - * - * - *

Example zone: australia-southeast1-a - * - * @param metadataConfig The {@link GCPMetadataConfig} from which the cloud availability zone - * value is extracted. - */ - static Optional getAvailabilityZoneFromMetadata(GCPMetadataConfig metadataConfig) { - return Optional.ofNullable(metadataConfig.getZone()); - } - - /** - * Utility method to extract the cloud region from passed {@link GCPMetadataConfig}. The method - * modifies the passed attributesBuilder by adding the extracted property to it. - * - *

- * - *

Example region: australia-southeast1 - * - * @param metadataConfig The {@link GCPMetadataConfig} from which the cloud region value is - * extracted. - */ - static Optional getCloudRegionFromMetadataUsingZone(GCPMetadataConfig metadataConfig) { - Optional optZone = getAvailabilityZoneFromMetadata(metadataConfig); - if (optZone.isPresent()) { - // Parsing required to scope up to a region - String[] splitArr = optZone.get().split("-"); - if (splitArr.length > 2) { - return Optional.of(String.join("-", splitArr[0], splitArr[1])); - } - } - return Optional.empty(); - } - - /** - * Utility method to extract the cloud region from passed {@link GCPMetadataConfig}. The method - * modifies the passed attributesBuilder by adding the extracted property to it. - * - *

- * - *

Example region: australia-southeast1 - * - * @param metadataConfig The {@link GCPMetadataConfig} from which the cloud region value is - * extracted. - */ - static Optional getCloudRegionFromMetadataUsingRegion(GCPMetadataConfig metadataConfig) { - return Optional.ofNullable(metadataConfig.getRegion()); - } -} diff --git a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCPMetadataConfig.java b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCPMetadataConfig.java index 7ac54148..58315cd9 100644 --- a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCPMetadataConfig.java +++ b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCPMetadataConfig.java @@ -52,8 +52,16 @@ String getProjectId() { return getAttribute("project/project-id"); } - // Example response: projects/640212054955/zones/australia-southeast1-a - // Returns null on failure to retrieve from metadata server + /** + * Method to extract cloud availability zone from the metadata server. + * + *

Example response: projects/640212054955/zones/australia-southeast1-a + * + *

Example zone: australia-southeast1-a + * + * @return the extracted zone from the metadata server response or null in case of failure to + * retrieve from metadata server. + */ String getZone() { String zone = getAttribute("instance/zone"); if (zone != null && zone.contains("/")) { @@ -62,10 +70,14 @@ String getZone() { return zone; } - // Use this method only when the region cannot be parsed from the zone. Known use-cases of this - // method involve detecting region in GAE standard environment - // Example response: projects/5689182099321/regions/us-central1 - // Returns null on failure to retrieve from metadata server + /** + * Use this method only when the region cannot be parsed from the zone. Known use-cases of this + * method involve detecting region in GAE standard environment. + * + *

Example response: projects/5689182099321/regions/us-central1. + * + * @return the retrieved region or null in case of failure to retrieve from metadata server + */ String getRegion() { String region = getAttribute("instance/region"); if (region != null && region.contains("/")) { @@ -74,6 +86,27 @@ String getRegion() { return region; } + /** + * Use this method to parse region from zone. + * + *

Example region: australia-southeast1 + * + * @return parsed region from the zone, if zone is not found or is invalid, this method returns + * null. + */ + String getRegionFromZone() { + String region = null; + String zone = getZone(); + if (zone != null && !zone.isEmpty()) { + // Parsing required to scope up to a region + String[] splitArr = zone.split("-"); + if (splitArr.length > 2) { + region = String.join("-", splitArr[0], splitArr[1]); + } + } + return region; + } + // Example response: projects/640212054955/machineTypes/e2-medium String getMachineType() { String machineType = getAttribute("instance/machine-type"); diff --git a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleAppEngine.java b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleAppEngine.java index cf9f9256..e4f96de0 100644 --- a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleAppEngine.java +++ b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleAppEngine.java @@ -49,9 +49,7 @@ private Map> prepareAttributes() { map.put(GAE_MODULE_NAME, Optional.ofNullable(this.environmentVariables.get("GAE_SERVICE"))); map.put(GAE_APP_VERSION, Optional.ofNullable(this.environmentVariables.get("GAE_VERSION"))); map.put(GAE_INSTANCE_ID, Optional.ofNullable(this.environmentVariables.get("GAE_INSTANCE"))); - map.put( - GAE_AVAILABILITY_ZONE, - CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig)); + map.put(GAE_AVAILABILITY_ZONE, Optional.ofNullable(this.metadataConfig.getZone())); map.put(GAE_CLOUD_REGION, getCloudRegion()); return Collections.unmodifiableMap(map); } @@ -59,9 +57,9 @@ private Map> prepareAttributes() { private Optional getCloudRegion() { if (this.environmentVariables.get("GAE_ENV") != null && this.environmentVariables.get("GAE_ENV").equals("standard")) { - return CloudLocationUtil.getCloudRegionFromMetadataUsingRegion(this.metadataConfig); + return Optional.ofNullable(this.metadataConfig.getRegion()); } else { - return CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig); + return Optional.ofNullable(this.metadataConfig.getRegionFromZone()); } } diff --git a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleComputeEngine.java b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleComputeEngine.java index 53e03baf..20acbbad 100644 --- a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleComputeEngine.java +++ b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleComputeEngine.java @@ -15,6 +15,11 @@ */ package com.google.cloud.opentelemetry.detectors; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + 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_ID; @@ -22,11 +27,6 @@ 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; -import java.util.Map; -import java.util.Optional; - final class GoogleComputeEngine implements DetectedPlatform { private final GCPMetadataConfig metadataConfig; private final Map> availableAttributes; @@ -46,12 +46,8 @@ final class GoogleComputeEngine implements DetectedPlatform { private Map> prepareAttributes() { Map> map = new HashMap<>(); map.put(GCE_PROJECT_ID, Optional.ofNullable(this.metadataConfig.getProjectId())); - map.put( - GCE_AVAILABILITY_ZONE, - CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig)); - map.put( - GCE_CLOUD_REGION, - CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig)); + map.put(GCE_AVAILABILITY_ZONE, Optional.ofNullable(this.metadataConfig.getZone())); + map.put(GCE_CLOUD_REGION, Optional.ofNullable(this.metadataConfig.getRegionFromZone())); map.put(GCE_INSTANCE_ID, Optional.ofNullable(this.metadataConfig.getInstanceId())); map.put(GCE_INSTANCE_NAME, Optional.ofNullable(this.metadataConfig.getInstanceName())); map.put(GCE_MACHINE_TYPE, Optional.ofNullable(this.metadataConfig.getMachineType())); diff --git a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleServerlessCompute.java b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleServerlessCompute.java index 5f3b9a0b..606e92dc 100644 --- a/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleServerlessCompute.java +++ b/detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleServerlessCompute.java @@ -52,10 +52,10 @@ private Map> prepareAttributes() { Optional.ofNullable(this.environmentVariables.get("K_REVISION"))); map.put( AttributeKeys.SERVERLESS_COMPUTE_AVAILABILITY_ZONE, - CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig)); + Optional.ofNullable(this.metadataConfig.getZone())); map.put( AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION, - CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig)); + Optional.ofNullable(this.metadataConfig.getRegionFromZone())); map.put( AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID, Optional.ofNullable(this.metadataConfig.getInstanceId()));