API Observability TelemetryInterface - evansims/openfga-php GitHub Wiki

Interface for OpenTelemetry integration in the OpenFGA SDK. This interface provides methods for instrumenting OpenFGA operations with observability features including distributed tracing, metrics collection, and structured logging. Implementations should integrate with OpenTelemetry or other observability platforms to provide insights into SDK performance and operation outcomes. The interface supports both automatic instrumentation of HTTP requests and business-level instrumentation of OpenFGA API operations. All methods are designed to be safe to call even when no telemetry backend is configured, ensuring the SDK remains functional without observability dependencies.

Table of Contents


Namespace

OpenFGA\Observability


Source

View source code


Methods

endHttpRequest

public function endHttpRequest(
    object|null $span,
    ResponseInterface|null $response = NULL,
    Throwable|null $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 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|null $span,
    bool $success,
    Throwable|null $exception = NULL,
    array<string, mixed> $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&lt;string, mixed&gt;

Returns

void


recordAuthenticationEvent

public function recordAuthenticationEvent(
    string $event,
    bool $success,
    float $duration,
    array<string, mixed> $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&lt;string, mixed&gt;

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,
    StoreInterface|string $store,
    AuthorizationModelInterface|string|null $model = NULL,
    array<string, mixed> $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 | string | null The authorization model used
$attributes array&lt;string, mixed&gt;

Returns

void


recordRetryAttempt

public function recordRetryAttempt(
    string $endpoint,
    int $attempt,
    int $delayMs,
    string $outcome,
    Throwable|null $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<string, mixed> $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&lt;string, mixed&gt;

Returns

void


startHttpRequest

public function startHttpRequest(RequestInterface $request): object|null

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 RequestInterface The HTTP request being sent

Returns

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


startOperation

public function startOperation(
    string $operation,
    StoreInterface|string $store,
    AuthorizationModelInterface|string|null $model = NULL,
    array<string, mixed> $attributes = [],
): object|null

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 | string | null The authorization model being used
$attributes array&lt;string, mixed&gt;

Returns

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

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