OpenTelemetry (Tracing and Metrics)

OpenTelemetry is tracing abstraction to collect and aggregate data such as metrics and traces. As OpenTelemetry is tracing abstraction is not force to use specific Tracing 3rd party. Depending on the need and preferences, we may choose provides like DataDog, Jaeger, Zipkin etc.

Ecotone brings full tracing using OpenTelemetry to your PHP applications, which will provide with details about your Message flows in the system, no matter if they run synchronously or asynchronously.

Installation

composer require ecotone/open-telemetry

Configuration with Jaeger

We may use Jaeger for as our 3rd party Tracing System, which will is free open source option.

docker run -p 16686:16686 -p 4317:4317 -e COLLECTOR_OTLP_ENABLED=true jaegertracing/all-in-one:latest

Install GRPC and Protobuf

This is needed to make Tracing insignificant to your Application performance.

# Install PHP Extensions
install-php-extensions grpc protobuf
# Install PHP Packages in your application
composer require open-telemetry/transport-grpc
composer require open-telemetry/exporter-otlp

Add Tracer Provider to Dependency Container

In order to use OpenTelemetry Support we need to add TracerProviderInterface to our Dependency Container.

# config/services.yaml
OpenTelemetry\API\Trace\TracerProviderInterface:
    class: OpenTelemetry\API\Trace\TracerProviderInterface
    factory: ['Ecotone\OpenTelemetry\Support\OTelTracer', 'create']

By default OTelTracer will use localhost:4317 to push the logs, this is the port we've exposed in previous step, as this is where Jaeger exposes Collector.

You may now test integration by checking your Jaeger UI at localhost:16686.

Auto-configuration

OpenTelemetry comes with auto configuration which allows us to to hook in into any method and class without modifying existing code. There are multiple preconfigured packages available, that can hook into PDO, Curl, Laravel or Symfony Requests etc.

To make use of Auto-Instrumentation, we need to provide environment variables, that will be used to automatically configure it. This is needed, because auto-instrumentation may hook even before our Application is booted to provide full insightful details:

OTEL_SERVICE_NAME: 'customer_service'
OTEL_PHP_AUTOLOAD_ENABLED: true
OTEL_TRACES_EXPORTER: otlp
OTEL_METRICS_EXPORTER: otlp
OTEL_LOGS_EXPORTER: otlp
OTEL_EXPORTER_OTLP_PROTOCOL: grpc
OTEL_EXPORTER_OTLP_ENDPOINT: http://localhost:4317
OTEL_PROPAGATORS: baggage,tracecontext

When using environment variables to configure tracing instead of OTelTracer in your Dependency Container, you may use inbuilt Tracer Provider that will create the Tracer based on environments:

# config/services.yaml
OpenTelemetry\API\Trace\TracerProviderInterface:
    class: OpenTelemetry\API\Globals
    factory: [OpenTelemetry\API\Globals, tracerProvider]

Flushing Traces

By default Ecotone will flush traces after Command/Query buses are executed or after handling Message asynchronously. This way traces will be propagated without any extra configuration. However you may want to include bootstrap time for your HTTP Requests, and as Command Bus is executed after your Application is already bootstrapped, this won't be included. So if you want to take over the process or use of auto-configuration for this, then you can disable force flushing in Ecotone:

#[ServiceContext]
public function traceConfiguration()
{
    return TracingConfiguration::createWithDefaults()
              ->withForceFlushOnBusExecution(false)
              ->withForceFlushOnAsynchronousMessageHandled(false);
}

If you've enabled auto-instrumentation for Laravel or Symfony, you may disable flush on Bus withForceFlushOnBusExecution(false), as traces will be pushed at the end of the Request.

Materials

Demo implementation

Last updated