|
| 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 | +} |
0 commit comments