-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mtk3d/fix/example-app1
WIP: Fix first AWS example app
- Loading branch information
Showing
6 changed files
with
207 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "../../../" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.