Skip to content

Commit

Permalink
Merge pull request #52 from mtk3d/fix/example-app1
Browse files Browse the repository at this point in the history
WIP: Fix first AWS example app
  • Loading branch information
bobstrecansky authored Mar 16, 2022
2 parents 723ec03 + a2d81b6 commit ffda596
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 177 deletions.
4 changes: 4 additions & 0 deletions examples/aws/AwsClientApp/bin/app
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php

require_once __DIR__ . '/../src/main.php';
24 changes: 24 additions & 0 deletions examples/aws/AwsClientApp/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "open-telemetry/s3bucket-client-app",
"type": "application",
"license": "Apache-2.0",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"OpenTelemetry\\S3bucketClientApp\\": "src/"
}
},
"require": {
"aws/aws-sdk-php": "^3.213",
"php-http/guzzle7-adapter": "^1.0",
"open-telemetry/opentelemetry-php-contrib": "dev-main",
"guzzlehttp/guzzle": "^7.4",
"ext-grpc": "*"
},
"repositories": [
{
"type": "path",
"url": "../../../"
}
]
}
135 changes: 135 additions & 0 deletions examples/aws/AwsClientApp/src/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace OpenTelemetry\S3bucketClientApp;

use Aws\Exception\AwsException;
use Aws\S3\S3Client;
use GuzzleHttp\Client;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\Aws\Xray\IdGenerator;
use OpenTelemetry\Aws\Xray\Propagator;
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;

require __DIR__ . '/../vendor/autoload.php';

echo <<<EOL
Starting Aws Client App
Which call would you like to make?
Type outgoing-http-call or aws-sdk-call
EOL;

// Get user input
$handle = fopen('php://stdin', 'r');
$line = trim(fgets($handle));
fclose($handle);

if (!in_array($line, ['outgoing-http-call', 'aws-sdk-call'])) {
echo "Abort!\n";
exit(1);
}

// Create tracer and propagator
$spanProcessor = new SimpleSpanProcessor(new OTLPExporter());
$idGenerator = new IdGenerator();
$tracerProvider = new TracerProvider([$spanProcessor], null, null, null, $idGenerator);

$tracer = $tracerProvider->getTracer();
$propagator = new Propagator();

// Create root span
$root = $tracer->spanBuilder('root')->startSpan();
$root->activate();

$root->setAttribute('item_A', 'cars')
->setAttribute('item_B', 'motorcycles')
->setAttribute('item_C', 'planes');

$carrier = [];

if ($line === 'outgoing-http-call') {
$span = $tracer->spanBuilder('session.generate.http.span')->setSpanKind(SpanKind::KIND_CLIENT)->startSpan();

$propagator->inject($carrier);

try {
$client = new Client();
$client->request('GET', 'https://aws.amazon.com', ['headers' => $carrier, 'timeout' => 2000,]);
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}

printTraceId($span);

$span->end();
}

if ($line === 'aws-sdk-call') {
$span = $tracer->spanBuilder('session.generate.aws.sdk.span')->setSpanKind(SpanKind::KIND_CLIENT)->startSpan();

$propagator->inject($carrier);

$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2006-03-01',
]);

try {
$result = $s3Client->createBucket([
'Bucket' => uniqid('test-bucket-'),
]);

echo <<<EOL
The bucket's location is: {$result['Location']}
The bucket's effective URI is: {$result['@metadata']['effectiveUri']}
EOL;
} catch (AwsException $e) {
echo "Error: {$e->getAwsErrorMessage()}";
}

$buckets = $s3Client->listBuckets();

foreach ($buckets['Buckets'] as $bucket) {
echo $bucket['Name'] . PHP_EOL;
}

printTraceId($span);

$span->end();
}

$root->end();

echo "Aws Client App complete!\n";

function printTraceId($span): void
{
$traceId = $span->getContext()->getTraceId();
$traceIdJson = json_encode([
'traceId' => '1-' . substr($traceId, 0, 8) . '-' . substr($traceId, 8),
]);
echo "Final trace ID: $traceIdJson\n";
}
27 changes: 15 additions & 12 deletions examples/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is a getting started guide for the example applications found in the AWS co

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.

### Sample App 1
### Aws Client App

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.

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

### Run Collector

Open a new terminal window and navigate into the aws-otel-collector folder.

Run the following command. Make sure to replace `YOUR_ACCESS_KEY_HERE` and `YOUR_SECRET_ACCESS_KEY_HERE` with your own specific keys.

```console
docker run --rm -p 4317:4317 -p 55681:55681 -p 8889:8888 \
-e AWS_REGION=us-west-2 \
-e AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_HERE \
-e AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY_HERE \
-v "${PWD}/examples/docker/config-test.yaml":/otel-local-config.yaml \
-v "${PWD}/examples/aws/collector/config.yaml":/otel-local-config.yaml \
--name awscollector public.ecr.aws/aws-observability/aws-otel-collector:latest \
--config otel-local-config.yaml
```

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

Run the following command for Sample App 1:
To run `AwsClientApp`, navigate to `examples/aws/AwsClientApp`, then install required dependencies:

```
composer install
```

And run the following command:

`php examples/aws/SampleApp1/SampleApp1.php`
`php bin/app`

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

```console
Starting Sample App
Which call would you like to make?
Starting Aws Client App

Which call would you like to make?
Type outgoing-http-call or aws-sdk-call
outgoing-http-call
Final trace ID: {"traceId":"1-6115648a-d40b50a270b3c1249bcf60c2"}
Sample App complete!
Final trace ID: {"traceId":"1-622fb9fb-1b2031fcde9ac72610b6a0b9"}
Aws Client App complete!
```

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.

Run the following command for Sample App 2:

`php examples/aws/SampleApp2/SampleApp2.php`
Expand Down
29 changes: 29 additions & 0 deletions examples/aws/collector/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317

processors:
batch:

exporters:
logging:
loglevel: debug
awsxray:
region: 'us-west-2'
awsemf:
region: 'us-west-2'

service:
pipelines:
traces:
receivers: [otlp]
exporters: [awsxray]
metrics:
receivers: [otlp]
exporters: [awsemf]

telemetry:
logs:
level: debug
Loading

0 comments on commit ffda596

Please sign in to comment.