|
| 1 | +/* |
| 2 | + * Copyright 2022 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.opentelemetry.detectors; |
| 17 | + |
| 18 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 19 | +import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; |
| 20 | + |
| 21 | +/** |
| 22 | + * A utility class that contains method that facilitate extraction of attributes from environment |
| 23 | + * variables and metadata configurations. |
| 24 | + * |
| 25 | + * <p>This class only adds helper methods to extract {@link ResourceAttributes} that are common |
| 26 | + * across all the supported compute environments. |
| 27 | + */ |
| 28 | +public class AttributesExtractorUtil { |
| 29 | + |
| 30 | + /** |
| 31 | + * Utility method to extract cloud availability zone from passed {@link GCPMetadataConfig}. The |
| 32 | + * method modifies the passed attributesBuilder by adding the extracted property to it. If the |
| 33 | + * zone cannot be found, calling this method has no effect. |
| 34 | + * |
| 35 | + * <ul> |
| 36 | + * <li>If the availability zone cannot be found, calling this method has no effect. |
| 37 | + * <li>Calling this method will update {@link ResourceAttributes#CLOUD_AVAILABILITY_ZONE} |
| 38 | + * attribute. |
| 39 | + * </ul> |
| 40 | + * |
| 41 | + * <p>Example zone: australia-southeast1-a |
| 42 | + * |
| 43 | + * @param attributesBuilder The {@link AttributesBuilder} to which the extracted property needs to |
| 44 | + * be added. |
| 45 | + * @param metadataConfig The {@link GCPMetadataConfig} from which the cloud availability zone |
| 46 | + * value is extracted. |
| 47 | + */ |
| 48 | + public static void addAvailabilityZoneFromMetadata( |
| 49 | + AttributesBuilder attributesBuilder, GCPMetadataConfig metadataConfig) { |
| 50 | + String zone = metadataConfig.getZone(); |
| 51 | + if (zone != null) { |
| 52 | + attributesBuilder.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Utility method to extract the cloud region from passed {@link GCPMetadataConfig}. The method |
| 58 | + * modifies the passed attributesBuilder by adding the extracted property to it. |
| 59 | + * |
| 60 | + * <ul> |
| 61 | + * <li>If the cloud region cannot be found, calling this method has no effect. |
| 62 | + * <li>Calling this method will update {@link ResourceAttributes#CLOUD_REGION} attribute. |
| 63 | + * </ul> |
| 64 | + * |
| 65 | + * <p>Example region: australia-southeast1 |
| 66 | + * |
| 67 | + * @param attributesBuilder The {@link AttributesBuilder} to which the extracted property needs to |
| 68 | + * be added. |
| 69 | + * @param metadataConfig The {@link GCPMetadataConfig} from which the cloud region value is |
| 70 | + * extracted. |
| 71 | + */ |
| 72 | + public static void addCloudRegionFromMetadata( |
| 73 | + AttributesBuilder attributesBuilder, GCPMetadataConfig metadataConfig) { |
| 74 | + String zone = metadataConfig.getZone(); |
| 75 | + if (zone != null) { |
| 76 | + // Parsing required to scope up to a region |
| 77 | + String[] splitArr = zone.split("-"); |
| 78 | + if (splitArr.length > 2) { |
| 79 | + attributesBuilder.put(ResourceAttributes.CLOUD_REGION, splitArr[0] + "-" + splitArr[1]); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Utility method to extract the current compute instance ID from the passed {@link |
| 86 | + * GCPMetadataConfig}. The method modifies the passed attributesBuilder by adding the extracted |
| 87 | + * property to it. |
| 88 | + * |
| 89 | + * <ul> |
| 90 | + * <li>If the instance ID cannot be found, calling this method has no effect. |
| 91 | + * <li>Calling this method will update {@link ResourceAttributes#FAAS_ID} attribute. |
| 92 | + * </ul> |
| 93 | + * |
| 94 | + * @param attributesBuilder The {@link AttributesBuilder} to which the extracted property needs to |
| 95 | + * be added. |
| 96 | + * @param metadataConfig The {@link GCPMetadataConfig} from which the instance ID value is |
| 97 | + * extracted. |
| 98 | + */ |
| 99 | + public static void addInstanceIdFromMetadata( |
| 100 | + AttributesBuilder attributesBuilder, GCPMetadataConfig metadataConfig) { |
| 101 | + String instanceId = metadataConfig.getInstanceId(); |
| 102 | + if (instanceId != null) { |
| 103 | + attributesBuilder.put(ResourceAttributes.FAAS_ID, instanceId); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments