|
| 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 static com.google.cloud.opentelemetry.detection.AttributeKeys.GAE_APP_VERSION; |
| 9 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GAE_AVAILABILITY_ZONE; |
| 10 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GAE_CLOUD_REGION; |
| 11 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GAE_INSTANCE_ID; |
| 12 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GAE_MODULE_NAME; |
| 13 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_AVAILABILITY_ZONE; |
| 14 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_CLOUD_REGION; |
| 15 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_HOSTNAME; |
| 16 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_ID; |
| 17 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_NAME; |
| 18 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_MACHINE_TYPE; |
| 19 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION; |
| 20 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION_TYPE; |
| 21 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_NAME; |
| 22 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_HOST_ID; |
| 23 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_LOCATION_TYPE_REGION; |
| 24 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_LOCATION_TYPE_ZONE; |
| 25 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_AVAILABILITY_ZONE; |
| 26 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_CLOUD_REGION; |
| 27 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID; |
| 28 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_NAME; |
| 29 | +import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_REVISION; |
| 30 | + |
| 31 | +import com.google.cloud.opentelemetry.detection.DetectedPlatform; |
| 32 | +import com.google.cloud.opentelemetry.detection.GCPPlatformDetector; |
| 33 | +import io.opentelemetry.api.common.Attributes; |
| 34 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 35 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 36 | +import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; |
| 37 | +import io.opentelemetry.sdk.resources.Resource; |
| 38 | +import io.opentelemetry.semconv.ResourceAttributes; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Optional; |
| 41 | +import java.util.logging.Logger; |
| 42 | + |
| 43 | +public class GCPResourceProvider implements ResourceProvider { |
| 44 | + |
| 45 | + private static final Logger LOGGER = Logger.getLogger(GCPResourceProvider.class.getSimpleName()); |
| 46 | + private final GCPPlatformDetector detector; |
| 47 | + |
| 48 | + // for testing only |
| 49 | + GCPResourceProvider(GCPPlatformDetector detector) { |
| 50 | + this.detector = detector; |
| 51 | + } |
| 52 | + |
| 53 | + public GCPResourceProvider() { |
| 54 | + this.detector = GCPPlatformDetector.DEFAULT_INSTANCE; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Generates and returns the attributes for the resource. The attributes vary depending on the |
| 59 | + * type of resource detected. |
| 60 | + * |
| 61 | + * @return The {@link Attributes} for the detected resource. |
| 62 | + */ |
| 63 | + public Attributes getAttributes() { |
| 64 | + DetectedPlatform detectedPlatform = detector.detectPlatform(); |
| 65 | + if (detectedPlatform.getSupportedPlatform() |
| 66 | + == GCPPlatformDetector.SupportedPlatform.UNKNOWN_PLATFORM) { |
| 67 | + return Attributes.empty(); |
| 68 | + } |
| 69 | + |
| 70 | + // This is running on some sort of GCPCompute - figure out the platform |
| 71 | + AttributesBuilder attrBuilder = Attributes.builder(); |
| 72 | + attrBuilder.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.GCP); |
| 73 | + attrBuilder.put(ResourceAttributes.CLOUD_ACCOUNT_ID, detectedPlatform.getProjectId()); |
| 74 | + |
| 75 | + switch (detectedPlatform.getSupportedPlatform()) { |
| 76 | + case GOOGLE_KUBERNETES_ENGINE: |
| 77 | + addGkeAttributes(attrBuilder, detectedPlatform.getAttributes()); |
| 78 | + break; |
| 79 | + case GOOGLE_CLOUD_RUN: |
| 80 | + addGcrAttributes(attrBuilder, detectedPlatform.getAttributes()); |
| 81 | + break; |
| 82 | + case GOOGLE_CLOUD_FUNCTIONS: |
| 83 | + addGcfAttributes(attrBuilder, detectedPlatform.getAttributes()); |
| 84 | + break; |
| 85 | + case GOOGLE_APP_ENGINE: |
| 86 | + addGaeAttributes(attrBuilder, detectedPlatform.getAttributes()); |
| 87 | + break; |
| 88 | + case GOOGLE_COMPUTE_ENGINE: |
| 89 | + addGceAttributes(attrBuilder, detectedPlatform.getAttributes()); |
| 90 | + break; |
| 91 | + default: |
| 92 | + // We don't support this platform yet, so just return with what we have |
| 93 | + } |
| 94 | + |
| 95 | + return attrBuilder.build(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Resource createResource(ConfigProperties config) { |
| 100 | + return Resource.create(getAttributes()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Updates the attributes builder with required attributes for GCE resource, if GCE resource is |
| 105 | + * applicable. By default, if the resource is running on GCP, it is assumed to be GCE. This means |
| 106 | + * additional attributes are added/overwritten if later on, the resource is identified to be some |
| 107 | + * other platform - like GKE, GAE, etc. |
| 108 | + */ |
| 109 | + private static void addGceAttributes( |
| 110 | + AttributesBuilder attrBuilder, Map<String, String> attributesMap) { |
| 111 | + attrBuilder.put( |
| 112 | + ResourceAttributes.CLOUD_PLATFORM, |
| 113 | + ResourceAttributes.CloudPlatformValues.GCP_COMPUTE_ENGINE); |
| 114 | + |
| 115 | + Optional.ofNullable(attributesMap.get(GCE_AVAILABILITY_ZONE)) |
| 116 | + .ifPresent(zone -> attrBuilder.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone)); |
| 117 | + Optional.ofNullable(attributesMap.get(GCE_CLOUD_REGION)) |
| 118 | + .ifPresent(region -> attrBuilder.put(ResourceAttributes.CLOUD_REGION, region)); |
| 119 | + Optional.ofNullable(attributesMap.get(GCE_INSTANCE_ID)) |
| 120 | + .ifPresent(instanceId -> attrBuilder.put(ResourceAttributes.HOST_ID, instanceId)); |
| 121 | + Optional.ofNullable(attributesMap.get(GCE_INSTANCE_NAME)) |
| 122 | + .ifPresent( |
| 123 | + instanceName -> { |
| 124 | + attrBuilder.put(ResourceAttributes.HOST_NAME, instanceName); |
| 125 | + attrBuilder.put(ResourceAttributes.GCP_GCE_INSTANCE_NAME, instanceName); |
| 126 | + }); |
| 127 | + Optional.ofNullable(attributesMap.get(GCE_INSTANCE_HOSTNAME)) |
| 128 | + .ifPresent( |
| 129 | + instanceHostname -> |
| 130 | + attrBuilder.put(ResourceAttributes.GCP_GCE_INSTANCE_HOSTNAME, instanceHostname)); |
| 131 | + Optional.ofNullable(attributesMap.get(GCE_MACHINE_TYPE)) |
| 132 | + .ifPresent(machineType -> attrBuilder.put(ResourceAttributes.HOST_TYPE, machineType)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Updates the attributes with the required keys for a GKE (Google Kubernetes Engine) environment. |
| 137 | + * The attributes are not updated in case the environment is not deemed to be GKE. |
| 138 | + * |
| 139 | + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the |
| 140 | + * necessary keys. |
| 141 | + */ |
| 142 | + private static void addGkeAttributes( |
| 143 | + AttributesBuilder attrBuilder, Map<String, String> attributesMap) { |
| 144 | + attrBuilder.put( |
| 145 | + ResourceAttributes.CLOUD_PLATFORM, |
| 146 | + ResourceAttributes.CloudPlatformValues.GCP_KUBERNETES_ENGINE); |
| 147 | + |
| 148 | + Optional.ofNullable(attributesMap.get(GKE_CLUSTER_NAME)) |
| 149 | + .ifPresent( |
| 150 | + clusterName -> attrBuilder.put(ResourceAttributes.K8S_CLUSTER_NAME, clusterName)); |
| 151 | + Optional.ofNullable(attributesMap.get(GKE_HOST_ID)) |
| 152 | + .ifPresent(hostId -> attrBuilder.put(ResourceAttributes.HOST_ID, hostId)); |
| 153 | + Optional.ofNullable(attributesMap.get(GKE_CLUSTER_LOCATION_TYPE)) |
| 154 | + .ifPresent( |
| 155 | + locationType -> { |
| 156 | + if (attributesMap.get(GKE_CLUSTER_LOCATION) != null) { |
| 157 | + switch (locationType) { |
| 158 | + case GKE_LOCATION_TYPE_REGION: |
| 159 | + attrBuilder.put( |
| 160 | + ResourceAttributes.CLOUD_REGION, attributesMap.get(GKE_CLUSTER_LOCATION)); |
| 161 | + break; |
| 162 | + case GKE_LOCATION_TYPE_ZONE: |
| 163 | + attrBuilder.put( |
| 164 | + ResourceAttributes.CLOUD_AVAILABILITY_ZONE, |
| 165 | + attributesMap.get(GKE_CLUSTER_LOCATION)); |
| 166 | + break; |
| 167 | + default: |
| 168 | + // TODO: Figure out how to handle unexpected conditions like this |
| 169 | + LOGGER.severe( |
| 170 | + String.format( |
| 171 | + "Unrecognized format for cluster location: %s", |
| 172 | + attributesMap.get(GKE_CLUSTER_LOCATION))); |
| 173 | + } |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Updates the attributes with the required keys for a GCR (Google Cloud Run) environment. The |
| 180 | + * attributes are not updated in case the environment is not deemed to be GCR. |
| 181 | + * |
| 182 | + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the |
| 183 | + * necessary keys. |
| 184 | + */ |
| 185 | + private static void addGcrAttributes( |
| 186 | + AttributesBuilder attrBuilder, Map<String, String> attributesMap) { |
| 187 | + attrBuilder.put( |
| 188 | + ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.GCP_CLOUD_RUN); |
| 189 | + addCommonAttributesForServerlessCompute(attrBuilder, attributesMap); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Updates the attributes with the required keys for a GCF (Google Cloud Functions) environment. |
| 194 | + * The attributes are not updated in case the environment is not deemed to be GCF. |
| 195 | + * |
| 196 | + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the |
| 197 | + * necessary keys. |
| 198 | + */ |
| 199 | + private static void addGcfAttributes( |
| 200 | + AttributesBuilder attrBuilder, Map<String, String> attributesMap) { |
| 201 | + attrBuilder.put( |
| 202 | + ResourceAttributes.CLOUD_PLATFORM, |
| 203 | + ResourceAttributes.CloudPlatformValues.GCP_CLOUD_FUNCTIONS); |
| 204 | + addCommonAttributesForServerlessCompute(attrBuilder, attributesMap); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Updates the attributes with the required keys for a GAE (Google App Engine) environment. The |
| 209 | + * attributes are not updated in case the environment is not deemed to be GAE. |
| 210 | + * |
| 211 | + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the |
| 212 | + * necessary keys. |
| 213 | + */ |
| 214 | + private static void addGaeAttributes( |
| 215 | + AttributesBuilder attrBuilder, Map<String, String> attributesMap) { |
| 216 | + attrBuilder.put( |
| 217 | + ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.GCP_APP_ENGINE); |
| 218 | + Optional.ofNullable(attributesMap.get(GAE_MODULE_NAME)) |
| 219 | + .ifPresent(appName -> attrBuilder.put(ResourceAttributes.FAAS_NAME, appName)); |
| 220 | + Optional.ofNullable(attributesMap.get(GAE_APP_VERSION)) |
| 221 | + .ifPresent(appVersion -> attrBuilder.put(ResourceAttributes.FAAS_VERSION, appVersion)); |
| 222 | + Optional.ofNullable(attributesMap.get(GAE_INSTANCE_ID)) |
| 223 | + .ifPresent( |
| 224 | + appInstanceId -> attrBuilder.put(ResourceAttributes.FAAS_INSTANCE, appInstanceId)); |
| 225 | + Optional.ofNullable(attributesMap.get(GAE_CLOUD_REGION)) |
| 226 | + .ifPresent(cloudRegion -> attrBuilder.put(ResourceAttributes.CLOUD_REGION, cloudRegion)); |
| 227 | + Optional.ofNullable(attributesMap.get(GAE_AVAILABILITY_ZONE)) |
| 228 | + .ifPresent( |
| 229 | + cloudAvailabilityZone -> |
| 230 | + attrBuilder.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, cloudAvailabilityZone)); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * This function adds common attributes required for most serverless compute platforms within GCP. |
| 235 | + * Currently, these attributes are required for both GCF and GCR. |
| 236 | + * |
| 237 | + * @param attrBuilder The {@link AttributesBuilder} object that needs to be updated with the |
| 238 | + * necessary keys. |
| 239 | + */ |
| 240 | + private static void addCommonAttributesForServerlessCompute( |
| 241 | + AttributesBuilder attrBuilder, Map<String, String> attributesMap) { |
| 242 | + Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_NAME)) |
| 243 | + .ifPresent(name -> attrBuilder.put(ResourceAttributes.FAAS_NAME, name)); |
| 244 | + Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_REVISION)) |
| 245 | + .ifPresent(revision -> attrBuilder.put(ResourceAttributes.FAAS_VERSION, revision)); |
| 246 | + Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_INSTANCE_ID)) |
| 247 | + .ifPresent(instanceId -> attrBuilder.put(ResourceAttributes.FAAS_INSTANCE, instanceId)); |
| 248 | + Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_AVAILABILITY_ZONE)) |
| 249 | + .ifPresent(zone -> attrBuilder.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone)); |
| 250 | + Optional.ofNullable(attributesMap.get(SERVERLESS_COMPUTE_CLOUD_REGION)) |
| 251 | + .ifPresent(region -> attrBuilder.put(ResourceAttributes.CLOUD_REGION, region)); |
| 252 | + } |
| 253 | +} |
0 commit comments