Skip to content
Search

Last updated

Send PHP traces over OTLP: endpoint, ports, HTTP or gRPC

Let's say our nightly import already creates spans, and they are printed to the console. Now we want them in a real tool, with a timeline, where we can look at last night's run.

These scripts need a collector, so they run only on your machine, not in the playground.

OTLP ports 4317 and 4318

OTLP is the protocol of OpenTelemetry. Every collector and every monitoring tool that supports OpenTelemetry accepts it, in two flavors:

Flavor Default port Address we give In PHP it needs
OTLP/HTTP 4318 http://localhost:4318 the curl extension
OTLP/gRPC 4317 localhost:4317 the grpc extension

Over HTTP every kind of data has its own path: /v1/traces, /v1/metrics and /v1/logs. We give only the address, and the transport adds the path.

Send traces from PHP over OTLP/HTTP

flow-php/telemetry is an independent implementation of OpenTelemetry, written in PHP. It needs PHP 8.3 or newer and the curl extension, and it is not a wrapper around the official SDK.

composer require flow-php/telemetry flow-php/telemetry-otlp-bridge flow-php/etl
<?php

declare(strict_types=1);

use function Flow\Bridge\Telemetry\OTLP\DSL\{otlp_curl_options, otlp_curl_transport, otlp_exporter};
use function Flow\ETL\DSL\clock;
use function Flow\Telemetry\DSL\{
    batching_span_processor,
    memory_context_storage,
    resource,
    telemetry,
    tracer_provider,
};

require __DIR__ . '/vendor/autoload.php';

$exporter = otlp_exporter(otlp_curl_transport(
    endpoint: 'http://localhost:4318',
    options: otlp_curl_options()->withTimeout(2000)->withConnectTimeout(500),
));

$telemetry = telemetry(
    resource(['service.name' => 'nightly-import']),
    tracer_provider(batching_span_processor($exporter), clock(), memory_context_storage()),
)->registerShutdownFunction();

$tracer = $telemetry->tracer('import');

$tracer->trace('import-orders', static function () use ($tracer): void {
    $tracer->trace('download', static fn () => usleep(120_000));
    $tracer->trace('save', static fn () => usleep(250_000));
});

echo "import finished\n";
import finished

The script prints nothing else, and the collector has one trace named import-orders with three spans, 0.38s long.

Three lines do the work:

  • otlp_curl_transport sends the data as JSON. No protobuf package is needed.
  • batching_span_processor keeps the spans and sends them together, 512 at a time. Three spans are one request, not three.
  • registerShutdownFunction sends what is left when the script ends. Without it the last spans stay in memory and are lost.

The wrong OTLP port

The mistake everybody makes once is the gRPC port with the HTTP transport.

$exporter = otlp_exporter(otlp_curl_transport(endpoint: 'http://localhost:4317'));
import finished
[flow-telemetry] Flow\Bridge\Telemetry\OTLP\Transport\TransportException: curl error 1 (Unsupported protocol) for http://localhost:4317/v1/traces; elapsed connect=0ms total=10ms (configured connect_timeout=500ms, timeout=2000ms)

Unsupported protocol on port 4317 means HTTP was sent to the gRPC port. Change the port to 4318.

What happens when the collector is down

A monitoring tool must never break the thing it monitors. Let's stop the collector and run the import again.

import finished
[flow-telemetry] Flow\Bridge\Telemetry\OTLP\Transport\TransportException: curl error 7 (Could not connect to server) for http://localhost:4318/v1/traces; elapsed connect=0ms total=1ms (configured connect_timeout=500ms, timeout=2000ms)

The import finished. The spans of this run are lost, and the reason is in the PHP error log, with the address and the timeouts.

The two timeouts are the most a failed export can cost: half a second to connect, two seconds for the whole request. They are in milliseconds. With a collector on the same machine they can be much smaller.

Send traces to a monitoring service

A hosted service gives us an address and a token. Both go into the transport:

$exporter = otlp_exporter(otlp_curl_transport(
    endpoint: 'https://otlp.example.com:4318',
    options: otlp_curl_options()
        ->withHeader('Authorization', 'Bearer ' . $_ENV['OTLP_TOKEN'])
        ->withCompression(),
));

OTLP over HTTP or gRPC in PHP

Use HTTP. It needs only the curl extension, which almost every PHP has, and JSON can be read with any tool when something goes wrong.

gRPC needs the grpc extension, which has to be compiled or installed for every PHP version on every server. Take it when the collector accepts nothing else:

use function Flow\Bridge\Telemetry\OTLP\DSL\otlp_grpc_transport;

$exporter = otlp_exporter(otlp_grpc_transport(endpoint: 'localhost:4317'));

The rest of the script doesn't change. The gRPC address has no http:// in front.

Where to go from here

The spans themselves, with attributes and errors, are in the guide about tracing in PHP. Metrics go the same way, with /v1/metrics as the path: see PHP metrics with OpenTelemetry. All the options of the transports are in the OTLP bridge documentation.

Found a typo or an outdated section? Edit this guide on GitHub

scroll back to top