monkeyscloud/monkeyslegion-devtools

Enterprise-grade debugging, profiling, inspection, and diagnostics package for MonkeysLegion β€” zero-magic PHP 8.4+ observability.

Maintainers

Package info

github.com/MonkeysCloud/MonkeysLegion-DevTools

Homepage

Issues

pkg:composer/monkeyscloud/monkeyslegion-devtools

Statistics

Installs: 13

Dependents: 1

Suggesters: 0

Stars: 0

2.0.4 2026-04-29 20:41 UTC

This package is auto-updated.

Last update: 2026-04-29 20:42:01 UTC


README

PHP 8.4+ MIT License Tests Coverage PHP 8.4 Hooks

πŸ’ MonkeysLegion DevTools

Enterprise-grade debugging, profiling, and diagnostics for MonkeysLegion v2.

Inspect every request, route, query, cache operation, event, and exception β€” with a self-contained debug toolbar, intelligent anomaly detection, and zero runtime overhead when disabled.

Laravel has Telescope.  Symfony has the Web Profiler.
MonkeysLegion has DevTools β€” and it's smarter than both.

✨ Key Features

Feature Description
πŸš€ Zero Overhead Early-return when disabled β€” no reflection, no magic, no cost
πŸͺ PHP 8.4 Property Hooks 26 computed properties replace traditional getters across the codebase
πŸ” N+1 Detection Automatic duplicate query grouping with configurable thresholds
⚑ Cache Intelligence Hit/miss ratios, hot key ranking, per-store breakdown
πŸ“‘ Event Storm Detection Timeline visualization with storm alerts and slow listener tracking
🎯 Deterministic Sampling Hash-based consistency for distributed trace coherence
πŸ”’ Redaction Pipeline Recursive key-based masking β€” secrets never leak, even in dev
πŸ›‘οΈ Request Fingerprinting xxh3-based grouping for automatic request clustering
πŸ–₯️ Debug Toolbar Self-contained Tokyo Night UI β€” no external assets required
πŸ§ͺ 184 Tests 572 assertions across 22 test files β€” 90%+ coverage

πŸ“¦ Installation

composer require monkeyscloud/monkeyslegion-devtools --dev

For production diagnostics (sampling + redacted output):

composer require monkeyscloud/monkeyslegion-devtools

Requirements

  • PHP 8.4+ (property hooks, asymmetric visibility)
  • PSR-7 HTTP Message (psr/http-message ^2.0)
  • PSR-15 Middleware (psr/http-server-middleware ^1.0)

πŸš€ Quick Start

1. Boot & Register Middleware

use MonkeysLegion\DevTools\DevToolsServiceProvider;

$devtools = new DevToolsServiceProvider();
$profiler = $devtools->boot([
    'enabled'     => true,
    'environment' => 'local',
    'sample_rate' => 1.0,
    'storage'     => ['driver' => 'file', 'path' => 'var/devtools/profiles'],
    'collectors'  => [
        'request'    => true,
        'route'      => true,
        'middleware'  => true,
        'query'      => true,
        'cache'      => true,
        'event'      => true,
        'exception'  => true,
    ],
    'toolbar' => ['enabled' => true],
]);

// Add to your PSR-15 pipeline
$middleware = $devtools->createMiddleware();

2. Record Intelligence Data

// Query profiling β€” N+1 is detected automatically
$queryCollector = $profiler->getCollector('query');
$queryCollector->recordQuery('SELECT * FROM users WHERE id = ?', durationMs: 2.3, connection: 'mysql');

// Cache tracking β€” hot keys are ranked automatically
$cacheCollector = $profiler->getCollector('cache');
$cacheCollector->recordOperation('redis', 'user:42', 'get', hit: true, durationMs: 0.4);

// Event timeline β€” storms are flagged automatically
$eventCollector = $profiler->getCollector('event');
$eventCollector->recordDispatch('App\Event\OrderPlaced', [
    ['name' => 'SendConfirmation', 'duration_ms' => 45.2],
    ['name' => 'UpdateInventory', 'duration_ms' => 12.1],
]);

3. Inspect via CLI

# System status
php ml devtools:status

# List recent profiled requests (color-coded by status)
php ml devtools:requests --limit=20

# Inspect the latest request in detail
php ml devtools:request latest

# Inspect by partial ID
php ml devtools:request c461

