Skip to content

Commit

Permalink
Merge pull request #11 from aws-observability/fix-detach-scope
Browse files Browse the repository at this point in the history
Fix scope detach warning in integration test workflow
  • Loading branch information
carolabadeer authored May 30, 2023
2 parents fb866f6 + e77ad20 commit 6fb140e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .github/collector/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ services:
- LISTEN_ADDRESS=0.0.0.0:8000
- OTEL_EXPORTER_OTLP_INSECURE=True
- OTEL_RESOURCE_ATTRIBUTES=service.name=aws-otel-integ-test
- OTEL_EXPORTER_OTLP_ENDPOINT=otel:4317
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=otel:4317
- OTEL_EXPORTER_OTLP_PROTOCOL=grpc
- OTEL_PHP_TRACES_PROCESSOR=simple
ports:
- '8000:8000'

Expand Down
11 changes: 7 additions & 4 deletions SampleApp/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.234",
"aws/aws-sdk-php-symfony": "^2.5",
"google/protobuf": ">=3.5.0",
"grpc/grpc": "^1.42",
"monolog/monolog": "^2.3",
"open-telemetry/api": "^1.0.0beta3",
"open-telemetry/sdk": "^1.0.0beta3",
"open-telemetry/contrib-aws": "^1.0.0beta3",
"open-telemetry/exporter-otlp" : "^1.0.0beta3",
"open-telemetry/transport-grpc" : "^1.0.0beta3",
"open-telemetry/exporter-otlp": "^1.0.0beta3",
"open-telemetry/sdk": "^1.0.0beta3",
"open-telemetry/transport-grpc": "^1.0.0beta3",
"php-http/guzzle7-adapter": "^1.0",
"php-http/message": "^1.13",
"php-http/message-factory": "^1.1",
"sensio/framework-extra-bundle": "^6.2",
"symfony/console": "6.1.*",
"symfony/dotenv": "6.1.*",
Expand All @@ -24,7 +27,7 @@
"symfony/http-client": "6.1.*",
"symfony/runtime": "6.1.*",
"symfony/yaml": "6.1.*",
"google/protobuf": ">=3.5.0"
"ext-grpc": "*"
},
"config": {
"allow-plugins": {
Expand Down
119 changes: 69 additions & 50 deletions SampleApp/src/Controller/AwsSdkInstrumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;


\OpenTelemetry\API\Common\Log\LoggerHolder::set(new \Monolog\Logger('grpc', [new \Monolog\Handler\StreamHandler('php://stderr')]));

class AwsSdkInstrumentationController
{
Expand Down Expand Up @@ -57,64 +57,75 @@ private function convertOtelTraceIdToXrayFormat(String $otelTraceId) : String
#[Route('/outgoing-http-call')]
public function outgoingHttpCall(): Response
{
// Initialize Span Processor, X-Ray ID generator, Tracer Provider, and Propagator
$transport = (new GrpcTransportFactory())->create('http://127.0.0.1:4317' . OtlpUtil::method(Signals::TRACE));
/*
otel:4317 endpoint corresponds to the collector endpoint in docker-compose
If running this sample app locally, set the endpoint to correspond to the endpoint
of your collector instance.
*/
$transport = (new GrpcTransportFactory())->create('http://otel:4317' . OtlpUtil::method(Signals::TRACE));
$exporter = new SpanExporter($transport);
$spanProcessor = new SimpleSpanProcessor($exporter);

// Initialize Span Processor, X-Ray ID generator, Tracer Provider, and Propagator
$spanProcessor = new SimpleSpanProcessor($exporter);
$idGenerator = new IdGenerator();
$tracerProvider = new TracerProvider($spanProcessor, null, null, null, $idGenerator);
$propagator = new Propagator();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');
$carrier = [];
$traceId = "";

// Create and activate root span
$root = $tracer
->spanBuilder('outgoing-http-call')
->setSpanKind(SpanKind::KIND_CLIENT)
->startSpan();
$rootScope = $root->activate();

$httpSpan = $tracer
->spanBuilder('get-request')
->setSpanKind(SpanKind::KIND_CLIENT)
->startSpan();
$httpScope = $httpSpan->activate();
try {
// Create and activate root span
$root = $tracer
->spanBuilder('outgoing-http-call')
->setSpanKind(SpanKind::KIND_CLIENT)
->startSpan();
$rootScope = $root->activate();

// Make HTTP request
$client = HttpClient::create();
$httpSpan = $tracer
->spanBuilder('get-request')
->setSpanKind(SpanKind::KIND_CLIENT)
->startSpan();
$httpScope = $httpSpan->activate();

$awsHttpUrl = 'https://aws.amazon.com/';
// Make HTTP request
$client = HttpClient::create();

$response = $client->request(
'GET',
$awsHttpUrl
);
$awsHttpUrl = 'https://aws.amazon.com/';

$propagator->inject($carrier);
$response = $client->request(
'GET',
$awsHttpUrl
);

$root->setAttributes([
"http.method" => $this->request->getMethod(),
"http.url" => $this->request->getUri(),
"http.status_code" => $response->getStatusCode()
]);
$propagator->inject($carrier);

$httpSpan->setAttributes([
"http.method" => $this->request->getMethod(),
"http.url" => $awsHttpUrl,
"http.status_code" => $response->getStatusCode()
]);
$root->setAttributes([
"http.method" => $this->request->getMethod(),
"http.url" => $this->request->getUri(),
"http.status_code" => $response->getStatusCode()
]);

$httpSpan->end();
$httpScope->detach();
$httpSpan->setAttributes([
"http.method" => $this->request->getMethod(),
"http.url" => $awsHttpUrl,
"http.status_code" => $response->getStatusCode()
]);

$root->end();
$rootScope->detach();
$traceId = $this->convertOtelTraceIdToXrayFormat(
$root->getContext()->getTraceId()
);
} finally {
$httpSpan->end();
$httpScope->detach();

$traceId = $this->convertOtelTraceIdToXrayFormat(
$root->getContext()->getTraceId()
);
$root->end();
$rootScope->detach();

$tracerProvider->shutdown();
}

return new JsonResponse(
['traceId' => $traceId]
);
Expand All @@ -123,10 +134,15 @@ public function outgoingHttpCall(): Response
#[Route('/aws-sdk-call')]
public function awsSdkCall(): Response
{
// Initialize Span Processor, X-Ray ID generator, Tracer Provider, and Propagator
$transport = (new GrpcTransportFactory())->create('http://127.0.0.1:4317' . OtlpUtil::method(Signals::TRACE));
/*
otel:4317 endpoint corresponds to the collector endpoint in docker-compose
If running this sample app locally, set the endpoint to correspond to the endpoint
of your collector instance.
*/
$transport = (new GrpcTransportFactory())->create('http://otel:4317' . OtlpUtil::method(Signals::TRACE));
$exporter = new SpanExporter($transport);

// Initialize Span Processor, X-Ray ID generator, Tracer Provider, and Propagator
$spanProcessor = new SimpleSpanProcessor($exporter);
$idGenerator = new IdGenerator();
$tracerProvider = new TracerProvider($spanProcessor, null, null, null, $idGenerator);
Expand All @@ -138,6 +154,7 @@ public function awsSdkCall(): Response
// Configure AWS SDK Instrumentation with Propagator and set Tracer Provider (created above)
$awssdkinstrumentation->setPropagator($propagator);
$awssdkinstrumentation->setTracerProvider($tracerProvider);
$traceId = "";

// Create and activate root span
$root = $awssdkinstrumentation
Expand Down Expand Up @@ -174,17 +191,19 @@ public function awsSdkCall(): Response
'http.status_code' => $result['@metadata']['statusCode'],
]);

$traceId = $this->convertOtelTraceIdToXrayFormat(
$root->getContext()->getTraceId()
);

} catch (AwsException $e){
$root->recordException($e);
}
} finally {
// End the root span after all the calls to the AWS SDK have been made
$root->end();
$rootScope->detach();

// End the root span after all the calls to the AWS SDK have been made
$root->end();
$rootScope->detach();

$traceId = $this->convertOtelTraceIdToXrayFormat(
$root->getContext()->getTraceId()
);
$tracerProvider->shutdown();
}

return new JsonResponse(
['traceId' => $traceId]
Expand Down

0 comments on commit 6fb140e

Please sign in to comment.