-
-
Notifications
You must be signed in to change notification settings - Fork 1
Basic Usage
Jean-Marc Strauven edited this page Aug 1, 2025
·
2 revisions
This guide shows you how to get started with Laravel ChronoTrace through practical examples.
composer require --dev grazulex/laravel-chronotrace# Automatic installation with middleware setup (recommended)
php artisan chronotrace:install
# Force reinstall if needed
php artisan chronotrace:install --forceWhat this does:
- Publishes configuration file to
config/chronotrace.php - Detects Laravel version and configures middleware appropriately
- For Laravel 12.x: Automatically adds middleware to
bootstrap/app.php - Creates storage directory with proper permissions
# Diagnose configuration
php artisan chronotrace:diagnose
# Test middleware setup
php artisan chronotrace:test-middlewareEdit config/chronotrace.php or set environment variables:
# Basic settings
CHRONOTRACE_ENABLED=true
CHRONOTRACE_MODE=record_on_error
CHRONOTRACE_STORAGE=local
# Performance settings
CHRONOTRACE_ASYNC_STORAGE=true
CHRONOTRACE_QUEUE_CONNECTION=database
# Event capture settings
CHRONOTRACE_CAPTURE_DATABASE=true
CHRONOTRACE_CAPTURE_CACHE=true
CHRONOTRACE_CAPTURE_HTTP=true
CHRONOTRACE_CAPTURE_JOBS=trueRecording Modes:
-
always- Record every request (development) -
sample- Record percentage of requests (staging) -
record_on_error- Only record on errors (production) -
targeted- Record specific routes/jobs only
# Record a simple API endpoint
php artisan chronotrace:record /api/usersThis captures all events that occur during the request:
- Database queries
- Cache operations
- HTTP requests to external services
- Queue jobs dispatched
# Record user creation
php artisan chronotrace:record /api/users \
--method=POST \
--data='{"name":"John Doe","email":"john@example.com"}'# Record authenticated endpoint
php artisan chronotrace:record /api/protected \
--method=GET \
--headers='{"Authorization":"Bearer your-token-here"}'# Record an e-commerce checkout process
php artisan chronotrace:record /checkout/process \
--method=POST \
--data='{"cart_id": 123, "payment_method": "credit_card"}' \
--headers='{"Authorization":"Bearer token","Content-Type":"application/json"}'php artisan chronotrace:listOutput:
βββββββββββββββ¬ββββββββββββββ¬ββββββββββββββββββββββ
β Trace ID β Size β Created At β
βββββββββββββββΌββββββββββββββΌββββββββββββββββββββββ€
β a1b2c3d4... β 15,234 bytesβ 2024-01-15 14:30:22 β
β e5f6g7h8... β 8,912 bytes β 2024-01-15 13:45:18 β
βββββββββββββββ΄ββββββββββββββ΄ββββββββββββββββββββββ
# Show only the 5 most recent traces
php artisan chronotrace:list --limit=5
# Show full trace IDs for easy copying
php artisan chronotrace:list --full-id
# Combine options
php artisan chronotrace:list --limit=10 --full-id# Replace with your actual trace ID
php artisan chronotrace:replay a1b2c3d4-e5f6-7890-abcd-ef1234567890This shows comprehensive information:
=== TRACE INFORMATION ===
π Trace ID: a1b2c3d4-e5f6-7890-abcd-ef1234567890
π Timestamp: 2024-01-15 14:30:22
π Environment: local
π Request URL: http://localhost:8000/api/users
π Response Status: 200
β±οΈ Duration: 245ms
πΎ Memory Usage: 18.45 KB
=== CAPTURED EVENTS ===
π DATABASE EVENTS
π [14:30:22.123] Query: SELECT * FROM users WHERE active = ? (15ms on mysql)
π [14:30:22.145] Query: SELECT * FROM roles WHERE user_id IN (?, ?, ?) (8ms on mysql)
ποΈ CACHE EVENTS
β [14:30:22.120] Cache MISS: users:list (store: redis)
πΎ [14:30:22.150] Cache WRITE: users:list (store: redis)
π HTTP EVENTS
π€ [14:30:22.200] HTTP Request: GET https://api.external.com/validation
π₯ [14:30:22.230] HTTP Response: GET https://api.external.com/validation β 200
π EVENTS SUMMARY
π Database events: 2
ποΈ Cache events: 2
π HTTP events: 2
βοΈ Job events: 0
π Total events: 6
# View detailed information with context, headers, and content
php artisan chronotrace:replay a1b2c3d4 --detailed
# Show SQL query bindings for debugging
php artisan chronotrace:replay a1b2c3d4 --db --bindings
# Show Laravel context (versions, config, environment)
php artisan chronotrace:replay a1b2c3d4 --context
# Show request and response headers
php artisan chronotrace:replay a1b2c3d4 --headers
# Show response content
php artisan chronotrace:replay a1b2c3d4 --content
# Compact output (minimal information)
php artisan chronotrace:replay a1b2c3d4 --compact
# Output as JSON for programmatic processing
php artisan chronotrace:replay a1b2c3d4 --format=json
# Output as raw data
php artisan chronotrace:replay a1b2c3d4 --format=raw# View database and cache events only
php artisan chronotrace:replay a1b2c3d4 --db --cache# Generate a Pest test from a trace
php artisan chronotrace:replay a1b2c3d4 --generate-test
# Generate test in specific directory
php artisan chronotrace:replay a1b2c3d4 --generate-test --test-path=tests/Integration
# View the generated test
cat tests/Generated/ChronoTrace_a1b2c3d4_Test.php
# Run the generated test
./vendor/bin/pest tests/Generated/ChronoTrace_a1b2c3d4_Test.php# 1. Record the slow endpoint
php artisan chronotrace:record /api/dashboard/stats
# 2. Get the trace ID
php artisan chronotrace:list --limit=1
# 3. Analyze database queries for N+1 problems
php artisan chronotrace:replay {trace-id} --dbLook for:
- Repeated similar queries
- Long execution times
- Too many queries for simple operations
# 1. Record an endpoint that calls external services
php artisan chronotrace:record /api/weather/forecast
# 2. View HTTP events to see external dependencies
php artisan chronotrace:replay {trace-id} --httpThis helps you:
- Track external API response times
- Monitor API failures
- Understand service dependencies
# 1. Record a data-heavy endpoint
php artisan chronotrace:record /api/products/search?q=laptop
# 2. Analyze cache effectiveness
php artisan chronotrace:replay {trace-id} --cacheLook for:
- Cache miss ratios
- Opportunities for additional caching
- Cache key patterns
# 1. Record an endpoint that dispatches jobs
php artisan chronotrace:record /orders/confirmation \
--method=POST \
--data='{"order_id": 12345}'
# 2. View queue job processing
php artisan chronotrace:replay {trace-id} --jobsThis shows:
- Which jobs were dispatched
- Job processing status
- Job failures and retries
# 1. Record a critical business workflow
php artisan chronotrace:record /api/orders/complete \
--method=POST \
--data='{"order_id": 123, "confirm": true}' \
--headers='{"Authorization":"Bearer token"}'
# 2. Verify the trace captured everything
php artisan chronotrace:replay {trace-id}
# 3. Generate a regression test
php artisan chronotrace:replay {trace-id} --generate-test --test-path=tests/Business
# 4. Review and run the generated test
cat tests/Business/ChronoTrace_{trace-id}_Test.php
./vendor/bin/pest tests/Business/ChronoTrace_{trace-id}_Test.php
# 5. Commit the test to prevent regressions
git add tests/Business/ChronoTrace_{trace-id}_Test.php
git commit -m "Add regression test for order completion workflow"This creates comprehensive tests that validate:
- HTTP status codes
- Response structure
- Performance expectations
- Database interactions
- Cache behavior
# Morning: Validate setup and check overnight traces
php artisan chronotrace:diagnose
php artisan chronotrace:list --limit=10
# During development: Record new features
php artisan chronotrace:record /api/new-feature
# Generate tests for new features
php artisan chronotrace:replay {trace-id} --generate-test --test-path=tests/Feature
# Debug issues: Replay problematic traces
php artisan chronotrace:replay {trace-id} --db
# End of day: Clean up old traces
php artisan chronotrace:purge --days=7# 1. Reproduce the bug with headers if needed
php artisan chronotrace:record /problematic-endpoint \
--method=POST \
--data='{"reproduce": "bug"}' \
--headers='{"Authorization":"Bearer token"}'
# 2. Identify the trace with full ID
php artisan chronotrace:list --limit=5 --full-id
# 3. Analyze step by step
php artisan chronotrace:replay {trace-id} # Overview
php artisan chronotrace:replay {trace-id} --db # Database issues
php artisan chronotrace:replay {trace-id} --http # External services
php artisan chronotrace:replay {trace-id} --cache # Cache problems
# 4. Generate test to prevent regression
php artisan chronotrace:replay {trace-id} --generate-test --test-path=tests/Bugs
# 5. Focus on specific areas based on findings# 1. Record baseline performance
php artisan chronotrace:record /performance-critical-endpoint
# 2. Note the trace ID and performance metrics
php artisan chronotrace:replay {trace-id}
# 3. Make optimizations (add caching, optimize queries, etc.)
# 4. Record again and compare
php artisan chronotrace:record /performance-critical-endpoint
# 5. Compare the two traces
php artisan chronotrace:replay {old-trace-id} --db
php artisan chronotrace:replay {new-trace-id} --db# Clean up traces older than 7 days
php artisan chronotrace:purge --days=7
# Force cleanup without confirmation
php artisan chronotrace:purge --days=7 --confirm# Check how much space traces are using
du -sh storage/chronotrace/
# List trace file sizes
php artisan chronotrace:list --limit=50Once you're comfortable with basic usage:
- Configure different recording modes
- Set up production monitoring
- Learn about custom storage options
- Explore advanced event filtering
# If installation fails, try diagnosing first
php artisan chronotrace:diagnose
# Test middleware setup
php artisan chronotrace:test-middleware
# Force reinstall
php artisan chronotrace:install --forceCheck your configuration:
# Verify ChronoTrace is enabled
php artisan config:show chronotrace.enabled
# Check recording mode
php artisan config:show chronotrace.mode
# Run full diagnosis
php artisan chronotrace:diagnose# Fix storage permissions
chmod -R 755 storage/chronotrace/
chown -R www-data:www-data storage/chronotrace/This usually means no events were captured. Check:
- Are the event listeners enabled in config?
- Is the request actually executing the code you expect?
- Are there any errors in the Laravel logs?
- Getting Started - Install and configure ChronoTrace
- Examples - Real-world debugging scenarios
- Production Guide - Deploy safely in production
- Troubleshooting - Solve common issues
| Section | Page | Description |
|---|---|---|
| π Basics | Your First Trace | Step-by-step beginner guide |
| ποΈ Config | Recording Modes | Choose when to record traces |
| π‘οΈ Security | Security & PII | Protect sensitive data |
| π§ Tools | Commands | Complete command reference |
- π¬ GitHub Discussions - Community support
- π Report Issues - Bug reports & feature requests
- π§ Contact - Direct support
- π‘ Feature Requests - Suggest improvements
ChronoTrace helps Laravel developers debug applications faster with intelligent request tracing.
Made with β€οΈ by Grazulex β’ Documentation updated August 2024