|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +import logging |
| 17 | +from urllib.request import Request, urlopen |
| 18 | + |
| 19 | +from opentelemetry.sdk.resources import Resource, ResourceDetector |
| 20 | +from opentelemetry.semconv.resource import ( |
| 21 | + CloudPlatformValues, |
| 22 | + CloudProviderValues, |
| 23 | + ResourceAttributes, |
| 24 | +) |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | +_AWS_METADATA_TOKEN_HEADER = "X-aws-ec2-metadata-token" |
| 29 | +_GET_METHOD = "GET" |
| 30 | + |
| 31 | + |
| 32 | +def _aws_http_request(method, path, headers): |
| 33 | + with urlopen( |
| 34 | + Request( |
| 35 | + "http://169.254.169.254" + path, headers=headers, method=method |
| 36 | + ), |
| 37 | + timeout=1000, |
| 38 | + ) as response: |
| 39 | + return response.read().decode("utf-8") |
| 40 | + |
| 41 | + |
| 42 | +def _get_token(): |
| 43 | + return _aws_http_request( |
| 44 | + "PUT", |
| 45 | + "/latest/api/token", |
| 46 | + {"X-aws-ec2-metadata-token-ttl-seconds": "60"}, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def _get_identity(token): |
| 51 | + return _aws_http_request( |
| 52 | + _GET_METHOD, |
| 53 | + "/latest/dynamic/instance-identity/document", |
| 54 | + {_AWS_METADATA_TOKEN_HEADER: token}, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _get_host(token): |
| 59 | + return _aws_http_request( |
| 60 | + _GET_METHOD, |
| 61 | + "/latest/meta-data/hostname", |
| 62 | + {_AWS_METADATA_TOKEN_HEADER: token}, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class AwsEc2ResourceDetector(ResourceDetector): |
| 67 | + """Detects attribute values only available when the app is running on AWS |
| 68 | + Elastic Compute Cloud (EC2) and returns them in a Resource. |
| 69 | +
|
| 70 | + Uses a special URI to get instance meta-data. See more: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html |
| 71 | + """ |
| 72 | + |
| 73 | + def detect(self) -> "Resource": |
| 74 | + try: |
| 75 | + token = _get_token() |
| 76 | + identity_dict = json.loads(_get_identity(token)) |
| 77 | + hostname = _get_host(token) |
| 78 | + |
| 79 | + return Resource( |
| 80 | + { |
| 81 | + ResourceAttributes.CLOUD_PROVIDER: CloudProviderValues.AWS.value, |
| 82 | + ResourceAttributes.CLOUD_PLATFORM: CloudPlatformValues.AWS_EC2.value, |
| 83 | + ResourceAttributes.CLOUD_ACCOUNT_ID: identity_dict[ |
| 84 | + "accountId" |
| 85 | + ], |
| 86 | + ResourceAttributes.CLOUD_REGION: identity_dict["region"], |
| 87 | + ResourceAttributes.CLOUD_AVAILABILITY_ZONE: identity_dict[ |
| 88 | + "availabilityZone" |
| 89 | + ], |
| 90 | + ResourceAttributes.HOST_ID: identity_dict["instanceId"], |
| 91 | + ResourceAttributes.HOST_TYPE: identity_dict[ |
| 92 | + "instanceType" |
| 93 | + ], |
| 94 | + ResourceAttributes.HOST_NAME: hostname, |
| 95 | + } |
| 96 | + ) |
| 97 | + # pylint: disable=broad-except |
| 98 | + except Exception as exception: |
| 99 | + if self.raise_on_error: |
| 100 | + raise exception |
| 101 | + |
| 102 | + logger.warning("%s failed: %s", self.__class__.__name__, exception) |
| 103 | + return Resource.get_empty() |
0 commit comments