|
1 | | -# Contrib Sampler |
| 1 | +# AWS X-Ray Sampler |
2 | 2 |
|
3 | | -Provides additional samplers that are not part of the official specification. |
| 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 | 4 |
|
5 | 5 | ## Installation |
6 | 6 |
|
7 | 7 | ```shell |
8 | | -composer require open-telemetry/sampler-rule-based |
| 8 | +composer require open-telemetry/contrib-aws-xray-sampler |
9 | 9 | ``` |
10 | 10 |
|
11 | | -## RuleBasedSampler |
12 | | - |
13 | | -Allows sampling based on a list of rule sets. The first matching rule set will decide the sampling result. |
| 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). |
14 | 15 |
|
15 | 16 | ```php |
16 | | -$sampler = new RuleBasedSampler( |
17 | | - [ |
18 | | - new RuleSet( |
19 | | - [ |
20 | | - new SpanKindRule(Kind::Server), |
21 | | - new AttributeRule('url.path', '~^/health$~'), |
22 | | - ], |
23 | | - new AlwaysOffSampler(), |
24 | | - ), |
25 | | - ], |
26 | | - new AlwaysOnSampler(), |
27 | | -); |
28 | | -``` |
29 | | - |
30 | | -### Configuration |
31 | | - |
32 | | -###### Example: drop spans for the /health endpoint |
33 | | - |
34 | | -```yaml |
35 | | -contrib_rule_based: |
36 | | - rule_sets: |
37 | | - - rules: |
38 | | - - span_kind: { kind: SERVER } |
39 | | - - attribute: { key: url.path, pattern: ~^/health$~ } |
40 | | - delegate: |
41 | | - always_off: {} |
42 | | - fallback: # ... |
43 | | -``` |
| 17 | +<?php |
44 | 18 |
|
45 | | -###### Example: sample spans with at least one sampled link |
| 19 | +declare(strict_types=1); |
46 | 20 |
|
47 | | -```yaml |
48 | | -contrib_rule_based: |
49 | | - rule_sets: |
50 | | - - rules: [ link: { sampled: true } ] |
51 | | - delegate: |
52 | | - always_on: {} |
53 | | - fallback: # ... |
54 | | -``` |
55 | | -
|
56 | | -###### Example: modeling parent based sampler as rule based sampler |
57 | | -
|
58 | | -```yaml |
59 | | -rule_based: |
60 | | - rule_sets: |
61 | | - - rules: [ parent: { sampled: true, remote: true } ] |
62 | | - delegate: # remote_parent_sampled |
63 | | - - rules: [ parent: { sampled: false, remote: true } ] |
64 | | - delegate: # remote_parent_not_sampled |
65 | | - - rules: [ parent: { sampled: true, remote: false } ] |
66 | | - delegate: # local_parent_sampled |
67 | | - - rules: [ parent: { sampled: false, remote: false } ] |
68 | | - delegate: # local_parent_not_sampled |
69 | | - fallback: # root |
70 | | -``` |
| 21 | +require __DIR__ . '/vendor/autoload.php'; |
71 | 22 |
|
72 | | -## AlwaysRecordingSampler |
| 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; |
73 | 29 |
|
74 | | -Records all spans to allow the usage of span processors that generate metrics from spans. |
| 30 | +$resource = ResourceInfo::create(Attributes::create([ |
| 31 | + 'service.name' => 'MyServiceName', |
| 32 | + 'service.version'=> '1.0.0', |
| 33 | + 'cloud.provider' => 'aws', |
| 34 | +])); |
75 | 35 |
|
76 | | -```php |
77 | | -$sampler = new AlwaysRecordingSampler( |
78 | | - new ParentBasedSampler(new AlwaysOnSampler()), |
| 36 | +$xraySampler = new AWSXRayRemoteSampler( |
| 37 | + $resource, |
| 38 | + 'http://localhost:2000', |
| 39 | + 2 |
79 | 40 | ); |
80 | | -``` |
81 | | - |
82 | | -### Configuration |
83 | 41 |
|
84 | | -```yaml |
85 | | -always_recording: |
86 | | - sampler: # ... |
| 42 | +$tracerProvider = TracerProvider::builder() |
| 43 | + ->setResource($resource) |
| 44 | + ->setSampler($xraySampler) |
| 45 | + ->addSpanProcessor( |
| 46 | + new SimpleSpanProcessor( |
| 47 | + (new ConsoleSpanExporterFactory())->create() |
| 48 | + ) |
| 49 | + ) |
| 50 | + ->build(); |
87 | 51 | ``` |
0 commit comments