-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor platform detection from resource provider
- Loading branch information
Showing
12 changed files
with
822 additions
and
613 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...ces-support/src/main/java/com/google/cloud/opentelemetry/detectors/CloudLocationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2023 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. | ||
* | ||
* <ul> | ||
* <li>If the availability zone cannot be found, calling this method has no effect. | ||
* </ul> | ||
* | ||
* <p>Example zone: australia-southeast1-a | ||
* | ||
* @param metadataConfig The {@link GCPMetadataConfig} from which the cloud availability zone | ||
* value is extracted. | ||
*/ | ||
static Optional<String> 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. | ||
* | ||
* <ul> | ||
* <li>If the cloud region cannot be found, calling this method has no effect. | ||
* <li>This method uses zone attribute to parse region from it. | ||
* </ul> | ||
* | ||
* <p>Example region: australia-southeast1 | ||
* | ||
* @param metadataConfig The {@link GCPMetadataConfig} from which the cloud region value is | ||
* extracted. | ||
*/ | ||
static Optional<String> getCloudRegionFromMetadataUsingZone(GCPMetadataConfig metadataConfig) { | ||
Optional<String> 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. | ||
* | ||
* <ul> | ||
* <li>If the cloud region cannot be found, calling this method has no effect. | ||
* <li>This method directly uses the region attribute from the metadata config. | ||
* </ul> | ||
* | ||
* <p>Example region: australia-southeast1 | ||
* | ||
* @param metadataConfig The {@link GCPMetadataConfig} from which the cloud region value is | ||
* extracted. | ||
*/ | ||
static Optional<String> getCloudRegionFromMetadataUsingRegion(GCPMetadataConfig metadataConfig) { | ||
return Optional.ofNullable(metadataConfig.getRegion()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GAEDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2023 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; | ||
|
||
public final class GAEDetector { | ||
public static final GAEDetector DEFAULT_INSTANCE = new GAEDetector(); | ||
|
||
private final EnvironmentVariables environmentVariables; | ||
private final GCPMetadataConfig metadataConfig; | ||
|
||
GAEDetector() { | ||
this.environmentVariables = EnvironmentVariables.DEFAULT_INSTANCE; | ||
this.metadataConfig = GCPMetadataConfig.DEFAULT_INSTANCE; | ||
} | ||
|
||
// for testing only | ||
GAEDetector(EnvironmentVariables environmentVariables, GCPMetadataConfig metadataConfig) { | ||
this.environmentVariables = environmentVariables; | ||
this.metadataConfig = metadataConfig; | ||
} | ||
|
||
public Optional<String> getAppModuleName() { | ||
return Optional.ofNullable(this.environmentVariables.get("GAE_SERVICE")); | ||
} | ||
|
||
public Optional<String> getAppVersion() { | ||
return Optional.ofNullable(this.environmentVariables.get("GAE_VERSION")); | ||
} | ||
|
||
public Optional<String> getAppInstanceId() { | ||
return Optional.ofNullable(this.environmentVariables.get("GAE_INSTANCE")); | ||
} | ||
|
||
public Optional<String> getAvailabilityZone() { | ||
return CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig); | ||
} | ||
|
||
public Optional<String> getCloudRegion() { | ||
if (this.environmentVariables.get("GAE_ENV") == null) { | ||
return Optional.empty(); | ||
} | ||
if (this.environmentVariables.get("GAE_ENV").equals("standard")) { | ||
return CloudLocationUtil.getCloudRegionFromMetadataUsingRegion(this.metadataConfig); | ||
} else { | ||
return CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...resources-support/src/main/java/com/google/cloud/opentelemetry/detectors/GCEDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2023 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; | ||
|
||
public final class GCEDetector { | ||
public static final GCEDetector DEFAULT_INSTANCE = new GCEDetector(); | ||
|
||
private final GCPMetadataConfig metadataConfig; | ||
|
||
GCEDetector() { | ||
this.metadataConfig = GCPMetadataConfig.DEFAULT_INSTANCE; | ||
} | ||
|
||
// for testing only | ||
GCEDetector(GCPMetadataConfig metadataConfig) { | ||
this.metadataConfig = metadataConfig; | ||
} | ||
|
||
public Optional<String> getProjectId() { | ||
return Optional.ofNullable(this.metadataConfig.getProjectId()); | ||
} | ||
|
||
public Optional<String> getAvailabilityZone() { | ||
return CloudLocationUtil.getAvailabilityZoneFromMetadata(this.metadataConfig); | ||
} | ||
|
||
public Optional<String> getCloudRegion() { | ||
return CloudLocationUtil.getCloudRegionFromMetadataUsingZone(this.metadataConfig); | ||
} | ||
|
||
public Optional<String> getInstanceId() { | ||
return Optional.ofNullable(this.metadataConfig.getInstanceId()); | ||
} | ||
|
||
public Optional<String> getInstanceName() { | ||
return Optional.ofNullable(this.metadataConfig.getInstanceName()); | ||
} | ||
|
||
public Optional<String> getMachineType() { | ||
return Optional.ofNullable(this.metadataConfig.getMachineType()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.