# Export a sanitized profile for sharing
php ml devtools:export latest --output=profile.json

# Clear all stored profiles
php ml devtools:clear

4. Debug Toolbar

When toolbar.enabled is true, the toolbar auto-injects into HTML responses:

  • Toggle: Click the πŸ’ bar or press Ctrl+Shift+D
  • 5 Panels: Overview, Queries, Cache, Events, Exceptions
  • Severity Badges: Green / Yellow / Red per-panel status at a glance
  • Self-Contained: Zero external CSS/JS β€” embedded Tokyo Night theme

The toolbar only injects into text/html responses and respects a configurable size limit.

βš™οΈ Configuration

Create config/devtools.mlc:

devtools {
    enabled     = ${DEVTOOLS_ENABLED:true}
    environment = ${APP_ENV:local}
    sample_rate = ${DEVTOOLS_SAMPLE_RATE:1.0}

    storage {
        driver         = "file"
        path           = "var/devtools/profiles"
        retention_days = 7
        max_profiles   = 1000
    }

    collectors {
        request    = true
        route      = true
        middleware = true
        query      = true
        cache      = true
        event      = true
        exception  = true
    }

    redaction {
        enabled = true
        keys    = ["password", "token", "secret", "api_key", "authorization", "cookie"]
    }

    thresholds {
        slow_request_ms  = 200
        slow_query_ms    = 100
        n_plus_one_count = 5
    }

    toolbar {
        enabled        = true
        max_payload_kb = 256
    }

    production {
        sample_rate = 0.01
    }
}

πŸ”¬ Collectors

Built-in Collectors (7)

# Collector Icon Priority Key Features
1 Request 🌐 1000 Method, URI, headers, IP hashing, fingerprint, timing, memory
2 Middleware πŸ”— 950 Per-middleware timing, memory delta, bottleneck detection
3 Route πŸ”€ 900 Pattern, controller, params, middleware stack, OpenAPI hints
4 Query πŸ—„οΈ 800 SQL timing, N+1 detection, duplicate grouping, source tracing
5 Cache ⚑ 700 Hit/miss ratio, hot key ranking, per-store breakdown
6 Event πŸ“‘ 600 Timeline, storm detection, slow listeners, failed tracking
7 Exception πŸ’₯ 100 Class, message, trace, chain traversal, xxh3 fingerprint

Intelligence Features

N+1 Query Detection

πŸ”΄ N+1 DETECTED: SELECT * FROM comments WHERE post_id = ? (Γ—47 executions)
   Source: App\Repository\CommentRepository::findByPost() at line 42

The QueryCollector automatically fingerprints SQL by normalizing literals and compares execution counts against the configurable threshold.

Cache Hot Key Analysis

πŸ”₯ Hot Keys:
   user:session:abc123  (Γ—84 accesses)
   config:app           (Γ—31 accesses)
   route:cache          (Γ—22 accesses)

Event Storm Detection

πŸŒͺ️ STORM: App\Event\CacheInvalidated dispatched 142 times in this request

Custom Collectors

Implement CollectorInterface to add your own:

use MonkeysLegion\DevTools\Contract\CollectorInterface;
use MonkeysLegion\DevTools\Profiler\ProfileContext;

final class MyCollector implements CollectorInterface
{
    public function name(): string { return 'custom'; }
    public function label(): string { return 'Custom'; }
    public function icon(): string { return 'πŸ”§'; }
    public function priority(): int { return 500; }
    public function isEnabled(): bool { return true; }

    public function start(ProfileContext $context): void { /* setup */ }
    public function stop(ProfileContext $context): void { /* teardown */ }
    public function collect(ProfileContext $context): array { return [...]; }
}

$profiler->addCollector(new MyCollector());

🏷️ Attributes

use MonkeysLegion\DevTools\Attribute\{Profile, IgnoreProfile, Redact};

// Force profiling on a specific route with a label
#[Profile(name: 'checkout.process', includePayload: true)]
public function processCheckout(): Response { }

// Exclude high-frequency endpoints from profiling
#[IgnoreProfile(reason: 'health check')]
public function healthCheck(): Response { }

// Mark sensitive constructor parameters
public function __construct(
    #[Redact] private readonly string $apiSecret,
    #[Redact(replacement: '***')] private readonly string $dbPassword,
) {}

πŸͺ PHP 8.4 Property Hooks

