2121namespace OpenTelemetry \Aws \Ec2 ;
2222
2323use GuzzleHttp \Client ;
24- use GuzzleHttp \Exception \RequestException ;
2524use GuzzleHttp \Psr7 \Request ;
26- use OpenTelemetry \SDK \Resource \ResourceConstants ;
2725use OpenTelemetry \SDK \Resource \ResourceInfo ;
2826use OpenTelemetry \SDK \Trace \Attributes ;
27+ use OpenTelemetry \SemConv \ResourceAttributes ;
28+ use Throwable ;
2929
3030/**
3131 * The AwsEc2Detector can be used to detect if a process is running in AWS EC2
@@ -58,10 +58,14 @@ public function __construct(Client $guzzle)
5858 * if the connection or parsing of the identity document fails.
5959 *
6060 */
61- public function detect ()
61+ public function detect (): ResourceInfo
6262 {
6363 try {
6464 $ token = $ this ->fetchToken ();
65+
66+ if ($ token === null ) {
67+ return ResourceInfo::emptyResource ();
68+ }
6569
6670 $ hostName = $ this ->fetchHostname ($ token );
6771
@@ -76,34 +80,34 @@ public function detect()
7680 foreach ($ identitiesJson as $ key => $ value ) {
7781 switch ($ key ) {
7882 case 'instanceId ' :
79- $ attributes ->setAttribute (ResourceConstants ::HOST_ID , $ value );
83+ $ attributes ->setAttribute (ResourceAttributes ::HOST_ID , $ value );
8084
8185 break ;
8286 case 'availabilityZone ' :
83- $ attributes ->setAttribute (ResourceConstants:: CLOUD_ZONE , $ value );
87+ $ attributes ->setAttribute (ResourceAttributes:: CLOUD_AVAILABILITY_ZONE , $ value );
8488
8589 break ;
8690 case 'instanceType ' :
87- $ attributes ->setAttribute (ResourceConstants ::HOST_TYPE , $ value );
91+ $ attributes ->setAttribute (ResourceAttributes ::HOST_TYPE , $ value );
8892
8993 break ;
9094 case 'imageId ' :
91- $ attributes ->setAttribute (ResourceConstants ::HOST_IMAGE_ID , $ value );
95+ $ attributes ->setAttribute (ResourceAttributes ::HOST_IMAGE_ID , $ value );
9296
9397 break ;
9498 case 'accountId ' :
95- $ attributes ->setAttribute (ResourceConstants ::CLOUD_ACCOUNT_ID , $ value );
99+ $ attributes ->setAttribute (ResourceAttributes ::CLOUD_ACCOUNT_ID , $ value );
96100
97101 break ;
98102 case 'region ' :
99- $ attributes ->setAttribute (ResourceConstants ::CLOUD_REGION , $ value );
103+ $ attributes ->setAttribute (ResourceAttributes ::CLOUD_REGION , $ value );
100104
101105 break ;
102106 }
103107 }
104108
105- $ attributes ->setAttribute (ResourceConstants:: HOST_HOSTNAME , $ hostName );
106- $ attributes ->setAttribute (ResourceConstants ::CLOUD_PROVIDER , self ::CLOUD_PROVIDER );
109+ $ attributes ->setAttribute (ResourceAttributes:: HOST_NAME , $ hostName );
110+ $ attributes ->setAttribute (ResourceAttributes ::CLOUD_PROVIDER , self ::CLOUD_PROVIDER );
107111
108112 return ResourceInfo::create ($ attributes );
109113 } catch (\Throwable $ e ) {
@@ -112,7 +116,7 @@ public function detect()
112116 }
113117 }
114118
115- private function fetchToken ()
119+ private function fetchToken (): ? string
116120 {
117121 return $ this ->request (
118122 'PUT ' ,
@@ -121,24 +125,26 @@ private function fetchToken()
121125 );
122126 }
123127
124- private function fetchIdentity (String $ token )
128+ private function fetchIdentity (String $ token ): ? array
125129 {
126130 $ body = $ this ->request (
127131 'GET ' ,
128132 self ::AWS_INSTANCE_IDENTITY_DOCUMENT_PATH ,
129133 [self ::AWS_METADATA_TOKEN_HEADER => $ token ]
130134 );
131135
132- $ json = json_decode ($ body , true );
133-
134- if (isset ($ json )) {
135- return $ json ;
136+ if (empty ($ body )) {
137+ return null ;
136138 }
137139
138- return null ;
140+ try {
141+ return json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
142+ } catch (Throwable $ t ) {
143+ return null ;
144+ }
139145 }
140146
141- private function fetchHostname (String $ token )
147+ private function fetchHostname (String $ token ): ? string
142148 {
143149 return $ this ->request (
144150 'GET ' ,
@@ -151,7 +157,7 @@ private function fetchHostname(String $token)
151157 * Function to create a request for any of the given
152158 * fetch functions.
153159 */
154- private function request ($ method , $ path , $ header )
160+ private function request ($ method , $ path , $ header ): ? string
155161 {
156162 $ client = $ this ->guzzle ;
157163
@@ -173,7 +179,7 @@ private function request($method, $path, $header)
173179 }
174180
175181 return null ;
176- } catch (RequestException $ e ) {
182+ } catch (Throwable $ e ) {
177183 // TODO: add log for exception. The code below
178184 // provides the exception thrown:
179185 // echo Psr7\Message::toString($e->getRequest());
0 commit comments