|
16 | 16 |
|
17 | 17 | import { |
18 | 18 | Detector, |
19 | | - Resource, |
| 19 | + IResource, |
20 | 20 | ResourceDetectionConfig, |
21 | 21 | } from '@opentelemetry/resources'; |
22 | | -import { |
23 | | - SEMRESATTRS_CLOUD_PROVIDER, |
24 | | - SEMRESATTRS_CLOUD_PLATFORM, |
25 | | - SEMRESATTRS_CLOUD_REGION, |
26 | | - SEMRESATTRS_CLOUD_ACCOUNT_ID, |
27 | | - SEMRESATTRS_CLOUD_AVAILABILITY_ZONE, |
28 | | - SEMRESATTRS_HOST_ID, |
29 | | - SEMRESATTRS_HOST_TYPE, |
30 | | - SEMRESATTRS_HOST_NAME, |
31 | | - CLOUDPROVIDERVALUES_AWS, |
32 | | - CLOUDPLATFORMVALUES_AWS_EC2, |
33 | | -} from '@opentelemetry/semantic-conventions'; |
34 | | -import * as http from 'http'; |
| 22 | + |
| 23 | +import { awsEc2DetectorSync } from './AwsEc2DetectorSync'; |
35 | 24 |
|
36 | 25 | /** |
37 | 26 | * The AwsEc2Detector can be used to detect if a process is running in AWS EC2 |
38 | 27 | * and return a {@link Resource} populated with metadata about the EC2 |
39 | | - * instance. Returns an empty Resource if detection fails. |
| 28 | + * instance. |
| 29 | + * |
| 30 | + * @deprecated Use {@link AwsEc2DetectorSync} class instead. |
40 | 31 | */ |
41 | 32 | class AwsEc2Detector implements Detector { |
42 | | - /** |
43 | | - * See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html |
44 | | - * for documentation about the AWS instance identity document |
45 | | - * and standard of IMDSv2. |
46 | | - */ |
47 | | - readonly AWS_IDMS_ENDPOINT = '169.254.169.254'; |
48 | | - readonly AWS_INSTANCE_TOKEN_DOCUMENT_PATH = '/latest/api/token'; |
49 | | - readonly AWS_INSTANCE_IDENTITY_DOCUMENT_PATH = |
50 | | - '/latest/dynamic/instance-identity/document'; |
51 | | - readonly AWS_INSTANCE_HOST_DOCUMENT_PATH = '/latest/meta-data/hostname'; |
52 | | - readonly AWS_METADATA_TTL_HEADER = 'X-aws-ec2-metadata-token-ttl-seconds'; |
53 | | - readonly AWS_METADATA_TOKEN_HEADER = 'X-aws-ec2-metadata-token'; |
54 | | - readonly MILLISECOND_TIME_OUT = 5000; |
55 | | - |
56 | | - /** |
57 | | - * Attempts to connect and obtain an AWS instance Identity document. If the |
58 | | - * connection is successful it returns a promise containing a {@link Resource} |
59 | | - * populated with instance metadata. Returns a promise containing an |
60 | | - * empty {@link Resource} if the connection or parsing of the identity |
61 | | - * document fails. |
62 | | - * |
63 | | - * @param config (unused) The resource detection config |
64 | | - */ |
65 | | - async detect(_config?: ResourceDetectionConfig): Promise<Resource> { |
66 | | - const token = await this._fetchToken(); |
67 | | - const { accountId, instanceId, instanceType, region, availabilityZone } = |
68 | | - await this._fetchIdentity(token); |
69 | | - const hostname = await this._fetchHost(token); |
70 | | - |
71 | | - return new Resource({ |
72 | | - [SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS, |
73 | | - [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_EC2, |
74 | | - [SEMRESATTRS_CLOUD_ACCOUNT_ID]: accountId, |
75 | | - [SEMRESATTRS_CLOUD_REGION]: region, |
76 | | - [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: availabilityZone, |
77 | | - [SEMRESATTRS_HOST_ID]: instanceId, |
78 | | - [SEMRESATTRS_HOST_TYPE]: instanceType, |
79 | | - [SEMRESATTRS_HOST_NAME]: hostname, |
80 | | - }); |
81 | | - } |
82 | | - |
83 | | - private async _fetchToken(): Promise<string> { |
84 | | - const options = { |
85 | | - host: this.AWS_IDMS_ENDPOINT, |
86 | | - path: this.AWS_INSTANCE_TOKEN_DOCUMENT_PATH, |
87 | | - method: 'PUT', |
88 | | - timeout: this.MILLISECOND_TIME_OUT, |
89 | | - headers: { |
90 | | - [this.AWS_METADATA_TTL_HEADER]: '60', |
91 | | - }, |
92 | | - }; |
93 | | - return await this._fetchString(options); |
94 | | - } |
95 | | - |
96 | | - private async _fetchIdentity(token: string): Promise<any> { |
97 | | - const options = { |
98 | | - host: this.AWS_IDMS_ENDPOINT, |
99 | | - path: this.AWS_INSTANCE_IDENTITY_DOCUMENT_PATH, |
100 | | - method: 'GET', |
101 | | - timeout: this.MILLISECOND_TIME_OUT, |
102 | | - headers: { |
103 | | - [this.AWS_METADATA_TOKEN_HEADER]: token, |
104 | | - }, |
105 | | - }; |
106 | | - const identity = await this._fetchString(options); |
107 | | - return JSON.parse(identity); |
108 | | - } |
109 | | - |
110 | | - private async _fetchHost(token: string): Promise<string> { |
111 | | - const options = { |
112 | | - host: this.AWS_IDMS_ENDPOINT, |
113 | | - path: this.AWS_INSTANCE_HOST_DOCUMENT_PATH, |
114 | | - method: 'GET', |
115 | | - timeout: this.MILLISECOND_TIME_OUT, |
116 | | - headers: { |
117 | | - [this.AWS_METADATA_TOKEN_HEADER]: token, |
118 | | - }, |
119 | | - }; |
120 | | - return await this._fetchString(options); |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * Establishes an HTTP connection to AWS instance document url. |
125 | | - * If the application is running on an EC2 instance, we should be able |
126 | | - * to get back a valid JSON document. Parses that document and stores |
127 | | - * the identity properties in a local map. |
128 | | - */ |
129 | | - private async _fetchString(options: http.RequestOptions): Promise<string> { |
130 | | - return new Promise((resolve, reject) => { |
131 | | - const timeoutId = setTimeout(() => { |
132 | | - req.abort(); |
133 | | - reject(new Error('EC2 metadata api request timed out.')); |
134 | | - }, this.MILLISECOND_TIME_OUT); |
135 | | - |
136 | | - const req = http.request(options, res => { |
137 | | - clearTimeout(timeoutId); |
138 | | - const { statusCode } = res; |
139 | | - res.setEncoding('utf8'); |
140 | | - let rawData = ''; |
141 | | - res.on('data', chunk => (rawData += chunk)); |
142 | | - res.on('end', () => { |
143 | | - if (statusCode && statusCode >= 200 && statusCode < 300) { |
144 | | - try { |
145 | | - resolve(rawData); |
146 | | - } catch (e) { |
147 | | - reject(e); |
148 | | - } |
149 | | - } else { |
150 | | - reject( |
151 | | - new Error('Failed to load page, status code: ' + statusCode) |
152 | | - ); |
153 | | - } |
154 | | - }); |
155 | | - }); |
156 | | - req.on('error', err => { |
157 | | - clearTimeout(timeoutId); |
158 | | - reject(err); |
159 | | - }); |
160 | | - req.end(); |
161 | | - }); |
| 33 | + detect(_config?: ResourceDetectionConfig): Promise<IResource> { |
| 34 | + return Promise.resolve(awsEc2DetectorSync.detect(_config)); |
162 | 35 | } |
163 | 36 | } |
164 | 37 |
|
|
0 commit comments