|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.gcp.resource; |
| 7 | + |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.io.InputStreamReader; |
| 12 | +import java.net.HttpURLConnection; |
| 13 | +import java.net.URL; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | +import javax.annotation.Nullable; |
| 18 | + |
| 19 | +/** |
| 20 | + * Retrieves Google Cloud project-id and a limited set of instance attributes from Metadata server. |
| 21 | + * |
| 22 | + * @see <a href= "https://cloud.google.com/compute/docs/storing-retrieving-metadata"> |
| 23 | + * https://cloud.google.com/compute/docs/storing-retrieving-metadata</a> |
| 24 | + */ |
| 25 | +final class GcpMetadataConfig { |
| 26 | + static final GcpMetadataConfig DEFAULT_INSTANCE = new GcpMetadataConfig(); |
| 27 | + |
| 28 | + private static final String DEFAULT_URL = "http://metadata.google.internal/computeMetadata/v1/"; |
| 29 | + private final String url; |
| 30 | + private final Map<String, String> cachedAttributes = new ConcurrentHashMap<>(); |
| 31 | + |
| 32 | + private GcpMetadataConfig() { |
| 33 | + this.url = DEFAULT_URL; |
| 34 | + } |
| 35 | + |
| 36 | + // For testing only |
| 37 | + GcpMetadataConfig(String url) { |
| 38 | + this.url = url; |
| 39 | + } |
| 40 | + |
| 41 | + // Returns null on failure to retrieve from metadata server |
| 42 | + String getProjectId() { |
| 43 | + return getAttribute("project/project-id"); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Method to extract cloud availability zone from the metadata server. |
| 48 | + * |
| 49 | + * <p>Example response: projects/640212054955/zones/australia-southeast1-a |
| 50 | + * |
| 51 | + * <p>Example zone: australia-southeast1-a |
| 52 | + * |
| 53 | + * @return the extracted zone from the metadata server response or null in case of failure to |
| 54 | + * retrieve from metadata server. |
| 55 | + */ |
| 56 | + String getZone() { |
| 57 | + String zone = getAttribute("instance/zone"); |
| 58 | + if (zone != null && zone.contains("/")) { |
| 59 | + zone = zone.substring(zone.lastIndexOf('/') + 1); |
| 60 | + } |
| 61 | + return zone; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 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 | + * |
| 68 | + * <p>Example response: projects/5689182099321/regions/us-central1. |
| 69 | + * |
| 70 | + * @return the retrieved region or null in case of failure to retrieve from metadata server |
| 71 | + */ |
| 72 | + String getRegion() { |
| 73 | + String region = getAttribute("instance/region"); |
| 74 | + if (region != null && region.contains("/")) { |
| 75 | + region = region.substring(region.lastIndexOf('/') + 1); |
| 76 | + } |
| 77 | + return region; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Use this method to parse region from zone. |
| 82 | + * |
| 83 | + * <p>Example region: australia-southeast1 |
| 84 | + * |
| 85 | + * @return parsed region from the zone, if zone is not found or is invalid, this method returns |
| 86 | + * null. |
| 87 | + */ |
| 88 | + @Nullable |
| 89 | + String getRegionFromZone() { |
| 90 | + String region = null; |
| 91 | + String zone = getZone(); |
| 92 | + if (zone != null && !zone.isEmpty()) { |
| 93 | + // Parsing required to scope up to a region |
| 94 | + String[] splitArr = zone.split("-"); |
| 95 | + if (splitArr.length > 2) { |
| 96 | + region = String.join("-", splitArr[0], splitArr[1]); |
| 97 | + } |
| 98 | + } |
| 99 | + return region; |
| 100 | + } |
| 101 | + |
| 102 | + // Example response: projects/640212054955/machineTypes/e2-medium |
| 103 | + String getMachineType() { |
| 104 | + String machineType = getAttribute("instance/machine-type"); |
| 105 | + if (machineType != null && machineType.contains("/")) { |
| 106 | + machineType = machineType.substring(machineType.lastIndexOf('/') + 1); |
| 107 | + } |
| 108 | + return machineType; |
| 109 | + } |
| 110 | + |
| 111 | + // Returns null on failure to retrieve from metadata server |
| 112 | + String getInstanceId() { |
| 113 | + return getAttribute("instance/id"); |
| 114 | + } |
| 115 | + |
| 116 | + // Returns null on failure to retrieve from metadata server |
| 117 | + String getClusterName() { |
| 118 | + return getAttribute("instance/attributes/cluster-name"); |
| 119 | + } |
| 120 | + |
| 121 | + // Returns null on failure to retrieve from metadata server |
| 122 | + String getClusterLocation() { |
| 123 | + return getAttribute("instance/attributes/cluster-location"); |
| 124 | + } |
| 125 | + |
| 126 | + // Returns null on failure to retrieve from metadata server |
| 127 | + String getInstanceHostName() { |
| 128 | + return getAttribute("instance/hostname"); |
| 129 | + } |
| 130 | + |
| 131 | + // Returns null on failure to retrieve from metadata server |
| 132 | + String getInstanceName() { |
| 133 | + return getAttribute("instance/name"); |
| 134 | + } |
| 135 | + |
| 136 | + // Returns null on failure to retrieve from metadata server |
| 137 | + private String getAttribute(String attributeName) { |
| 138 | + return cachedAttributes.computeIfAbsent(attributeName, this::fetchAttribute); |
| 139 | + } |
| 140 | + |
| 141 | + // Return the attribute received at <attributeName> relative path or null on |
| 142 | + // failure |
| 143 | + @Nullable |
| 144 | + private String fetchAttribute(String attributeName) { |
| 145 | + try { |
| 146 | + URL url = new URL(this.url + attributeName); |
| 147 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 148 | + connection.setRequestProperty("Metadata-Flavor", "Google"); |
| 149 | + if (connection.getResponseCode() == 200 |
| 150 | + && "Google".equals(connection.getHeaderField("Metadata-Flavor"))) { |
| 151 | + InputStream input = connection.getInputStream(); |
| 152 | + try (BufferedReader reader = |
| 153 | + new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) { |
| 154 | + return reader.readLine(); |
| 155 | + } |
| 156 | + } |
| 157 | + } catch (IOException ignore) { |
| 158 | + // ignore |
| 159 | + } |
| 160 | + return null; |
| 161 | + } |
| 162 | +} |
0 commit comments