|
| 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 Examples\Aws\SampleApp1; |
| 21 | + |
| 22 | +require __DIR__ . '/../../../vendor/autoload.php'; |
| 23 | + |
| 24 | +use GuzzleHttp\Client; |
| 25 | +use Instrumentation\Aws\Xray\AwsXrayIdGenerator; |
| 26 | +use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter; |
| 27 | +use OpenTelemetry\Sdk\Trace\PropagationMap; |
| 28 | +use OpenTelemetry\Sdk\Trace\SpanProcessor\SimpleSpanProcessor; |
| 29 | +use OpenTelemetry\Sdk\Trace\TracerProvider; |
| 30 | +use OpenTelemetry\Trace as API; |
| 31 | +use Propagators\Aws\Xray\AwsXrayPropagator; |
| 32 | + |
| 33 | +// use Aws\S3\S3Client; |
| 34 | +// use Aws\Exception\AwsException; |
| 35 | + |
| 36 | +/** |
| 37 | + * This is a sample app that makes an http request |
| 38 | + * to aws.amazon.com or a call to AWS s3 buckets |
| 39 | + * It uses the OTEL GRPC Exporter |
| 40 | + * Sends traces to the aws-otel-collector |
| 41 | + * It will generate one trace that has a child span and uses the |
| 42 | + * AWS X-Ray propagator to inject the context into the carrier. |
| 43 | + */ |
| 44 | + |
| 45 | +/** |
| 46 | + * To use aws-sdk-call: |
| 47 | + * Comment line 110 |
| 48 | + * Please downgrade guzzle from "^2.0@RC" to "^1.8.2" in composer.json |
| 49 | + * Then run composer require aws/aws-sdk-php |
| 50 | + * Run composer update to make sure everything is up to date |
| 51 | + * Uncomment lines 32-33, 111-134, 151-165 |
| 52 | + */ |
| 53 | + |
| 54 | +echo 'Starting Sample App' . PHP_EOL; |
| 55 | + |
| 56 | +// Prompts the user to choose which type of request to make |
| 57 | +echo 'Which call would you like to make? ' . PHP_EOL; |
| 58 | +echo 'Type outgoing-http-call or aws-sdk-call' . PHP_EOL; |
| 59 | +$handle = fopen('php://stdin', 'r'); |
| 60 | +$line = trim(fgets($handle)); |
| 61 | +if ($line !== 'outgoing-http-call' && $line !== 'aws-sdk-call') { |
| 62 | + echo "ABORTING!\n"; |
| 63 | + exit; |
| 64 | +} |
| 65 | +fclose($handle); |
| 66 | + |
| 67 | +// Create an exporter for exporting traces |
| 68 | +// Create a client for making http requests |
| 69 | +$Exporter = new OTLPExporter(); |
| 70 | +$client = new Client(); |
| 71 | + |
| 72 | +// Create a tracer object that uses the AWS X-Ray ID Generator to |
| 73 | +// generate trace Ids in the correct format |
| 74 | +$tracer = (new TracerProvider(null, null, new AwsXrayIdGenerator())) |
| 75 | + ->addSpanProcessor(new SimpleSpanProcessor($Exporter)) |
| 76 | + ->getTracer('io.opentelemetry.contrib.php'); |
| 77 | + |
| 78 | +// Create a span with the tracer |
| 79 | +$span = $tracer->startAndActivateSpan('session.generate.span.' . microtime(true)); |
| 80 | + |
| 81 | +// Add some dummy attributes to the parent span (also the root span) |
| 82 | +$span->setAttribute('item_A', 'cars') |
| 83 | +->setAttribute('item_B', 'motorcycles') |
| 84 | +->setAttribute('item_C', 'planes'); |
| 85 | + |
| 86 | +// Create a carrier and map to inject the context of the child span into the carrier |
| 87 | +$carrier = []; |
| 88 | +$map = new PropagationMap(); |
| 89 | + |
| 90 | +if ($line === 'outgoing-http-call') { |
| 91 | + // Create a child span for http call |
| 92 | + // Make an HTTP request to take some time up |
| 93 | + // Carrier is injected into the header to simulate a microservice needing the carrier |
| 94 | + $childSpan = $tracer->startAndActivateSpan('session.generate.http.span.' . microtime(true), API\SpanKind::KIND_CLIENT); |
| 95 | + AwsXrayPropagator::inject($childSpan->getContext(), $carrier, $map); |
| 96 | + |
| 97 | + try { |
| 98 | + $res = $client->request('GET', 'https://aws.amazon.com', ['headers' => $carrier, 'timeout' => 2000,]); |
| 99 | + } catch (\Throwable $e) { |
| 100 | + echo $e->getMessage() . PHP_EOL; |
| 101 | + |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + // Format and output the trace Id of the childspan |
| 106 | + getTraceId($childSpan); |
| 107 | + |
| 108 | + $childSpan->end(); |
| 109 | +} else { |
| 110 | + echo 'The desired function is currently unavailable'; |
| 111 | + // // Create a child span for sdk call |
| 112 | + // $childSpan = $tracer->startAndActivateSpan('session.generate.aws.sdk.span.' . microtime(true), API\SpanKind::KIND_CLIENT); |
| 113 | + // AwsXrayPropagator::inject($childSpan->getContext(), $carrier, $map); |
| 114 | + |
| 115 | + // // Make a call to aws s3 buckets |
| 116 | + // $s3Client = new S3Client([ |
| 117 | + // 'profile' => 'default', |
| 118 | + // 'region' => 'us-west-2', |
| 119 | + // 'version' => '2006-03-01' |
| 120 | + // ]); |
| 121 | + |
| 122 | + // // To create a new bucket uncomment this line |
| 123 | + // // createBucket($s3Client, 'testBucket'); |
| 124 | + |
| 125 | + // $buckets = $s3Client->listBuckets(); |
| 126 | + |
| 127 | + // foreach ($buckets['Buckets'] as $bucket) { |
| 128 | + // echo $bucket['Name'] . "\n"; |
| 129 | + // } |
| 130 | + |
| 131 | + // // Format and output the trace Id of the childspan |
| 132 | + // getTraceId($childSpan); |
| 133 | + |
| 134 | + // $childSpan->end(); |
| 135 | +} |
| 136 | + |
| 137 | +// End both the root span to be able to export the trace |
| 138 | +$span->end(); |
| 139 | + |
| 140 | +echo PHP_EOL . 'Sample App complete!'; |
| 141 | +echo PHP_EOL; |
| 142 | + |
| 143 | +// Get a traceId from a span and prints it |
| 144 | +function getTraceId($span) |
| 145 | +{ |
| 146 | + $traceId = $span->getContext()->getTraceId(); |
| 147 | + $xrayTraceId = '1-' . substr($traceId, 0, 8) . '-' . substr($traceId, 8); |
| 148 | + echo 'Final trace ID: ' . json_encode(['traceId' => $xrayTraceId]); |
| 149 | +} |
| 150 | + |
| 151 | +// // Function for creating s3 buckets |
| 152 | +// function createBucket($s3Client, $bucketName) |
| 153 | +// { |
| 154 | +// try { |
| 155 | +// $result = $s3Client->createBucket([ |
| 156 | +// 'Bucket' => $bucketName, |
| 157 | +// ]); |
| 158 | +// return 'The bucket\'s location is: ' . |
| 159 | +// $result['Location'] . '. ' . |
| 160 | +// 'The bucket\'s effective URI is: ' . |
| 161 | +// $result['@metadata']['effectiveUri']; |
| 162 | +// } catch (AwsException $e) { |
| 163 | +// return 'Error: ' . $e->getAwsErrorMessage(); |
| 164 | +// } |
| 165 | +// } |
0 commit comments