Skip to content

Commit d3df7d6

Browse files
committed
Remove CloudLocationUtil class
1 parent cbaaa31 commit d3df7d6

File tree

6 files changed

+52
-111
lines changed

6 files changed

+52
-111
lines changed

detectors/resources-support/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ afterEvaluate {
2424
tasks.named("compileJava"){
2525
options.release = 8
2626
}
27-
}
27+
}

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/CloudLocationUtil.java

Lines changed: 0 additions & 86 deletions
This file was deleted.

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCPMetadataConfig.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ String getProjectId() {
5252
return getAttribute("project/project-id");
5353
}
5454

55-
// Example response: projects/640212054955/zones/australia-southeast1-a
56-
// Returns null on failure to retrieve from metadata server
55+
/**
56+
* Method to extract cloud availability zone from the metadata server.
57+
*
58+
* <p>Example response: projects/640212054955/zones/australia-southeast1-a
59+
*
60+
* <p>Example zone: australia-southeast1-a
61+
*
62+
* @return the extracted zone from the metadata server response or null in case of failure to
63+
* retrieve from metadata server.
64+
*/
5765
String getZone() {
5866
String zone = getAttribute("instance/zone");
5967
if (zone != null && zone.contains("/")) {
@@ -62,10 +70,14 @@ String getZone() {
6270
return zone;
6371
}
6472

65-
// Use this method only when the region cannot be parsed from the zone. Known use-cases of this
66-
// method involve detecting region in GAE standard environment
67-
// Example response: projects/5689182099321/regions/us-central1
68-
// Returns null on failure to retrieve from metadata server
73+
/**
74+
* Use this method only when the region cannot be parsed from the zone. Known use-cases of this
75+
* method involve detecting region in GAE standard environment.
76+
*
77+
* <p>Example response: projects/5689182099321/regions/us-central1.
78+
*
79+
* @return the retrieved region or null in case of failure to retrieve from metadata server
80+
*/
6981
String getRegion() {
7082
String region = getAttribute("instance/region");
7183
if (region != null && region.contains("/")) {
@@ -74,6 +86,27 @@ String getRegion() {
7486
return region;
7587
}
7688

89+
/**
90+
* Use this method to parse region from zone.
91+
*
92+
* <p>Example region: australia-southeast1
93+
*
94+
* @return parsed region from the zone, if zone is not found or is invalid, this method returns
95+
* null.
96+
*/
97+
String getRegionFromZone() {
98+
String region = null;
99+
String zone = getZone();
100+
if (zone != null && !zone.isEmpty()) {
101+
// Parsing required to scope up to a region
102+
String[] splitArr = zone.split("-");
103+
if (splitArr.length > 2) {
104+
region = String.join("-", splitArr[0], splitArr[1]);
105+
}
106+
}
107+
return region;
108+
}
109+
77110
// Example response: projects/640212054955/machineTypes/e2-medium
78111
String getMachineType() {
79112
String machineType = getAttribute("instance/machine-type");

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleAppEngine.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,17 @@ private Map<String, Optional<String>> prepareAttributes() {
4949
map.put(GAE_MODULE_NAME, Optional.ofNullable(this.environmentVariables.get("GAE_SERVICE")));
5050
map.put(GAE_APP_VERSION, Optional.ofNullable(this.environmentVariables.get("GAE_VERSION")));
5151
map.put(GAE_INSTANCE_ID, Optional.ofNullable(this.environmentVariables.get("GAE_INSTANCE")));
52-
map.put(
53-
GAE_AVAILABILITY_ZONE,
54-
CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig));
52+
map.put(GAE_AVAILABILITY_ZONE, Optional.ofNullable(this.metadataConfig.getZone()));
5553
map.put(GAE_CLOUD_REGION, getCloudRegion());
5654
return Collections.unmodifiableMap(map);
5755
}
5856

5957
private Optional<String> getCloudRegion() {
6058
if (this.environmentVariables.get("GAE_ENV") != null
6159
&& this.environmentVariables.get("GAE_ENV").equals("standard")) {
62-
return CloudLocationUtil.getCloudRegionFromMetadataUsingRegion(this.metadataConfig);
60+
return Optional.ofNullable(this.metadataConfig.getRegion());
6361
} else {
64-
return CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig);
62+
return Optional.ofNullable(this.metadataConfig.getRegionFromZone());
6563
}
6664
}
6765

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleComputeEngine.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616
package com.google.cloud.opentelemetry.detectors;
1717

18+
import java.util.Collections;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Optional;
22+
1823
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_AVAILABILITY_ZONE;
1924
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_CLOUD_REGION;
2025
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_INSTANCE_ID;
2126
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_INSTANCE_NAME;
2227
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_MACHINE_TYPE;
2328
import static com.google.cloud.opentelemetry.detectors.AttributeKeys.GCE_PROJECT_ID;
2429

25-
import java.util.Collections;
26-
import java.util.HashMap;
27-
import java.util.Map;
28-
import java.util.Optional;
29-
3030
final class GoogleComputeEngine implements DetectedPlatform {
3131
private final GCPMetadataConfig metadataConfig;
3232
private final Map<String, Optional<String>> availableAttributes;
@@ -46,12 +46,8 @@ final class GoogleComputeEngine implements DetectedPlatform {
4646
private Map<String, Optional<String>> prepareAttributes() {
4747
Map<String, Optional<String>> map = new HashMap<>();
4848
map.put(GCE_PROJECT_ID, Optional.ofNullable(this.metadataConfig.getProjectId()));
49-
map.put(
50-
GCE_AVAILABILITY_ZONE,
51-
CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig));
52-
map.put(
53-
GCE_CLOUD_REGION,
54-
CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig));
49+
map.put(GCE_AVAILABILITY_ZONE, Optional.ofNullable(this.metadataConfig.getZone()));
50+
map.put(GCE_CLOUD_REGION, Optional.ofNullable(this.metadataConfig.getRegionFromZone()));
5551
map.put(GCE_INSTANCE_ID, Optional.ofNullable(this.metadataConfig.getInstanceId()));
5652
map.put(GCE_INSTANCE_NAME, Optional.ofNullable(this.metadataConfig.getInstanceName()));
5753
map.put(GCE_MACHINE_TYPE, Optional.ofNullable(this.metadataConfig.getMachineType()));

detectors/resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GoogleServerlessCompute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ private Map<String, Optional<String>> prepareAttributes() {
5252
Optional.ofNullable(this.environmentVariables.get("K_REVISION")));
5353
map.put(
5454
AttributeKeys.SERVERLESS_COMPUTE_AVAILABILITY_ZONE,
55-
CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig));
55+
Optional.ofNullable(this.metadataConfig.getZone()));
5656
map.put(
5757
AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION,
58-
CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig));
58+
Optional.ofNullable(this.metadataConfig.getRegionFromZone()));
5959
map.put(
6060
AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID,
6161
Optional.ofNullable(this.metadataConfig.getInstanceId()));

0 commit comments

Comments
 (0)