PHP code example of kirschbaum-development / monitor
1. Go to this page and download the library: Download kirschbaum-development/monitor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kirschbaum-development / monitor example snippets
use Kirschbaum\Monitor\Facades\Monitor;
// In App\Http\Controllers\Api\UserController
class UserController extends Controller
{
public function login(LoginRequest $request)
{
// Automatic origin resolution from full namespace
Monitor::log($this)->info('User login attempt', [
'email' => $request->email,
'ip' => $request->ip()
]);
}
}
// In App\Services\Payment\StripePaymentService
class StripePaymentService
{
public function processPayment($amount)
{
// Origin automatically resolved to clean, readable format
Monitor::log($this)->info('Processing payment', [
'amount' => $amount,
'processor' => 'stripe'
]);
}
}
// bootstrap/app.php or register as route middleware
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'circuit' => \Kirschbaum\Monitor\Http\Middleware\CheckCircuitBreakers::class,
]);
})
// In your routes
Route::middleware(['circuit:payment_gateway,external_api'])
->group(function () {
Route::post('/payments', [PaymentController::class, 'store']);
Route::get('/external-data', [DataController::class, 'fetch']);
});
// Or on individual routes
Route::get('/api/data')
->middleware('circuit:slow_service')
->name('data.fetch');
Monitor::controlled('payment_processing', $this)
->overrideTraceId('custom-trace-12345')
// Origin is automatically set from the second parameter ($this)
use Kirschbaum\Monitor\Facades\Monitor;
class OrderController extends Controller
{
public function store()
{
// Start trace (typically via middleware)
Monitor::trace()->start();
Monitor::log($this)->info('Processing order');
// All subsequent operations share the same trace ID
$this->paymentService->charge($amount);
// Queue job with trace context
ProcessOrderJob::dispatch($order);
}
}
class PaymentService
{
public function charge($amount)
{
// Automatically
// Manual control
Monitor::trace()->start(); // Generate new UUID (throws if already started)
Monitor::trace()->override($traceId); // Use specific ID (overwrites existing)
Monitor::trace()->pickup($traceId); // Start if not started, optionally with specific ID
Monitor::trace()->id(); // Get current ID (throws if not started)
Monitor::trace()->hasStarted(); // Check if active
Monitor::trace()->hasNotStarted(); // Check if not active
// Service A
$response = Http::withHeaders([
'X-Trace-Id' => Monitor::trace()->id()
])->get('https://service-b.example.com/api/data');
// Service B automatically uses the same trace ID
use Kirschbaum\Monitor\Facades\Monitor;
class DataProcessor
{
public function processData()
{
$timer = Monitor::time(); // Auto-starts
// Your processing code
$this->heavyOperation();
$elapsed = $timer->elapsed(); // Milliseconds
Monitor::log($this)->info('Processing complete', [
'duration_ms' => $elapsed
]);
}
}
use Kirschbaum\Monitor\Facades\Monitor;
// Check circuit breaker state
$isOpen = Monitor::breaker()->isOpen('payment_gateway');
$state = Monitor::breaker()->getState('payment_gateway');
// Manual state management
Monitor::breaker()->recordFailure('api_service', 300); // Record failure with 300s decay
Monitor::breaker()->recordSuccess('api_service'); // Record success (resets failures)
Monitor::breaker()->reset('api_service'); // Force reset
Monitor::breaker()->forceOpen('api_service'); // Force open state
class ExternalApiService
{
public function makeRequest()
{
if (Monitor::breaker()->isOpen('external_api')) {
return $this->getCachedResponse();
}
try {
$response = $this->performApiCall();
Monitor::breaker()->recordSuccess('external_api');
return $response;
} catch (Exception $e) {
Monitor::breaker()->recordFailure('external_api', 120);
throw $e;
}
}
}
use Kirschbaum\Monitor\Facades\Monitor;
// Direct redaction using configured profile
$redactedData = Monitor::redactor()->redact($sensitiveData);
// Custom profile redaction
$redactedData = Monitor::redactor()->redact($sensitiveData, 'strict');
// Example usage
class UserDataProcessor
{
public function processUserData(array $userData)
{
// Redact before logging or storing
$safeData = Monitor::redactor()->redact($userData);
Monitor::log($this)->info('Processing user data', $safeData);
return $this->process($userData); // Use original for processing
}
}