DevTools leverages 26 PHP 8.4 property hooks β€” replacing traditional getters with declarative computed properties. This is a first for PHP frameworks.

// Profile model β€” zero getters, all computed
$profile->durationMs           // float: endedAt - startedAt
$profile->durationFormatted    // "42.7ms" | "1.23s" | "850ΞΌs"
$profile->isError              // statusCode >= 400
$profile->isSlow               // durationMs > threshold
$profile->statusBadge          // 🟒 πŸ”΅ 🟠 πŸ”΄ βšͺ
$profile->memoryPeakFormatted  // "12.4 MB"
$profile->memoryDelta          // bytes used during request
$profile->createdAtFormatted   // "2026-04-27 03:42:53.087"

// Profiler engine β€” live state hooks
$profiler->isActive            // currently profiling?
$profiler->collectorCount      // number of registered collectors
$profiler->collectorNames      // ['request', 'query', 'cache', ...]

// QueryCollector β€” computed analytics
$collector->queryCount         // total queries recorded
$collector->totalDurationMs    // sum of all query times
$collector->duplicateCount     // grouped duplicate queries
$collector->hasNPlusOne        // automatic N+1 flag
$collector->slowestQueryMs     // max single query time

// CacheCollector β€” computed metrics
$collector->hitRatio           // hits / total (float)
$collector->hitRatioFormatted  // "85.7%"
$collector->operationCount     // total operations tracked

// EventCollector β€” computed state
$collector->hasStorm           // event storm detected?
$collector->failedListenerCount // listeners that threw
$collector->totalListenerMs    // aggregate listener time

// ServiceProvider β€” boot state
$provider->booted              // has boot() been called?
$provider->profiler            // resolved Profiler instance
$provider->toolbar             // resolved ToolbarRenderer

Asymmetric Visibility

All mutable state uses private(set) β€” the profiler's internal state is read-only for external consumers:

public private(set) string $id;
public private(set) bool $booted = false;
public private(set) ?Profiler $profiler = null;

πŸ—„οΈ Storage Drivers

Driver Use Case Persistence
FileProfileStorage Local development β€” JSON files with index βœ… Disk
MemoryProfileStorage Testing & benchmarks ❌ Request-scoped
NullProfileStorage Disabled / CI environments ❌ No-op

File storage features:

  • JSON format β€” human-readable, diffable, grep-friendly
  • Index file β€” fast listing without deserializing all profiles
  • Retention pruning β€” automatic cleanup on write (no cron needed)
  • Query filtering β€” method, URI, status, duration, environment, time range

πŸ–₯️ Debug Toolbar Panels

Overview Panel

Request summary, performance alerts (slow/error), and collector badge aggregation.

Query Panel

SQL listing with per-query timing, duplicate warnings (🟑), and N+1 alerts (πŸ”΄). Source file tracing for each query.

Cache Panel

Hit/miss ratio gauges, per-store breakdown table, and πŸ”₯ hot key ranking.

Event Panel

Chronological timeline with relative timestamps, storm warnings (πŸŒͺ️), and failed listener tracking.

Exception Panel

Error display with class highlighting, sanitized stack traces, and previous exception chain traversal.

Custom Panels

Extend AbstractPanel to add your own:

use MonkeysLegion\DevTools\Toolbar\AbstractPanel;
use MonkeysLegion\DevTools\Profiler\Profile;

final class MyPanel extends AbstractPanel
{
    public function id(): string { return 'custom'; }
    public function label(): string { return 'Custom'; }
    public function icon(): string { return 'πŸ”§'; }
    public function priority(): int { return 400; }

    public function badge(Profile $profile): string { return '3 items'; }
    public function badgeSeverity(Profile $profile): string { return 'ok'; }

    public function render(Profile $profile): string
    {
        return $this->section('Data', $this->renderTable([
            'Key' => 'Value',
        ]));
    }
}

πŸ—οΈ Architecture

