Skip to content

Commit 6ae7ac9

Browse files
authored
Fix compatibility with main package (#34)
* Fix compatibility with main package * Use SemConv\ResourceAttributes in AWS tests * Use correct PHP version in psalm runs * Use mikey179/vfsstream * Add AWS tests * Add .env file * Use different PHP versions in make * Add PHP 8.1 to test matrix * Add PHP 8.1 to test matrix * Fix filesystem tests * Fix style * Use correct PHP version for psalm runs * Add Integration Tests * Remove debug output * Fix typo
1 parent 3d1031b commit 6ae7ac9

23 files changed

+331
-254
lines changed

.env.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PHP_USER=php
2+
XDEBUG_MODE=debug

.github/workflows/php.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
operating-system: [ubuntu-latest]
17-
php-versions: ['7.4', '8.0']
17+
php-versions: ['7.4', '8.0', '8.1']
1818

1919
steps:
2020
- uses: actions/checkout@v2
@@ -55,13 +55,16 @@ jobs:
5555
run: vendor/bin/phan
5656

5757
- name: Run Psalm
58-
run: vendor/bin/psalm --output-format=github
58+
run: vendor/bin/psalm --output-format=github --php-version=${{ matrix.php-versions }}
5959

6060
- name: Run Phpstan
6161
run: vendor/bin/phpstan analyse --error-format=github
6262

63-
- name: Run PHPUnit
63+
- name: Run Unit Tests
6464
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
6565

66+
- name: Run Integration tests
67+
run: vendor/bin/phpunit --coverage-text tests/Integration
68+
6669
- name: Code Coverage
6770
run: bash <(curl -s https://codecov.io/bash)

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ phpstan.neon
1515
tests/TraceContext/W3CTestService/test_app
1616
tests/TraceContext/W3CTestService/trace-context
1717

18+
/.env

Makefile

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
PHP_VERSION ?= 7.4
12
DC_RUN_PHP = docker-compose run --rm php
23

4+
all: update style phan psalm phpstan test
35
install:
4-
$(DC_RUN_PHP) composer install
6+
$(DC_RUN_PHP) env XDEBUG_MODE=off composer install
57
update:
6-
$(DC_RUN_PHP) composer update
8+
$(DC_RUN_PHP) env XDEBUG_MODE=off composer update
79
test:
8-
$(DC_RUN_PHP) php ./vendor/bin/phpunit --colors=always --coverage-text --testdox --coverage-clover coverage.clover
10+
$(DC_RUN_PHP) env XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always --coverage-text --testdox --coverage-clover coverage.clover
11+
test-coverage:
12+
$(DC_RUN_PHP) env XDEBUG_MODE=coverage vendor/bin/phpunit --colors=always --testdox --coverage-html=tests/coverage/html
913
phan:
10-
$(DC_RUN_PHP) env PHAN_DISABLE_XDEBUG_WARN=1 php ./vendor/bin/phan
14+
$(DC_RUN_PHP) env XDEBUG_MODE=off env PHAN_DISABLE_XDEBUG_WARN=1 vendor/bin/phan
1115
psalm:
12-
$(DC_RUN_PHP) php ./vendor/bin/psalm
16+
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/psalm --threads=1 --no-cache --php-version=${PHP_VERSION}
1317
psalm-info:
14-
$(DC_RUN_PHP) php ./vendor/bin/psalm --show-info=true
15-
phpstan:
16-
$(DC_RUN_PHP) php ./vendor/bin/phpstan analyse
18+
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/psalm --show-info=true --threads=1
19+
phpstan:
20+
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/phpstan analyse
1721
bash:
1822
$(DC_RUN_PHP) bash
1923
style:
20-
$(DC_RUN_PHP) php ./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --using-cache=no -vvv
24+
$(DC_RUN_PHP) env XDEBUG_MODE=off vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --using-cache=no -vvv
2125
FORCE:

README.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,13 @@ For more information, please, consult the documentation of the main [OpenTelemet
88
## Installation
99
The recommended way to install the library is through [Composer](http://getcomposer.org):
1010

11-
1.) Install the composer package using [Composer's installation instructions](https://getcomposer.org/doc/00-intromd#installation-linux-unix-macos).
12-
13-
2.) Add
14-
```bash
15-
"minimum-stability": "dev",
16-
"prefer-stable": true,
17-
"repositories": [
18-
{
19-
"type": "vcs",
20-
"url": "https://github.com/open-telemetry/opentelemetry-php-contrib"
21-
}
22-
],
23-
```
24-
25-
To your project's `composer.json` file, as this utility has not reached a stable release status yet,
26-
and is not yet registered on packagist.org
27-
28-
3.) Install the dependency with composer:
11+
Install the package:
2912

3013
```bash
3114
$ composer require open-telemetry/opentelemetry-php-contrib
3215
```
3316

34-
## Usage/Examples
17+
## Usage/Examples
3518

3619
### AWS
3720
- You can find examples on how to use the ASW classes in the [examples directory](/examples/aws/README.md).

composer.json

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111
],
1212
"minimum-stability": "dev",
1313
"prefer-stable": true,
14-
"repositories": [
15-
{
16-
"type": "vcs",
17-
"url": "https://github.com/open-telemetry/opentelemetry-php"
18-
}
19-
],
2014
"require": {
2115
"php": "^7.4 || ^8.0",
2216
"ext-json": "*",
23-
"open-telemetry/opentelemetry": "dev-main",
17+
"open-telemetry/opentelemetry": "^0.0.4",
2418
"php-http/message": "^1.12",
2519
"php-http/discovery": "^1.14"
2620
},
@@ -41,26 +35,27 @@
4135
}
4236
},
4337
"require-dev": {
44-
"phpunit/phpunit": "^9.5",
4538
"composer/xdebug-handler": "^2.0",
46-
"phan/phan": "^4.1",
39+
"dg/bypass-finals": "^1.3",
4740
"friendsofphp/php-cs-fixer": "^3.0",
48-
"vimeo/psalm": "^4.0",
49-
"phpstan/phpstan": "^0.12.50",
50-
"phpstan/phpstan-phpunit": "^0.12.16",
51-
"psalm/plugin-phpunit": "^0.13.0",
5241
"guzzlehttp/guzzle": "^7.3",
5342
"guzzlehttp/psr7": "^2.0@RC",
54-
"symfony/http-client": "^5.3",
43+
"kriswallsmith/buzz": "^1.2",
44+
"mikey179/vfsstream": "^1.6",
5545
"nyholm/psr7": "^1.4",
46+
"phan/phan": "^4.1",
47+
"phpstan/phpstan": "^0.12.50",
48+
"phpstan/phpstan-phpunit": "^0.12.16",
49+
"phpunit/phpunit": "^9.5",
50+
"psalm/plugin-phpunit": "^0.13.0",
5651
"symfony/config": "^4.4|^5.0|^6.0",
52+
"symfony/http-client": "^5.3",
5753
"symfony/http-kernel": "^4.4|^5.3|^6.0",
54+
"symfony/options-resolver": "^4.4|^5.3|^6.0",
5855
"symfony/polyfill-php80": "^1.16",
59-
"symfony/yaml": "^4.4|^5.3|^6.0",
6056
"symfony/proxy-manager-bridge": "^4.4|^5.3|^6.0",
61-
"symfony/options-resolver": "^4.4|^5.3|^6.0",
62-
"dg/bypass-finals": "^1.3",
63-
"kriswallsmith/buzz": "^1.2"
57+
"symfony/yaml": "^4.4|^5.3|^6.0",
58+
"vimeo/psalm": "^4.0"
6459
},
6560
"suggest": {
6661
"symfony/config": "Needed to use otel-sdk-bundle",
@@ -74,5 +69,8 @@
7469
"post-update-cmd": [
7570
"composer dump-autoload --optimize"
7671
]
72+
},
73+
"config": {
74+
"sort-packages": true
7775
}
7876
}

