Skip to content

Commit f52dd35

Browse files
authored
Use PSR Http Client instead of Guzzle (#50)
* add php-http/message library to get psr7 interfaces * replace guzzle to psr7 http client * add request factory to test ec2 detector * replace guzzle to psr7 in eks detector * add request factory to eks detector test * Supress UndefinedDocblockClass in ContainerBuilder Refference to issue: vimeo/psalm#7738 Symfony isn't waiting to implement the new PHP features until they limit they range versions to fully support those features. The PHP Enums are not compatible until PHP 8.1 so the Psalm is reporting the issue, that UnitEnum type not exist. There are no fix for this issue yet in Psalm or in Psalm Symfony Plugin. Only way to fix it at this point is to suppress the error but limit that to specific class and specific file in code. This is done in this commit.
1 parent 16960b1 commit f52dd35

File tree

6 files changed

+97
-79
lines changed

6 files changed

+97
-79
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"php": "^7.4 || ^8.0",
1616
"ext-json": "*",
1717
"open-telemetry/opentelemetry": "~0.0.6",
18-
"php-http/message": "^1.12",
19-
"php-http/discovery": "^1.14"
18+
"php-http/discovery": "^1.14",
19+
"php-http/message": "^1.12"
2020
},
2121
"replace": {
2222
"open-telemetry/otel-sdk-bundle": "self.version"

psalm.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@
1717
<plugins>
1818
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
1919
</plugins>
20+
<issueHandlers>
21+
<UndefinedDocblockClass>
22+
<errorLevel type="suppress">
23+
<file name="src/Symfony/OtelSdkBundle/DependencyInjection/OtelSdkExtension.php" />
24+
<referencedClass name="Symfony\Component\DependencyInjection\ContainerBuilder" />
25+
</errorLevel>
26+
</UndefinedDocblockClass>
27+
</issueHandlers>
2028
</psalm>

src/Aws/Ec2/Detector.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020

2121
namespace OpenTelemetry\Aws\Ec2;
2222

23-
use GuzzleHttp\Client;
24-
use GuzzleHttp\Psr7\Request;
2523
use OpenTelemetry\SDK\Attributes;
2624
use OpenTelemetry\SDK\Resource\ResourceDetectorInterface;
2725
use OpenTelemetry\SDK\Resource\ResourceInfo;
2826
use OpenTelemetry\SemConv\ResourceAttributes;
27+
use Psr\Http\Client\ClientInterface;
28+
use Psr\Http\Message\RequestFactoryInterface;
29+
use Psr\Http\Message\RequestInterface;
2930
use Throwable;
3031

3132
/**
@@ -42,14 +43,15 @@ class Detector implements ResourceDetectorInterface
4243
private const AWS_INSTANCE_HOST_DOCUMENT_PATH = '/latest/meta-data/hostname';
4344
private const AWS_METADATA_TTL_HEADER = 'X-aws-ec2-metadata-token-ttl-seconds';
4445
private const AWS_METADATA_TOKEN_HEADER = 'X-aws-ec2-metadata-token';
45-
private const MILLISECOND_TIME_OUT = 1000;
4646
private const CLOUD_PROVIDER = 'aws';
4747

48-
private Client $guzzle;
48+
private ClientInterface $client;
49+
private RequestFactoryInterface $requestFactory;
4950

50-
public function __construct(Client $guzzle)
51+
public function __construct(ClientInterface $client, RequestFactoryInterface $requestFactory)
5152
{
52-
$this->guzzle = $guzzle;
53+
$this->client = $client;
54+
$this->requestFactory = $requestFactory;
5355
}
5456

5557
/**
@@ -119,20 +121,17 @@ public function getResource(): ResourceInfo
119121

120122
private function fetchToken(): ?string
121123
{
122-
return $this->request(
123-
'PUT',
124-
self::AWS_INSTANCE_TOKEN_DOCUMENT_PATH,
125-
[self::AWS_METADATA_TTL_HEADER => '60']
126-
);
124+
$request = $this->createRequest('PUT', self::AWS_INSTANCE_TOKEN_DOCUMENT_PATH)
125+
->withHeader(self::AWS_METADATA_TTL_HEADER, '60');
126+
127+
return $this->request($request);
127128
}
128129

129130
private function fetchIdentity(String $token): ?array
130131
{
131-
$body = $this->request(
132-
'GET',
133-
self::AWS_INSTANCE_IDENTITY_DOCUMENT_PATH,
134-
[self::AWS_METADATA_TOKEN_HEADER => $token]
135-
);
132+
$request = $this->createRequest('GET', self::AWS_INSTANCE_IDENTITY_DOCUMENT_PATH)
133+
->withHeader(self::AWS_METADATA_TOKEN_HEADER, $token);
134+
$body = $this->request($request);
136135

137136
if (empty($body)) {
138137
return null;
@@ -147,30 +146,27 @@ private function fetchIdentity(String $token): ?array
147146

148147
private function fetchHostname(String $token): ?string
149148
{
150-
return $this->request(
151-
'GET',
152-
self::AWS_INSTANCE_HOST_DOCUMENT_PATH,
153-
[self::AWS_METADATA_TOKEN_HEADER => $token]
154-
);
149+
$request = $this->createRequest('GET', self::AWS_INSTANCE_HOST_DOCUMENT_PATH)
150+
->withHeader(self::AWS_METADATA_TOKEN_HEADER, $token);
151+
152+
return $this->request($request);
153+
}
154+
155+
private function createRequest(string $method, string $path): RequestInterface
156+
{
157+
return $this->requestFactory->createRequest($method, self::SCHEME . self::AWS_IDMS_ENDPOINT . $path);
155158
}
156159

157160
/**
158161
* Function to create a request for any of the given
159162
* fetch functions.
160163
*/
161-
private function request($method, $path, $header): ?string
164+
private function request(RequestInterface $request): ?string
162165
{
163-
$client = $this->guzzle;
166+
$client = $this->client;
164167

165168
try {
166-
$response = $client->request(
167-
$method,
168-
self::SCHEME . self::AWS_IDMS_ENDPOINT . $path,
169-
[
170-
'headers' => $header,
171-
'timeout' => self::MILLISECOND_TIME_OUT,
172-
]
173-
);
169+
$response = $client->sendRequest($request);
174170

175171
$body = $response->getBody()->getContents();
176172
$responseCode = $response->getStatusCode();

src/Aws/Eks/Detector.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
namespace OpenTelemetry\Aws\Eks;
2121

22-
use GuzzleHttp\Client;
23-
use GuzzleHttp\Exception\RequestException;
24-
use GuzzleHttp\Psr7\Request;
2522
use OpenTelemetry\SDK\Attributes;
2623
use OpenTelemetry\SDK\Resource\ResourceDetectorInterface;
2724
use OpenTelemetry\SDK\Resource\ResourceInfo;
2825
use OpenTelemetry\SemConv\ResourceAttributes;
26+
use Psr\Http\Client\ClientExceptionInterface;
27+
use Psr\Http\Client\ClientInterface;
28+
use Psr\Http\Message\RequestFactoryInterface;
2929

3030
/**
3131
* The AwsEksDetector can be used to detect if a process is running in AWS
@@ -45,12 +45,17 @@ class Detector implements ResourceDetectorInterface
4545
private const CONTAINER_ID_LENGTH = 64;
4646

4747
private DataProvider $dataProvider;
48-
private Client $client;
49-
50-
public function __construct(DataProvider $dataProvider, Client $client)
51-
{
48+
private ClientInterface $client;
49+
private RequestFactoryInterface $requestFactory;
50+
51+
public function __construct(
52+
DataProvider $dataProvider,
53+
ClientInterface $client,
54+
RequestFactoryInterface $requestFactory
55+
) {
5256
$this->dataProvider = $dataProvider;
5357
$this->client = $client;
58+
$this->requestFactory = $requestFactory;
5459
}
5560

5661
public function getResource(): ResourceInfo
@@ -104,16 +109,11 @@ public function getClusterName(): ?string
104109
$client = $this->client;
105110

106111
try {
107-
$response = $client->request(
108-
'GET',
109-
'https://' . self::K8S_SVC_URL . self::CW_CONFIGMAP_PATH,
110-
[
111-
'headers' => [
112-
'Authorization' => $this->dataProvider->getK8sHeader() . ', Bearer ' . self::K8S_CERT_PATH,
113-
],
114-
'timeout' => 2000,
115-
]
116-
);
112+
$request = $this->requestFactory
113+
->createRequest('GET', 'https://' . self::K8S_SVC_URL . self::CW_CONFIGMAP_PATH)
114+
->withHeader('Authorization', $this->dataProvider->getK8sHeader() . ', Bearer ' . self::K8S_CERT_PATH);
115+
116+
$response = $client->sendRequest($request);
117117

118118
$json = json_decode($response->getBody()->getContents(), true);
119119

@@ -122,7 +122,7 @@ public function getClusterName(): ?string
122122
}
123123

124124
return null;
125-
} catch (RequestException $e) {
125+
} catch (ClientExceptionInterface $e) {
126126
// TODO: add log for exception. The code below
127127
// provides the exception thrown:
128128
// echo Psr7\Message::toString($e->getRequest());
@@ -140,22 +140,17 @@ public function isEks(): bool
140140
$client = $this->client;
141141

142142
try {
143-
$response = $client->request(
144-
'GET',
145-
'https://' . self::K8S_SVC_URL . self::AUTH_CONFIGMAP_PATH,
146-
[
147-
'headers' => [
148-
'Authorization' => $this->dataProvider->getK8sHeader() . ', Bearer ' . self::K8S_CERT_PATH,
149-
],
150-
'timeout' => 2000,
151-
]
152-
);
143+
$request = $this->requestFactory
144+
->createRequest('GET', 'https://' . self::K8S_SVC_URL . self::AUTH_CONFIGMAP_PATH)
145+
->withHeader('Authorization', $this->dataProvider->getK8sHeader() . ', Bearer ' . self::K8S_CERT_PATH);
146+
147+
$response = $client->sendRequest($request);
153148

154149
$body = $response->getBody()->getContents();
155150
$responseCode = $response->getStatusCode();
156151

157152
return !empty($body) && $responseCode < 300 && $responseCode >= 200;
158-
} catch (RequestException $e) {
153+
} catch (ClientExceptionInterface $e) {
159154
// TODO: add log for exception. The code below
160155
// provides the exception thrown:
161156
// echo Psr7\Message::toString($e->getRequest());

tests/Unit/Aws/Ec2/DetectorTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GuzzleHttp\Client;
88
use GuzzleHttp\Handler\MockHandler;
99
use GuzzleHttp\HandlerStack;
10+
use GuzzleHttp\Psr7\HttpFactory;
1011
use GuzzleHttp\Psr7\Response;
1112
use OpenTelemetry\Aws\Ec2\Detector;
1213
use OpenTelemetry\SDK\Attributes;
@@ -58,8 +59,9 @@ public function TestValidEc2()
5859

5960
$handlerStack = HandlerStack::create($mockGuzzle);
6061
$client = new Client(['handler' => $handlerStack]);
62+
$requestFactory = new HttpFactory();
6163

62-
$detector = new Detector($client);
64+
$detector = new Detector($client, $requestFactory);
6365

6466
$this->assertEquals(
6567
new Attributes(
@@ -94,8 +96,9 @@ public function TestInvalidTokenBody()
9496

9597
$handlerStack = HandlerStack::create($mockGuzzle);
9698
$client = new Client(['handler' => $handlerStack]);
99+
$requestFactory = new HttpFactory();
97100

98-
$detector = new Detector($client);
101+
$detector = new Detector($client, $requestFactory);
99102

100103
$this->assertEquals(ResourceInfo::emptyResource(), $detector->getResource());
101104
}
@@ -116,8 +119,9 @@ public function TestInvalidTokenResponseCode()
116119

117120
$handlerStack = HandlerStack::create($mockGuzzle);
118121
$client = new Client(['handler' => $handlerStack]);
122+
$requestFactory = new HttpFactory();
119123

120-
$detector = new Detector($client);
124+
$detector = new Detector($client, $requestFactory);
121125

122126
$this->assertEquals(ResourceInfo::emptyResource(), $detector->getResource());
123127
}
@@ -138,8 +142,9 @@ public function TestInvalidHostName()
138142

139143
$handlerStack = HandlerStack::create($mockGuzzle);
140144
$client = new Client(['handler' => $handlerStack]);
145+
$requestFactory = new HttpFactory();
141146

142-
$detector = new Detector($client);
147+
$detector = new Detector($client, $requestFactory);
143148

144149
$this->assertEquals(
145150
new Attributes(
@@ -173,8 +178,9 @@ public function TestInvalidIdentities()
173178

174179
$handlerStack = HandlerStack::create($mockGuzzle);
175180
$client = new Client(['handler' => $handlerStack]);
181+
$requestFactory = new HttpFactory();
176182

177-
$detector = new Detector($client);
183+
$detector = new Detector($client, $requestFactory);
178184

179185
$this->assertEquals(ResourceInfo::emptyResource(), $detector->getResource());
180186
}
@@ -195,8 +201,9 @@ public function TestInvalidIncompleteIdentities()
195201

196202
$handlerStack = HandlerStack::create($mockGuzzle);
197203
$client = new Client(['handler' => $handlerStack]);
204+
$requestFactory = new HttpFactory();
198205

199-
$detector = new Detector($client);
206+
$detector = new Detector($client, $requestFactory);
200207

201208
$this->assertEquals(
202209
new Attributes(

0 commit comments

Comments
 (0)