Skip to content

Commit 7e47931

Browse files
committed
Add two AWS sample apps
1 parent 2961aad commit 7e47931

File tree

8 files changed

+411
-2
lines changed

8 files changed

+411
-2
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@
2525
"detectors/",
2626
"exporters/",
2727
"instrumentation/",
28-
"propagators/"
28+
"propagators/",
29+
"examples/"
2930
]
3031
},
3132
"autoload-dev": {
3233
"classmap": [
3334
"detectors/",
3435
"exporters/",
3536
"instrumentation/",
36-
"propagators/"
37+
"propagators/",
38+
"examples/"
3739
]
3840
},
3941
"require-dev": {
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
// }
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\SampleApp2;
21+
22+
require __DIR__ . '/../../../vendor/autoload.php';
23+
24+
use Instrumentation\Aws\Xray\AwsXrayIdGenerator;
25+
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter;
26+
use OpenTelemetry\Sdk\Trace\PropagationMap;
27+
use OpenTelemetry\Sdk\Trace\SpanProcessor\SimpleSpanProcessor;
28+
use OpenTelemetry\Sdk\Trace\TracerProvider;
29+
use Propagators\Aws\Xray\AwsXrayPropagator;
30+
31+
/**
32+
* This is a sample app that makes an http request to aws.amazon.com
33+
* It uses the OTEL GRPC Exporter
34+
* Sends traces to the aws-otel-collector
35+
* It will generate one trace that has a two child spans and uses the
36+
* AWS X-Ray propagator to inject the context into the carrier.
37+
*/
38+
39+
echo 'Starting Sample App' . PHP_EOL;
40+
41+
// Initialize an exporter for exporting traces
42+
// Initialize a map and carrier to use for propagation injection and extraction
43+
$Exporter = new OTLPExporter();
44+
$map = new PropagationMap();
45+
$carrier = [];
46+
47+
// Create a tracer object that uses the AWS X-Ray ID Generator to
48+
// generate trace IDs in the correct format
49+
$tracer = (new TracerProvider(null, null, new AwsXrayIdGenerator()))
50+
->addSpanProcessor(new SimpleSpanProcessor($Exporter))
51+
->getTracer('io.opentelemetry.contrib.php');
52+
53+
// Create a span (also the root span) with the tracer
54+
$span = $tracer->startAndActivateSpan('session.generate.span.' . microtime(true));
55+
56+
// Add some dummy attributes to the parent span
57+
$span->setAttribute('item_A', 'cars')
58+
->setAttribute('item_B', 'motorcycles')
59+
->setAttribute('item_C', 'planes');
60+
61+
// Inject the context of the child span into the carrier to pass to the first service1
62+
// The tracer is passed to each service for convinience of not creating another tracer
63+
// in the service.
64+
65+
// TODO: The next step for testing propagation would be to create two separate
66+
// web application, each making a request from a client front end.
67+
AwsXrayPropagator::inject($span->getContext(), $carrier, $map);
68+
$service1 = new Service1($carrier);
69+
$childSpanContext = $service1->useService();
70+
71+
// Inject the context of the child span into the carrier to pass to the first service2
72+
AwsXrayPropagator::inject($childSpanContext, $carrier, $map);
73+
$service2 = new Service2($carrier);
74+
$childSpanContext2 = $service2->useService();
75+
76+
// Format and output the trace ID of the a child span
77+
$traceId = $childSpanContext2->getTraceId();
78+
$xrayTraceId = '1-' . substr($traceId, 0, 8) . '-' . substr($traceId, 8);
79+
echo 'Child span trace ID after service 2: ' . json_encode(['traceId' => $xrayTraceId]);
80+
81+
// End the parent span
82+
$span->end();
83+
84+
echo PHP_EOL . 'Sample App complete!';
85+
echo PHP_EOL;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\SampleApp2;
21+
22+
require __DIR__ . '/../../../vendor/autoload.php';
23+
24+
use Instrumentation\Aws\Xray\AwsXrayIdGenerator;
25+
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter;
26+
use OpenTelemetry\Sdk\Trace\PropagationMap;
27+
use OpenTelemetry\Sdk\Trace\SpanProcessor\SimpleSpanProcessor;
28+
use OpenTelemetry\Sdk\Trace\TracerProvider;
29+
use OpenTelemetry\Trace as API;
30+
use Propagators\Aws\Xray\AwsXrayPropagator;
31+
32+
class Service1
33+
{
34+
private const LIMIT = 100;
35+
36+
private $tracer;
37+
private $carrier;
38+
39+
public function __construct(array $carrier)
40+
{
41+
$this->carrier = $carrier;
42+
}
43+
44+
public function useService()
45+
{
46+
$Exporter = new OTLPExporter();
47+
$map = new PropagationMap();
48+
49+
// Create a tracer object that uses the AWS X-Ray ID Generator to
50+
// generate trace IDs in the correct format
51+
$tracer = (new TracerProvider(null, null, new AwsXrayIdGenerator()))
52+
->addSpanProcessor(new SimpleSpanProcessor($Exporter))
53+
->getTracer('io.opentelemetry.contrib.php');
54+
55+
// Extract the SpanContext from the carrier
56+
$spanContext = AwsXrayPropagator::extract($this->carrier, $map);
57+
58+
// Do some kind of operation
59+
$i = 0;
60+
while ($i < self::LIMIT) {
61+
$i++;
62+
}
63+
64+
// Create a child span
65+
$childSpan = $tracer->startActiveSpan('session.second.child.span' . microtime(true), $spanContext, false, API\SpanKind::KIND_CLIENT);
66+
67+
// Set some dummy attributes
68+
$childSpan->setAttribute('service_2', 'microservice')
69+
->setAttribute('action_item', (string) $i);
70+
71+
// End child span
72+
$childSpan->end();
73+
74+
return $childSpan->getContext();
75+
}
76+
}

0 commit comments

Comments
 (0)