-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding PHP documentation for exporters, resources, propagation (#2415)
- Loading branch information
Showing
6 changed files
with
322 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
--- | ||
title: Exporters | ||
weight: 5 | ||
--- | ||
|
||
In order to visualize and analyze your telemetry, you will need to export it to | ||
a backend. OpenTelemetry PHP provides exporters for some common open source | ||
backends. | ||
|
||
## OTLP | ||
|
||
To send trace data to a OTLP endpoint (like the [collector](/docs/collector) or | ||
Jaeger) you'll need to use the `open-telemetry/exporter-otlp` package: | ||
|
||
```shell | ||
composer require open-telemetry/exporter-otlp | ||
``` | ||
|
||
If you use gRPC, you will also need to install the | ||
`open-telemetry/transport-grpc` package: | ||
|
||
```shell | ||
composer require open-telemetry/transport-grpc | ||
``` | ||
|
||
Next, configure the exporter with an OTLP endpoint. For example, you can update | ||
`GettingStarted.php` from [Getting Started](../getting-started/) like the | ||
following: | ||
|
||
<!-- prettier-ignore-start --> | ||
{{< tabpane >}} | ||
{{< tab gRPC >}} | ||
use OpenTelemetry\API\Common\Signal\Signals; | ||
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory; | ||
use OpenTelemetry\Contrib\Otlp\OtlpUtil; | ||
use OpenTelemetry\Contrib\Otlp\SpanExporter; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
|
||
$transport = (new GrpcTransportFactory())->create('http://collector:4317' . OtlpUtil::method(Signals::TRACE)); | ||
$exporter = new SpanExporter($transport); | ||
{{< /tab >}} | ||
{{< tab protobuf >}} | ||
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory; | ||
use OpenTelemetry\Contrib\Otlp\SpanExporter; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
|
||
$transport = (new OtlpHttpTransportFactory())->create('http://collector:4318/v1/traces', 'application/x-protobuf'); | ||
$exporter = new SpanExporter($transport); | ||
{{< /tab>}} | ||
{{< tab json >}} | ||
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory; | ||
use OpenTelemetry\Contrib\Otlp\SpanExporter; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
|
||
$transport = (new OtlpHttpTransportFactory())->create('http://collector:4318/v1/traces', 'application/json'); | ||
$exporter = new SpanExporter($transport); | ||
{{< /tab >}} | ||
{{< tab nd-json >}} | ||
/* newline-delimited JSON */ | ||
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory; | ||
use OpenTelemetry\Contrib\Otlp\SpanExporter; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
|
||
$transport = (new OtlpHttpTransportFactory())->create('http://collector:4318/v1/traces', 'application/x-ndjson'); | ||
$exporter = new SpanExporter($transport); | ||
{{< /tab >}} | ||
{{< /tabpane >}} | ||
<!-- prettier-ignore-end --> | ||
|
||
Then, register the exporter in a tracer provider: | ||
|
||
```php | ||
$tracerProvider = new TracerProvider( | ||
new SimpleSpanProcessor($exporter) | ||
); | ||
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); | ||
``` | ||
|
||
To try out the example locally, you can run | ||
[Jaeger](https://www.jaegertracing.io/) in a docker container: | ||
|
||
```shell | ||
docker run -d --name jaeger \ | ||
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \ | ||
-e COLLECTOR_OTLP_ENABLED=true \ | ||
-p 6831:6831/udp \ | ||
-p 6832:6832/udp \ | ||
-p 5778:5778 \ | ||
-p 16686:16686 \ | ||
-p 4317:4317 \ | ||
-p 4318:4318 \ | ||
-p 14250:14250 \ | ||
-p 14268:14268 \ | ||
-p 14269:14269 \ | ||
-p 9411:9411 \ | ||
jaegertracing/all-in-one:latest | ||
``` | ||
|
||
## Zipkin | ||
|
||
If you're using [Zipkin](https://zipkin.io/) to visualize traces, you'll need to | ||
set it up first. Here's how to run it locally in a docker container. | ||
|
||
```shell | ||
docker run --rm -d -p 9411:9411 --name zipkin openzipkin/zipkin | ||
``` | ||
|
||
Install the exporter package as a dependency for your application: | ||
|
||
```shell | ||
composer require open-telemetry/exporter-zipkin | ||
``` | ||
|
||
Update the example to use the Zipkin exporter and to send data to your zipkin | ||
backend: | ||
|
||
```php | ||
$transport = PsrTransportFactory::discover()->create('http://zipkin:9411/api/v2/spans', 'application/json'); | ||
$zipkinExporter = new ZipkinExporter($transport); | ||
$tracerProvider = new TracerProvider( | ||
new SimpleSpanProcessor($zipkinExporter) | ||
); | ||
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
title: Propagation | ||
description: Context propagation for the PHP API | ||
weight: 7 | ||
--- | ||
|
||
Propagation is the mechanism that moves data between services and processes. | ||
Although not limited to tracing, it is what allows traces to build causal | ||
information about a system across services that are arbitrarily distributed | ||
across process and network boundaries. | ||
|
||
OpenTelemetry provides a text-based approach to propagate context to remote | ||
services using the [W3C Trace Context](https://www.w3.org/TR/trace-context/) | ||
HTTP headers. | ||
|
||
## Context propagation with frameworks and libraries | ||
|
||
Auto-instrumentation exists for some popular PHP frameworks (eg Symfony, | ||
Laravel, Slim) and HTTP libraries propagate context for incoming and outgoing | ||
HTTP requests. | ||
|
||
**We highly recommend that you use auto-instrumentation or instrumentation | ||
libraries to propagate context**. Although it is possible to propagate context | ||
manually, the PHP auto-instrumentation and instrumentation libraries are | ||
well-tested and easier to use. | ||
|
||
### Incoming | ||
|
||
Auto-instrumentation for frameworks which implement the | ||
[PSR-15](https://www.php-fig.org/psr/psr-15/) `RequestHandlerInterface` will | ||
automatically extract W3C tracecontext headers, create a root span, and set a | ||
remote parent for the root span. | ||
|
||
```shell | ||
composer require open-telemetry/opentelemetry-auto-psr15 | ||
``` | ||
|
||
### Outgoing | ||
|
||
[PSR-18](https://www.php-fig.org/psr/psr-18/) auto-instrumentation will | ||
automatically apply W3C tracecontext headers to outgoing HTTP requests for any | ||
library which implements the PSR-18 interface. | ||
|
||
```shell | ||
open-telemetry/opentelemetry-auto-psr18 | ||
``` | ||
|
||
## Manual W3C Trace Context Propagation | ||
|
||
In some cases, it is not possible to propagate context with an instrumentation | ||
library. There may not be an instrumentation library that matches a library | ||
you're using to have services communicate with one another. Or you many have | ||
requirements that instrumentation libraries cannot fulfill, even if they exist. | ||
|
||
When you must propagate context manually, you can use the context api. | ||
|
||
The following presents an example of an outgoing HTTP request: | ||
|
||
```php | ||
$request = new Request('GET', 'http://localhost:8080/resource'); | ||
$outgoing = $tracer->spanBuilder('/resource')->setSpanKind(SpanKind::CLIENT)->startSpan(); | ||
$outgoing->setAttribute(TraceAttributes::HTTP_METHOD, $request->getMethod()); | ||
$outgoing->setAttribute(TraceAttributes::HTTP_URL, (string) $request->getUri()); | ||
|
||
$carrier = []; | ||
TraceContextPropagator::getInstance()->inject($carrier); | ||
foreach ($carrier as $name => $value) { | ||
$request = $request->withAddedHeader($name, $value); | ||
} | ||
try { | ||
$response = $client->send($request); | ||
} finally { | ||
$outgoing->end(); | ||
} | ||
``` | ||
|
||
Similarly, the text-based approach can be used to read the W3C Trace Context | ||
from incoming requests. The following presents an example of processing an | ||
incoming HTTP request: | ||
|
||
```php | ||
$request = ServerRequestCreator::createFromGlobals(); | ||
$context = TraceContextPropagator::getInstance()->extract($request->getHeaders()); | ||
$root = $tracer->spanBuilder('HTTP ' . $request->getMethod()) | ||
->setStartTimestamp((int) ($request->getServerParams()['REQUEST_TIME_FLOAT'] * 1e9)) | ||
->setParent($context) | ||
->setSpanKind(SpanKind::KIND_SERVER) | ||
->startSpan(); | ||
$scope = $root->activate(); | ||
try { | ||
/* do stuff */ | ||
} finally { | ||
$root->end(); | ||
$scope->detach(); | ||
} | ||
``` |
Oops, something went wrong.