docker-compose.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
version: '3.7'
22
services:
33
php:
4-
image: ghcr.io/open-telemetry/opentelemetry-php/opentelemetry-php-base:latest
4+
image: ghcr.io/open-telemetry/opentelemetry-php/opentelemetry-php-base:${PHP_VERSION:-7.4}
55
volumes:
66
- ./:/usr/src/myapp
7+
user: "${PHP_USER}:root"
8+
environment:
9+
XDEBUG_MODE: ${XDEBUG_MODE:-off}
10+
XDEBUG_CONFIG: ${XDEBUG_CONFIG:-''}
711
zipkin:
812
image: openzipkin/zipkin-slim
913
ports:

psalm.xml.dist

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
xmlns="https://getpsalm.org/schema/config"
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
88
<projectFiles>
9-
<directory name="."/>
9+
<directory name="src"/>
10+
<directory name="tests"/>
1011
<ignoreFiles>
11-
<directory name="var"/>
12-
<directory name="vendor"/>
1312
<directory name="examples"/> <!-- TODO: Remove this once examples are updated -->
1413
<directory name="src/Symfony/OtelSdkBundle/Resources"/>
1514
<file name="src/Symfony/OtelSdkBundle/DependencyInjection/Configuration.php"/>

src/Aws/Ec2/Detector.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
namespace OpenTelemetry\Aws\Ec2;
2222

2323
use GuzzleHttp\Client;
24-
use GuzzleHttp\Exception\RequestException;
2524
use GuzzleHttp\Psr7\Request;
26-
use OpenTelemetry\SDK\Resource\ResourceConstants;
2725
use OpenTelemetry\SDK\Resource\ResourceInfo;
2826
use 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());

src/Aws/Ecs/DataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DataProvider
1313
* This would be the os the container is running on,
1414
* i.e. the platform on which it is deployed
1515
*/
16-
public function getHostName()
16+
public function getHostName(): ?string
1717
{
1818
return php_uname('n');
1919
}

0 commit comments

Comments
 (0)