src/
β”œβ”€β”€ Attribute/               # #[Profile], #[IgnoreProfile], #[Redact]
β”œβ”€β”€ Collector/               # 7 intelligence collectors
β”‚   β”œβ”€β”€ CacheCollector.php       # Hit/miss, hot keys, per-store
β”‚   β”œβ”€β”€ EventCollector.php       # Timeline, storms, slow listeners
β”‚   β”œβ”€β”€ ExceptionCollector.php   # Traces, chains, fingerprinting
β”‚   β”œβ”€β”€ MiddlewareCollector.php  # Per-middleware timing, bottlenecks
β”‚   β”œβ”€β”€ QueryCollector.php       # N+1, duplicates, SQL fingerprinting
β”‚   β”œβ”€β”€ RequestCollector.php     # Headers, IP hash, fingerprint
β”‚   └── RouteCollector.php       # Pattern, controller, OpenAPI
β”œβ”€β”€ Command/                 # 5 CLI commands (devtools:*)
β”œβ”€β”€ Contract/                # Stable interfaces
β”œβ”€β”€ Exception/               # Package exceptions
β”œβ”€β”€ Middleware/               # PSR-15 DevToolsMiddleware
β”œβ”€β”€ Profiler/                # Core engine
β”‚   β”œβ”€β”€ Profile.php              # 10 property hooks
β”‚   β”œβ”€β”€ ProfileContext.php       # Request lifecycle context
β”‚   └── Profiler.php             # Orchestrator with 3 hooks
β”œβ”€β”€ Redaction/               # Key-based recursive masking
β”œβ”€β”€ Sampler/                 # Rate-based + deterministic sampling
β”œβ”€β”€ Storage/                 # File, Memory, Null drivers
β”œβ”€β”€ Toolbar/                 # Self-contained debug toolbar
β”‚   β”œβ”€β”€ Panel/                   # 5 built-in panels
β”‚   β”œβ”€β”€ AbstractPanel.php        # Rendering helpers
β”‚   β”œβ”€β”€ PanelInterface.php       # Panel contract
β”‚   β”œβ”€β”€ ToolbarInjector.php      # HTML response injection
β”‚   └── ToolbarRenderer.php      # Self-contained HTML/CSS/JS
└── DevToolsServiceProvider.php  # Config-driven bootstrap

40 source files Β· 5,099 lines Β· 22 test files Β· 184 tests Β· 572 assertions

πŸ†š Competitive Comparison

Feature Symfony Profiler Laravel Telescope ML DevTools
PHP Version 8.2+ 8.3+ 8.4+
Property Hooks 0 0 26
N+1 Detection ❌ ❌ βœ… Automatic
Event Storm Detection ❌ ❌ βœ…
Cache Hot Key Analysis ❌ ❌ βœ…
Deterministic Sampling ❌ ❌ βœ… Hash-based
Request Fingerprinting ❌ ❌ βœ… xxh3
Priority Collectors ❌ ❌ βœ… With wrap
Partial Value Redaction ❌ ❌ βœ…
Self-Contained Toolbar ❌ Requires Webpack ❌ Requires Tailwind βœ… Embedded
Zero-Overhead Disabled ⚠️ Partial ❌ βœ… Full
Asymmetric Visibility ❌ ❌ βœ… private(set)
MLC Config Format ❌ ❌ βœ… Native

πŸ§ͺ Testing

# Run all tests
composer test

# With testdox output
./vendor/bin/phpunit --testdox

# Static analysis
composer analyse

Test Suite Coverage

Area Test Files Tests Assertions
Collectors (7) 7 62 178
Profiler Core 3 28 102
Storage (3) 3 28 72
Toolbar & Panels 3 38 108
Service Provider 1 12 34
Middleware 1 4 12
Attributes 1 9 18
Exceptions 1 3 6
Redaction 1 10 24
Sampler 1 6 18
Total 22 184 572

πŸ—ΊοΈ Roadmap

Phase Focus Status
0 Contracts & Skeleton βœ… Complete
1 MVP Profiler & CLI βœ… Complete
2 Debug Toolbar & Local DX βœ… Complete
3 Query, Cache & Event Intelligence βœ… Complete
4 Container, Entity & Config Inspectors πŸ”œ Next
5 OpenAPI, Security & Validation Reports πŸ”œ Planned
6 Production Diagnostics & APM πŸ”œ Planned
7 MonkeysCloud SaaS Integration πŸ”œ Planned

🀝 Contributing

  1. Fork & clone the repository
  2. Install dependencies: composer install
  3. Run the test suite: composer test
  4. Run static analysis: composer analyse
  5. Submit a PR against main

Please ensure all new collectors include corresponding unit tests and property hooks where applicable.

πŸ“„ License

MIT β€” Β© 2026 MonkeysCloud Team

Built with πŸ’ by MonkeysCloud