DEV Community

Cover image for Using OpenTelemetry with PHP for End-to-End Tracing
Patoliya Infotech
Patoliya Infotech

Posted on

Using OpenTelemetry with PHP for End-to-End Tracing

Delivered systems are like puzzles. A single user request PHP Backend → API → Micro Services → Databases → External Services can hop before completing.

Now, when something breaks (or just slows down), where do you start debugging? Log? Matrix? Intestinal feeling?

👉 Enter Opentelemetry (Otel) : Open standard which helps developers to detect a request in every hop, every service, every database call end-to-end.

And yes, PHP Devs, it is also for us.

Why Tracing Matters (A Real-World Pain Point)

Imagine that you are running an e-commerce site. A customer clicks on "checkout".

Here’s what happens:

  • Request hits your PHP backend
  • Calls the inventory service (Go)
  • Talks to payment gateway (Java)
  • Hits the shipping API
  • Finally responds to the customer

The customer complains: “Checkout is too slow.”

Without tracing:

  • You check PHP logs → nothing odd
  • You ping payment team → “Not us”
  • You escalate → time wasted

With tracing:

  • You open your tracing UI
  • See 2.6s delay at the payment gateway API
  • Boom. Found the culprit in seconds.

This is why tracing is not "nice-to-have". It exists in distributed systems.

Setting Up OpenTelemetry in PHP

Alright, enough storytelling. Let’s see how we can actually use OTel in PHP.

1. Install the SDK

composer require open-telemetry/sdk
composer require open-telemetry/auto-instrumentation
Enter fullscreen mode Exit fullscreen mode

2. Configure an Exporter

Let’s send traces to Jaeger (one of the most popular backends).

use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;

$exporter = new JaegerExporter(
    'php-service',
    'http://localhost:14268/api/traces'
);

$tracerProvider = new TracerProvider($exporter);
$tracer = $tracerProvider->getTracer('php-app');
Enter fullscreen mode Exit fullscreen mode

3. Add Spans to Your Code

$span = $tracer->spanBuilder('checkout-process')->startSpan();
$scope = $span->activate();

callInventoryService();
callPaymentService();

$scope->detach();
$span->end();
Enter fullscreen mode Exit fullscreen mode

Now your checkout flow is traced end-to-end.

PHP is still highly relevant for building scalable, dynamic apps—see why PHP is a top choice for e-commerce development

Auto-Instrumentation: Tracing Without Effort

Let's be real - in any way nobody wants to add spans everywhere.

That’s why auto-instrumentation exists. Along with that, the autel traces automatically:

  • HTTP requests
  • Database queries (PDO, MySQLi)
  • Framework events (Laravel, Symfony, Slim)

Just enable it:

otel-php-auto-instrumentation enable

Enter fullscreen mode Exit fullscreen mode

Sit back. Watch magic happen.

Real End-to-End Tracing Across Services

Here’s how it looks in a real-world stack:

  • Frontend (React/Next.js) → sends trace headers
  • PHP Backend → continues trace
  • Go Inventory Service → continues trace
  • Java Payment Service → continues trace
  • All data → OTel Collector → exported to Jaeger/Grafana Tempo

Now every request has a trace ID connecting the dots across your ecosystem.

That’s end-to-end observability.

Best Practices for PHP Devs

  • Always propagate trace context → pass headers across all services
  • Use auto-instrumentation first → add manual spans only where it matters
  • Run everything through OTel Collector → flexibility + Supplier Nutrality
  • Don’t trace everything in prod → sampling saves costs & storage

Why PHP Devs Should Care About OTel

  • Future-proof your observability: Autel is becoming the standard across all languages and platforms
  • No vendor lock-in: switch between Jaeger, Zipkin, Tempo, Honeycomb, Datadog with no code changes
  • Debug faster, build smarter: spend less time guessing, more time fixing

If your PHP app talks to other services (and let’s be honest, most apps do), tracing is no longer optional—it’s essential.

Learn more about PHP & It's Trending Frameworks

Conclusion

Debugging complex PHP apps with just logs is like flying blind.

With OpenTelemetry in PHP, you get:

  • End-to-end tracing
  • Faster debugging
  • Standardized observability across your stack

So the next time your API feels slow—don’t guess. Trace it.

Top comments (0)