Skip to content

Commit ffda596

Browse files
Merge pull request #52 from mtk3d/fix/example-app1
WIP: Fix first AWS example app
2 parents 723ec03 + a2d81b6 commit ffda596

File tree

6 files changed

+207
-177
lines changed

6 files changed

+207
-177
lines changed

examples/aws/AwsClientApp/bin/app

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require_once __DIR__ . '/../src/main.php';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "open-telemetry/s3bucket-client-app",
3+
"type": "application",
4+
"license": "Apache-2.0",
5+
"minimum-stability": "dev",
6+
"autoload": {
7+
"psr-4": {
8+
"OpenTelemetry\\S3bucketClientApp\\": "src/"
9+
}
10+
},
11+
"require": {
12+
"aws/aws-sdk-php": "^3.213",
13+
"php-http/guzzle7-adapter": "^1.0",
14+
"open-telemetry/opentelemetry-php-contrib": "dev-main",
15+
"guzzlehttp/guzzle": "^7.4",
16+
"ext-grpc": "*"
17+
},
18+
"repositories": [
19+
{
20+
"type": "path",
21+
"url": "../../../"
22+
}
23+
]
24+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/*
3+
* Copyright The OpenTelemetry Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace OpenTelemetry\S3bucketClientApp;
21+
22+
use Aws\Exception\AwsException;
23+
use Aws\S3\S3Client;
24+
use GuzzleHttp\Client;
25+
use OpenTelemetry\API\Trace\SpanKind;
26+
use OpenTelemetry\Aws\Xray\IdGenerator;
27+
use OpenTelemetry\Aws\Xray\Propagator;
28+
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter;
29+
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
30+
use OpenTelemetry\SDK\Trace\TracerProvider;
31+
32+
require __DIR__ . '/../vendor/autoload.php';
33+
34+
echo <<<EOL
35+
Starting Aws Client App
36+
37+
Which call would you like to make?
38+
Type outgoing-http-call or aws-sdk-call
39+
40+
EOL;
41+
42+
// Get user input
43+
$handle = fopen('php://stdin', 'r');
44+
$line = trim(fgets($handle));
45+
fclose($handle);
46+
47+
if (!in_array($line, ['outgoing-http-call', 'aws-sdk-call'])) {
48+
echo "Abort!\n";
49+
exit(1);
50+
}
51+
52+
// Create tracer and propagator
53+
$spanProcessor = new SimpleSpanProcessor(new OTLPExporter());
54+
$idGenerator = new IdGenerator();
55+
$tracerProvider = new TracerProvider([$spanProcessor], null, null, null, $idGenerator);
56+
57+
$tracer = $tracerProvider->getTracer();
58+
$propagator = new Propagator();
59+
60+
// Create root span
61+
$root = $tracer->spanBuilder('root')->startSpan();
62+
$root->activate();
63+
64+
$root->setAttribute('item_A', 'cars')
65+
->setAttribute('item_B', 'motorcycles')
66+
->setAttribute('item_C', 'planes');
67+
68+
$carrier = [];
69+
70+
if ($line === 'outgoing-http-call') {
71+
$span = $tracer->spanBuilder('session.generate.http.span')->setSpanKind(SpanKind::KIND_CLIENT)->startSpan();
72+
73+
$propagator->inject($carrier);
74+
75+
try {
76+
$client = new Client();
77+
$client->request('GET', 'https://aws.amazon.com', ['headers' => $carrier, 'timeout' => 2000,]);
78+
} catch (\Throwable $e) {
79+
echo $e->getMessage() . PHP_EOL;
80+
exit(1);
81+
}
82+
83+
printTraceId($span);
84+
85+
$span->end();
86+
}
87+
88+
if ($line === 'aws-sdk-call') {
89+
$span = $tracer->spanBuilder('session.generate.aws.sdk.span')->setSpanKind(SpanKind::KIND_CLIENT)->startSpan();
90+
91+
$propagator->inject($carrier);
92+
93+
$s3Client = new S3Client([
94+
'profile' => 'default',
95+
'region' => 'us-west-2',
96+
'version' => '2006-03-01',
97+
]);
98+
99+
try {
100+
$result = $s3Client->createBucket([
101+
'Bucket' => uniqid('test-bucket-'),
102+
]);
103+
104+
echo <<<EOL
105+
The bucket's location is: {$result['Location']}
106+
The bucket's effective URI is: {$result['@metadata']['effectiveUri']}
107+
108+
EOL;
109+
} catch (AwsException $e) {
110+
echo "Error: {$e->getAwsErrorMessage()}";
111+
}
112+
113+
$buckets = $s3Client->listBuckets();
114+
115+
foreach ($buckets['Buckets'] as $bucket) {
116+
echo $bucket['Name'] . PHP_EOL;
117+
}
118+
119+
printTraceId($span);
120+
121+
$span->end();
122+
}
123+
124+
$root->end();
125+
126+
echo "Aws Client App complete!\n";
127+
128+
function printTraceId($span): void
129+
{
130+
$traceId = $span->getContext()->getTraceId();
131+
$traceIdJson = json_encode([
132+
'traceId' => '1-' . substr($traceId, 0, 8) . '-' . substr($traceId, 8),
133+
]);
134+
echo "Final trace ID: $traceIdJson\n";
135+
}

examples/aws/README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This is a getting started guide for the example applications found in the AWS co
1111

1212
Currently, the ability to instrument an application automatically does not exist, so manually instrumenting the apps was necessary. In both of the applications, creation of a tracer, generation of spans, propagation of contexts, and closing spans was implemented manually. Both of these applications are console applications that export trace data to the OTEL Collector which is then exported to AWS X-Ray.
1313

14-
### Sample App 1
14+
### Aws Client App
1515

1616
The first sample app in its implementation is creation of a span, then a child span, which is then populated in an HTTP header that makes a request to either aws.amazon.com (http://aws.amazon.com/) or the AWS SDK. The application will prompt you for input on which action you would like to take, and subsequently prints out the trace ID.
1717

@@ -90,39 +90,42 @@ At this point all necessary items have been installed in your system and you are
9090

9191
### Run Collector
9292

93-
Open a new terminal window and navigate into the aws-otel-collector folder.
94-
9593
Run the following command. Make sure to replace `YOUR_ACCESS_KEY_HERE` and `YOUR_SECRET_ACCESS_KEY_HERE` with your own specific keys.
9694

9795
```console
9896
docker run --rm -p 4317:4317 -p 55681:55681 -p 8889:8888 \
9997
-e AWS_REGION=us-west-2 \
10098
-e AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_HERE \
10199
-e AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY_HERE \
102-
-v "${PWD}/examples/docker/config-test.yaml":/otel-local-config.yaml \
100+
-v "${PWD}/examples/aws/collector/config.yaml":/otel-local-config.yaml \
103101
--name awscollector public.ecr.aws/aws-observability/aws-otel-collector:latest \
104102
--config otel-local-config.yaml
105103
```
106104

107105
In another terminal window, navigate to the opentelemetry-php-contrib folder.
108106

109-
Run the following command for Sample App 1:
107+
To run `AwsClientApp`, navigate to `examples/aws/AwsClientApp`, then install required dependencies:
108+
109+
```
110+
composer install
111+
```
112+
113+
And run the following command:
110114

111-
`php examples/aws/SampleApp1/SampleApp1.php`
115+
`php bin/app`
112116

113117
The output for this app should look similar to the following:
114118

115119
```console
116-
Starting Sample App
117-
Which call would you like to make?
120+
Starting Aws Client App
121+
122+
Which call would you like to make?
118123
Type outgoing-http-call or aws-sdk-call
119124
outgoing-http-call
120-
Final trace ID: {"traceId":"1-6115648a-d40b50a270b3c1249bcf60c2"}
121-
Sample App complete!
125+
Final trace ID: {"traceId":"1-622fb9fb-1b2031fcde9ac72610b6a0b9"}
126+
Aws Client App complete!
122127
```
123128

124-
Currently the `aws-sdk-call` option is commented out. This is due to dependency conflicts between AWS and the PHP Library. If you would like to enable it, follow the instructions in the comments of the SampleApp1.php file.
125-
126129
Run the following command for Sample App 2:
127130

128131
`php examples/aws/SampleApp2/SampleApp2.php`

examples/aws/collector/config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
receivers:
2+
otlp:
3+
protocols:
4+
grpc:
5+
endpoint: 0.0.0.0:4317
6+
7+
processors:
8+
batch:
9+
10+
exporters:
11+
logging:
12+
loglevel: debug
13+
awsxray:
14+
region: 'us-west-2'
15+
awsemf:
16+
region: 'us-west-2'
17+
18+
service:
19+
pipelines:
20+
traces:
21+
receivers: [otlp]
22+
exporters: [awsxray]
23+
metrics:
24+
receivers: [otlp]
25+
exporters: [awsemf]
26+
27+
telemetry:
28+
logs:
29+
level: debug

0 commit comments

Comments
 (0)