|
| 1 | +--- |
| 2 | +title: 'Tracing with the AWS Distro for OpenTelemetry PHP SDK and X-Ray' |
| 3 | +description: Learn how to get started with PHP SDK for adding tracing to applications and libraries. |
| 4 | +path: '/docs/getting-started/php-sdk/trace-manual-instr' |
| 5 | +--- |
| 6 | + |
| 7 | +import SectionSeparator from "components/MdxSectionSeparator/sectionSeparator.jsx" |
| 8 | +import SubSectionSeparator from "components/MdxSubSectionSeparator/subsectionSeparator.jsx" |
| 9 | + |
| 10 | +<SectionSeparator /> |
| 11 | + |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +This guide covers the components of the AWS Distro for OpenTelemetry (ADOT) PHP, and describes how to configure the relevant ADOT components to send trace data to the AWS X-Ray backend. |
| 16 | + |
| 17 | +For more information on OpenTelemetry PHP, see the [OpenTelemetry Developer Guide for PHP](https://opentelemetry.io/docs/instrumentation/php/) |
| 18 | + |
| 19 | +<SectionSeparator /> |
| 20 | + |
| 21 | +## Requirements |
| 22 | + |
| 23 | +[PHP 7.4 or later](https://www.php.net/downloads.php) is required to run an application using OpenTelemetry |
| 24 | + |
| 25 | +Note: You’ll also need to have the [ADOT Collector](https://aws-otel.github.io/docs/getting-started/collector) running to export traces to X-Ray. |
| 26 | + |
| 27 | +<SectionSeparator /> |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +In order to send traces from your application, the following OpenTelemetry PHP packages must be installed in your application’s root directory |
| 32 | + |
| 33 | + |
| 34 | +* [X-Ray ID Generator](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Aws/Xray) |
| 35 | +* [X-Ray propagator](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Aws/Xray) |
| 36 | + |
| 37 | +```bash |
| 38 | + composer require open-telemetry/contrib-aws |
| 39 | +``` |
| 40 | + |
| 41 | +* [OpenTelemetry API for tracing](https://packagist.org/packages/open-telemetry/api) |
| 42 | + |
| 43 | +```bash |
| 44 | +composer require open-telemetry/api |
| 45 | +``` |
| 46 | + |
| 47 | +* [OpenTelemetry SDK for tracing](https://packagist.org/packages/open-telemetry/sdk) |
| 48 | + |
| 49 | +```bash |
| 50 | +composer require open-telemetry/sdk |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +* [OTLP GRPC exporter](https://packagist.org/packages/open-telemetry/exporter-otlp-grpc) |
| 55 | + |
| 56 | +In order to use this package, you must also install the GRPC package using PECL. See the instructions on the [OpenTelemetry PHP repository](https://github.com/open-telemetry/opentelemetry-php#optional-dependencies) for more information. |
| 57 | + |
| 58 | +```bash |
| 59 | +composer require open-telemetry/exporter-otlp-grpc |
| 60 | +``` |
| 61 | + |
| 62 | +<SectionSeparator /> |
| 63 | + |
| 64 | + |
| 65 | +## Setting up the Global Tracer |
| 66 | + |
| 67 | +### Sending Traces to AWS X-Ray |
| 68 | + |
| 69 | +In order to send trace data to AWS X-Ray, instantiate a new tracer provider and provide it with the X-Ray ID generator, X-Ray propagator, and OTLP exporter pointing to the collector's address. |
| 70 | + |
| 71 | +``` |
| 72 | +// Initialize Span Processor, X-Ray ID generator, Tracer Provider, and Propagator |
| 73 | +
|
| 74 | +$spanProcessor = new SimpleSpanProcessor(new OTLPExporter()); |
| 75 | +$idGenerator = new IdGenerator(); |
| 76 | +$tracerProvider = new TracerProvider($spanProcessor, null, null, null, $idGenerator); |
| 77 | +$propagator = new Propagator(); |
| 78 | +$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); |
| 79 | +``` |
| 80 | + |
| 81 | +<SubSectionSeparator /> |
| 82 | + |
| 83 | +### Using the AWS resource detectors |
| 84 | + |
| 85 | +The AWS resource detectors are included with the X-Ray ID generator and X-Ray propagator in the `open-telemetry/contrib-aws` package. |
| 86 | + |
| 87 | +The ADOT PHP SDK supports automatically recording metadata in EC2, Elastic Beanstalk, ECS, and EKS environments. |
| 88 | + |
| 89 | + |
| 90 | +``` |
| 91 | +use GuzzleHttp\Client; |
| 92 | +use GuzzleHttp\Psr7\HttpFactory; |
| 93 | +use OpenTelemetry\Aws\Ec2\Detector; |
| 94 | + |
| 95 | +$client = new Client(); |
| 96 | +$requestFactory = new HttpFactory(); |
| 97 | +
|
| 98 | +$detector = new Detector($client, $requestFactory); |
| 99 | +
|
| 100 | +$tracerProvider = new TracerProvider($spanProcessor, null, $detector->getResource(), null, $idGenerator); |
| 101 | +
|
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +To see what attributes are captured and how to add other resource detectors, see the [OpenTelemetry documentation](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Aws). |
| 106 | + |
| 107 | +<SubSectionSeparator /> |
| 108 | + |
| 109 | +### Debug Logging |
| 110 | + |
| 111 | +To enable debug logging for the OpenTelemetry SDK, create a logger as follows: |
| 112 | + |
| 113 | +``` |
| 114 | +use Monolog\Handler\StreamHandler; |
| 115 | +use Monolog\Logger; |
| 116 | +use OpenTelemetry\SDK\Common\Log\LoggerHolder; |
| 117 | +use Psr\Log\LogLevel; |
| 118 | +
|
| 119 | +LoggerHolder::set( |
| 120 | + new Logger('otel-php', [new StreamHandler(STDOUT, LogLevel::DEBUG)]) |
| 121 | +); |
| 122 | +``` |
| 123 | + |
| 124 | +<SubSectionSeparator /> |
| 125 | + |
| 126 | +## Instrumenting an Application |
| 127 | + |
| 128 | +**Warning: Some instrumentations are not yet stable and the attributes they collect are subject to change until the instrumentation reaches 1.0 stability. It is recommended that you pin a specific version of an instrumentation** |
| 129 | + |
| 130 | +When you instrument a library, every time the library is used, the request is automatically wrapped with a populated span. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +### Instrumenting the AWS SDK |
| 135 | + |
| 136 | +Run the following command to import the AWS SDK Instrumentation: |
| 137 | + |
| 138 | + |
| 139 | +``` |
| 140 | +composer require open-telemetry/opentelemetry-php-contrib:0.0.15 |
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | +Import the `AwsSdkInstrumentation` class in your PHP source code to activate the SDK instrumentation: |
| 145 | + |
| 146 | + |
| 147 | +``` |
| 148 | +use OpenTelemetry\Instrumentation\AwsSdk\AwsSdkInstrumentation; |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +Tracing support for downstream AWS SDK calls to Amazon DynamoDB, S3, and others is provided by the [OpenTelemetry PHP AWS SDK Instrumentation](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Instrumentation/AwsSdk). The example below demonstrates setting up the AWS SDK instrumentation and tracing a call to S3. |
| 153 | + |
| 154 | + |
| 155 | +``` |
| 156 | +use OpenTelemetry\Instrumentation\AwsSdk\AwsSdkInstrumentation; |
| 157 | + |
| 158 | +// Initialize Span Processor, X-Ray ID generator, Tracer Provider, and Propagator |
| 159 | +$spanProcessor = new SimpleSpanProcessor(new OTLPExporter()); |
| 160 | +$xrayIdGenerator = new IdGenerator(); |
| 161 | +$tracerProvider = new TracerProvider($spanProcessor, null, null, null, $xrayIdGenerator); |
| 162 | +$xrayPropagator = new Propagator(); |
| 163 | + |
| 164 | +// Create new instance of AWS SDK Instrumentation class |
| 165 | +$awssdkinstrumentation = new AwsSdkInstrumentation(); |
| 166 | + |
| 167 | +// Configure AWS SDK Instrumentation with Propagator and set Tracer Provider (created above) |
| 168 | +$awssdkinstrumentation->setPropagator($xrayPropagator); |
| 169 | +$awssdkinstrumentation->setTracerProvider($tracerProvider); |
| 170 | + |
| 171 | +// Create and activate root span |
| 172 | +$root = $awssdkinstrumentation->getTracer()->spanBuilder('AwsSDKInstrumentation')->setSpanKind(SpanKind::KIND_SERVER)->startSpan(); |
| 173 | +$rootScope = $root->activate(); |
| 174 | + |
| 175 | +// Initialize all AWS Client instances |
| 176 | +$s3Client = new S3Client([ |
| 177 | + 'region' => 'us-west-2', |
| 178 | +]); |
| 179 | + |
| 180 | +// Pass client instances to AWS SDK |
| 181 | +$awssdkinstrumentation->instrumentClients([$s3Client]); |
| 182 | + |
| 183 | +// Activate Instrumentation -- all AWS Client calls will be automatically instrumented |
| 184 | +$awssdkinstrumentation->activate(); |
| 185 | + |
| 186 | +// Make S3 client call |
| 187 | +$result = $s3Client->listBuckets(); |
| 188 | + |
| 189 | +// End the root span after all the calls to the AWS SDK have been made |
| 190 | +$root->end(); |
| 191 | +$rootScope->detach(); |
| 192 | +``` |
| 193 | + |
| 194 | +<SectionSeparator /> |
| 195 | + |
| 196 | +## Custom Instrumentation |
| 197 | + |
| 198 | +### Creating Custom Spans |
| 199 | + |
| 200 | +Use custom spans to monitor the performance of internal activities that are not captured by instrumentation libraries. Note that only spans of kind `Server` are converted into X-Ray segments. All other spans are converted into X-Ray subsegments. For more on segments and subsegments, see the [AWS X-Ray developer guide](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-segments). |
| 201 | + |
| 202 | + |
| 203 | +``` |
| 204 | +// this span will be translated to a segment in X-Ray backend |
| 205 | +$span = $awssdkinstrumentation->getTracer()->spanBuilder('segment')->setSpanKind(SpanKind::KIND_SERVER)->startSpan(); |
| 206 | + |
| 207 | +// this span will be translated to a subsegment in X-Ray backend |
| 208 | +$span2 = $awssdkinstrumentation->getTracer()->spanBuilder('subsegment')->setSpanKind(SpanKind::KIND_CLIENT)->startSpan(); |
| 209 | +``` |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +### Adding custom attributes |
| 214 | + |
| 215 | +You can also add custom key-value pairs as attributes to your spans. Attributes are converted to metadata by default. If you [configure your collector](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/7bf2266a025425993a233f66c77a0810ab11a78b/exporter/awsxrayexporter#exporter-configuration), you can convert some or all of the attributes to annotations. To read more about X-Ray annotations and metadata see the [AWS X-Ray Developer Guide](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-annotations). |
| 216 | + |
| 217 | + |
| 218 | +``` |
| 219 | +$span = $tracer |
| 220 | + ->spanBuilder('span') |
| 221 | + ->startSpan(); |
| 222 | + |
| 223 | +$spanScope = $root->activate(); |
| 224 | + |
| 225 | +$span->setAttributes(["a" => "1"]); |
| 226 | + |
| 227 | +$span->end(); |
| 228 | +$spanScope->detach(); |
| 229 | +``` |
| 230 | + |
| 231 | +<SectionSeparator /> |
| 232 | + |
| 233 | + |
| 234 | +## Sample Application |
| 235 | + |
| 236 | +See the [AWS Distro for OpenTelemetry Sample Code with PHP SDK](https://github.com/aws-observability/aws-otel-php) for instructions on setting up and using the sample application. |
| 237 | + |
| 238 | + |
0 commit comments