Skip to content

Commit da7a997

Browse files
authored
[sampler-xray]Added AWS X-Ray Sampler implementation (#405)
* [aws-sdk]Added Initial auto-instrumentation library for aws-sdk * Updated to use sem covs instead of hardcoding * added to gitsplit * added gitattributes file * Updated to use the latest semcov 1.32 * Added aws tests to php.yml * Updated composer for php 8.1 and fixed style issue * Fixed psalm errors * initial sampler implementation * Updated sampler code * added unit tests * updated README * updated after running style checks * updated more files * Fixed build issues * added gitkeep file * used otel clock instead of new class * added entry to gitsplit
1 parent da45e60 commit da7a997

30 files changed

+2247
-0
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ jobs:
6262
'ResourceDetectors/Container',
6363
'ResourceDetectors/DigitalOcean',
6464
'Sampler/RuleBased',
65+
'Sampler/Xray',
6566
'Shims/OpenTracing',
6667
'Symfony',
6768
'Utils/Test'

.gitsplit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ splits:
8888
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-detector-digitalocean.git"
8989
- prefix: "src/Sampler/RuleBased"
9090
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sampler-rulebased.git"
91+
- prefix: "src/Sampler/Xray"
92+
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-sampler-aws-xray.git"
9193
- prefix: "src/Shims/OpenTracing"
9294
target: "https://${GH_TOKEN}@github.com/opentelemetry-php/contrib-shim-opentracing.git"
9395
- prefix: "src/Symfony"

src/Sampler/Xray/.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* text=auto
2+
3+
*.md diff=markdown
4+
*.php diff=php
5+
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/.phan export-ignore
9+
/.php-cs-fixer.php export-ignore
10+
/phpstan.neon.dist export-ignore
11+
/phpunit.xml.dist export-ignore
12+
/psalm.xml.dist export-ignore
13+
/tests export-ignore

src/Sampler/Xray/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
composer.lock
2+
3+
.phpunit.cache
4+
5+
var
6+
vendor

src/Sampler/Xray/.phan/config.php

Lines changed: 370 additions & 0 deletions
Large diffs are not rendered by default.

src/Sampler/Xray/.php-cs-fixer.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
$finder = PhpCsFixer\Finder::create()
3+
->exclude('vendor')
4+
->exclude('var/cache')
5+
->in(__DIR__);
6+
7+
$config = new PhpCsFixer\Config();
8+
return $config->setRules([
9+
'concat_space' => ['spacing' => 'one'],
10+
'declare_equal_normalize' => ['space' => 'none'],
11+
'is_null' => true,
12+
'modernize_types_casting' => true,
13+
'ordered_imports' => true,
14+
'php_unit_construct' => true,
15+
'single_line_comment_style' => true,
16+
'yoda_style' => false,
17+
'@PSR2' => true,
18+
'array_syntax' => ['syntax' => 'short'],
19+
'blank_line_after_opening_tag' => true,
20+
'blank_line_before_statement' => true,
21+
'cast_spaces' => true,
22+
'declare_strict_types' => true,
23+
'type_declaration_spaces' => true,
24+
'include' => true,
25+
'lowercase_cast' => true,
26+
'new_with_parentheses' => true,
27+
'no_extra_blank_lines' => true,
28+
'no_leading_import_slash' => true,
29+
'echo_tag_syntax' => true,
30+
'no_unused_imports' => true,
31+
'no_useless_else' => true,
32+
'no_useless_return' => true,
33+
'phpdoc_order' => true,
34+
'phpdoc_scalar' => true,
35+
'phpdoc_types' => true,
36+
'short_scalar_cast' => true,
37+
'blank_lines_before_namespace' => true,
38+
'single_quote' => true,
39+
'trailing_comma_in_multiline' => true,
40+
])
41+
->setRiskyAllowed(true)
42+
->setFinder($finder);
43+

src/Sampler/Xray/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# AWS X-Ray Sampler
2+
3+
Provides a sampler which can get sampling configurations from AWS X-Ray to make sampling decisions. See: [AWS X-Ray Sampling](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-sampling)
4+
5+
## Installation
6+
7+
```shell
8+
composer require open-telemetry/sampler-aws-xray
9+
```
10+
11+
## Configuration
12+
You can configure the `AWSXRayRemoteSampler` as per the following example.
13+
Note that you will need to configure your [OpenTelemetry Collector for
14+
X-Ray remote sampling](https://aws-otel.github.io/docs/getting-started/remote-sampling).
15+
16+
```php
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
require __DIR__ . '/vendor/autoload.php';
22+
23+
use OpenTelemetry\SDK\Trace\TracerProvider;
24+
use OpenTelemetry\SDK\Common\Attribute\Attributes;
25+
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporterFactory;
26+
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
27+
use OpenTelemetry\SDK\Resource\ResourceInfo;
28+
use OpenTelemetry\Contrib\Sampler\Xray\AWSXRayRemoteSampler;
29+
30+
$resource = ResourceInfo::create(Attributes::create([
31+
'service.name' => 'MyServiceName',
32+
'service.version'=> '1.0.0',
33+
'cloud.provider' => 'aws',
34+
]));
35+
36+
$xraySampler = new AWSXRayRemoteSampler(
37+
$resource,
38+
'http://localhost:2000',
39+
2
40+
);
41+
42+
$tracerProvider = TracerProvider::builder()
43+
->setResource($resource)
44+
->setSampler($xraySampler)
45+
->addSpanProcessor(
46+
new SimpleSpanProcessor(
47+
(new ConsoleSpanExporterFactory())->create()
48+
)
49+
)
50+
->build();
51+
```

src/Sampler/Xray/composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "open-telemetry/sampler-aws-xray",
3+
"description": "AWS X-Ray Remote Sampler for OpenTelemetry PHP Contrib",
4+
"type": "library",
5+
"license": "Apache-2.0",
6+
"require": {
7+
"php": "^8.1",
8+
"aws/aws-sdk-php": "^3.0",
9+
"open-telemetry/api": "^1.1.0",
10+
"open-telemetry/sdk": "^1.1.0",
11+
"open-telemetry/sdk-configuration": "^0.0.5",
12+
"open-telemetry/sem-conv": "^1.32"
13+
},
14+
"require-dev": {
15+
"symfony/config": "^5.4 || ^6.4 || ^7.0",
16+
"symfony/yaml": "^6 || ^7",
17+
"friendsofphp/php-cs-fixer": "^3",
18+
"phan/phan": "^5.0",
19+
"phpstan/phpstan": "^1.1",
20+
"phpstan/phpstan-phpunit": "^1.0",
21+
"psalm/plugin-phpunit": "^0.19.2",
22+
"phpunit/phpunit": "^10 || ^11",
23+
"vimeo/psalm": "^4|^5|6.4.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"OpenTelemetry\\Contrib\\Sampler\\Xray\\": "src/"
28+
},
29+
"classmap": ["src/AWSXRayRemoteSampler.php"]
30+
},
31+
"minimum-stability": "dev",
32+
"prefer-stable": true,
33+
"config": {
34+
"allow-plugins": {
35+
"php-http/discovery": true,
36+
"tbachert/spi": true
37+
}
38+
}
39+
}

src/Sampler/Xray/phpstan.neon.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- vendor/phpstan/phpstan-phpunit/extension.neon
3+
4+
parameters:
5+
tmpDir: var/cache/phpstan
6+
level: 5
7+
paths:
8+
- src
9+
- tests

src/Sampler/Xray/phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" cacheResult="false" colors="false" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" stopOnRisky="false" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60" cacheDirectory=".phpunit.cache" backupStaticProperties="false" requireCoverageMetadata="false">
3+
<php>
4+
<ini name="date.timezone" value="UTC"/>
5+
<ini name="display_errors" value="On"/>
6+
<ini name="display_startup_errors" value="On"/>
7+
<ini name="error_reporting" value="E_ALL"/>
8+
</php>
9+
<testsuites>
10+
<testsuite name="unit">
11+
<directory>tests/Unit</directory>
12+
</testsuite>
13+
<testsuite name="integration">
14+
<directory>tests/Integration</directory>
15+
</testsuite>
16+
</testsuites>
17+
<source>
18+
<include>
19+
<directory>src</directory>
20+
</include>
21+
</source>
22+
</phpunit>

0 commit comments

Comments
 (0)