Skip to content

Attributes & Diagnostic Reports

This section covers how to interact with the diagnostic reports and attributes exposed by Leakless.


1. Inspecting the Report Object

At the end of every request cycle, $leakless->endRequest() returns a Report object containing diagnostic data and memory metrics.

Practical Usage Example

php
$report = $leakless->endRequest();

// 1. Check if the request executed cleanly
if (! $report->isClean) {
    logger()->warning('Worker state anomaly detected in request');
}

// 2. Check if open database transactions were intercepted and rolled back
if ($report->hasTransactionLeak) {
    // Send metric to Prometheus, Datadog, or Sentry
    metrics()->increment('worker.transactions.rolled_back');
}

// 3. Inspect real Linux kernel RSS memory metrics
echo "Physical memory consumed in this request: {$report->memoryDriftMb} MB\n";
echo "Current worker resident memory (RSS): {$report->metricsAfter->rssMb} MB\n";

// 4. Check if worker reached memory ceilings or request limits
if ($report->shouldRecycle) {
    // Gracefully terminate or signal process manager
    $worker->stop();
}

Available Properties

PropertyTypeDescription
$report->isCleanbooltrue if no transactions leaked and no worker recycling was triggered.
$report->hasTransactionLeakbooltrue if one or more open PDO transactions were rolled back.
$report->shouldRecyclebooltrue if memory or request count limits were breached.
$report->memoryDriftMbfloatPhysical RSS delta ($\Delta\text{RSS}$) in megabytes during the request.
$report->metricsBeforeProcessMetricsSnapshot of process memory before request handling.
$report->metricsAfterProcessMetricsSnapshot of process memory after request handling.

2. Process Memory Metrics (ProcessMetrics)

The $report->metricsBefore and $report->metricsAfter properties contain Linux kernel memory details:

php
$metrics = $report->metricsAfter;

// Real physical RAM in MB (Resident Set Size)
$rssMb = $metrics->rssMb;

// Total virtual memory size in MB
$virtualMb = $metrics->sizeMb;

// Raw kernel page counts
$residentPages = $metrics->residentPages;

3. The #[AllowPersistentState] Attribute

Use this attribute to declare intentional, thread-safe static caches so they are excluded from static analysis and reflection warnings:

php
use TheMattos\Leakless\Attributes\AllowPersistentState;

class DatabaseSchemaRegistry
{
    // Explicitly permitted: thread-safe immutable boot metadata
    #[AllowPersistentState]
    public static array $tableDefinitions = [];
}

Released under the MIT License.