API Observability OpenTelemetryProvider - evansims/openfga-php GitHub Wiki

OpenTelemetry implementation for OpenFGA SDK observability. This class provides comprehensive telemetry capabilities for the OpenFGA SDK using OpenTelemetry APIs. It creates structured traces for all operations, records performance metrics, and tracks reliability indicators such as retry attempts and circuit breaker state changes. The implementation follows OpenTelemetry semantic conventions for HTTP clients and RPC operations, ensuring compatibility with standard observability tools and platforms. All telemetry is optional and gracefully degrades when OpenTelemetry is not configured.

Table of Contents


Namespace

OpenFGA\Observability


Source

View source code


Implements


Methods

endHttpRequest

public function endHttpRequest(
    ?object $span,
    ?Psr\Http\Message\ResponseInterface $response = NULL,
    ?Throwable $exception = NULL,
): void

End tracing for an HTTP request. Completes the HTTP request span, recording the response status and any errors that occurred. The span should include standard HTTP response attributes such as status code and response size.

View source


Parameters

Name Type Description
$span object | null The span identifier returned by startHttpRequest()
$response Psr\Http\Message\ResponseInterface | null The HTTP response received, if any
$exception Throwable | null Optional exception that occurred during the request

Returns

void


endOperation

public function endOperation(
    ?object $span,
    bool $success,
    ?Throwable $exception = NULL,
    array $attributes = [],
): void

End tracing for an OpenFGA API operation. Completes the trace span started with startOperation(), recording the operation outcome and any relevant metrics. If an exception occurred during the operation, it should be recorded in the span.

View source


Parameters

Name Type Description
$span object | null The span identifier returned by startOperation()
$success bool Whether the operation completed successfully
$exception Throwable | null Optional exception that occurred during the operation
$attributes array

Returns

void


recordAuthenticationEvent

public function recordAuthenticationEvent(
    string $event,
    bool $success,
    float $duration,
    array $attributes = [],
): void

Record authentication events. Records metrics and traces related to authentication flows, including token acquisition, refresh operations, and authentication failures. This helps monitor authentication performance and troubleshoot auth issues.

View source


Parameters

Name Type Description
$event string The authentication event type ('token_request', 'token_refresh', 'auth_failure')
$success bool Whether the authentication event was successful
$duration float The duration of the authentication operation in seconds
$attributes array

Returns

void


recordCircuitBreakerState

public function recordCircuitBreakerState(
    string $endpoint,
    string $state,
    int $failures,
    float $failureRate,
): void

Record circuit breaker state changes. Records metrics about circuit breaker state transitions and failure rates. This helps monitor the health of individual API endpoints and the SDK's resilience mechanisms.

View source


Parameters

Name Type Description
$endpoint string The API endpoint this circuit breaker protects
$state string The new circuit breaker state ('open', 'closed', 'half_open')
$failures int The current failure count
$failureRate float The current failure rate (0.0 to 1.0)

Returns

void


recordOperationMetrics

public function recordOperationMetrics(
    string $operation,
    float $duration,
    OpenFGA\Models\StoreInterface|string $store,
    ?OpenFGA\Models\AuthorizationModelInterface|string|null $model = NULL,
    array $attributes = [],
): void

Record performance metrics for OpenFGA operations. Records timing and throughput metrics for OpenFGA API operations, allowing monitoring of operation latency and identifying performance bottlenecks or degradations.

View source


Parameters

Name Type Description
$operation string The OpenFGA operation name
$duration float The operation duration in seconds
$store StoreInterface | string The store being operated on
$model AuthorizationModelInterface | null | string | null The authorization model used
$attributes array

Returns

void


recordRetryAttempt

public function recordRetryAttempt(
    string $endpoint,
    int $attempt,
    int $delayMs,
    string $outcome,
    ?Throwable $exception = NULL,
): void

Record retry attempt metrics. Records metrics about retry attempts, including the retry count, delay, and eventual outcome. This helps track the reliability and performance of API requests under various network conditions.

View source


Parameters

Name Type Description
$endpoint string The API endpoint being retried
$attempt int The current attempt number (1-based)
$delayMs int The delay before this attempt in milliseconds
$outcome string The outcome of this attempt ('success', 'failure', 'retry')
$exception Throwable | null Optional exception from this attempt

Returns

void


recordSpan

public function recordSpan(string $name, array $attributes = []): void

Record a telemetry span with attributes. Records a complete telemetry span for events that don't require start/end semantics. This is useful for event-driven telemetry where the event represents a point in time rather than a duration.

View source


Parameters

Name Type Description
$name string The span name
$attributes array

Returns

void


startHttpRequest

public function startHttpRequest(Psr\Http\Message\RequestInterface $request): object

Start tracing an HTTP request. Creates a new trace span for an outgoing HTTP request to the OpenFGA API. The span should follow OpenTelemetry semantic conventions for HTTP client operations, including standard HTTP attributes.

View source


Parameters

Name Type Description
$request Psr\Http\Message\RequestInterface The HTTP request being sent

Returns

object — A span identifier or context that can be passed to endHttpRequest()


startOperation

public function startOperation(
    string $operation,
    OpenFGA\Models\StoreInterface|string $store,
    ?OpenFGA\Models\AuthorizationModelInterface|string|null $model = NULL,
    array $attributes = [],
): object

Start tracing an OpenFGA API operation. Creates a new trace span for a high-level OpenFGA operation such as check, expand, or write operations. The span should include relevant attributes such as store ID, authorization model ID, and operation-specific metadata.

View source


Parameters

Name Type Description
$operation string The OpenFGA operation name (for example 'check', 'expand', 'write_tuples')
$store StoreInterface | string The store being operated on
$model AuthorizationModelInterface | null | string | null The authorization model being used
$attributes array

Returns

object — A span identifier or context that can be passed to endOperation()

⚠️ **GitHub.com Fallback** ⚠️