Skip to content

Commit

Permalink
Merge pull request #54 from mtk3d/fix/aws-example-services
Browse files Browse the repository at this point in the history
Specify AWS attributes to show exact services in XRay
  • Loading branch information
bobstrecansky authored Mar 17, 2022
2 parents ffda596 + 6bf6252 commit dbc5850
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions examples/aws/AwsClientApp/src/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@
$propagator = new Propagator();

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

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

$carrier = [];

if ($line === 'outgoing-http-call') {
Expand All @@ -74,7 +70,13 @@

try {
$client = new Client();
$client->request('GET', 'https://aws.amazon.com', ['headers' => $carrier, 'timeout' => 2000,]);
$result = $client->request('GET', 'https://aws.amazon.com', ['headers' => $carrier]);

$span->setAttributes([
'http.method' => 'GET',
'http.url' => 'https://aws.amazon.com',
'http.status_code' => $result->getStatusCode(),
]);
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
Expand All @@ -86,9 +88,14 @@
}

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

$propagator->inject($carrier);
$span->setAttributes([
'rpc.method' => 'CreateBucket',
'rpc.service' => 'S3',
'rpc.system' => 'aws-api',
]);

$s3Client = new S3Client([
'profile' => 'default',
Expand All @@ -98,18 +105,30 @@

try {
$result = $s3Client->createBucket([
'Bucket' => uniqid('test-bucket-'),
'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()}";
exit(1);
}

$span->end();

$span = $tracer->spanBuilder('S3.ListBuckets')->setSpanKind(SpanKind::KIND_CLIENT)->startSpan();
$span->activate();

$span->setAttributes([
'rpc.method' => 'ListBuckets',
'rpc.service' => 'S3',
'rpc.system' => 'aws-api',
]);

$buckets = $s3Client->listBuckets();

foreach ($buckets['Buckets'] as $bucket) {
Expand Down

0 comments on commit dbc5850

Please sign in to comment.