PHP code example of edwinekr / otel-elk-laravel

1. Go to this page and download the library: Download edwinekr/otel-elk-laravel 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/ */

    

edwinekr / otel-elk-laravel example snippets


use Edwinekr\OtelElkLaravel\Facades\ActivityLog;

// Log a custom activity
ActivityLog::log(
    action: 'order.checkout',
    entity: 'Order',
    entityId: $order->id,
    metadata: [
        'total' => $order->total,
        'items_count' => $order->items->count(),
        'payment_method' => $order->payment_method,
    ]
);

use Edwinekr\OtelElkLaravel\Services\ActivityLogService;

class OrderController extends Controller
{
    public function __construct(private ActivityLogService $activityLog)
    {
    }

    public function checkout(Request $request)
    {
        $order = Order::create([...]);

        $this->activityLog->log(
            action: 'order.checkout',
            entity: 'Order',
            entityId: $order->id,
            metadata: [
                'total' => $order->total,
                'items_count' => $order->items->count(),
            ]
        );

        return response()->json(['order' => $order]);
    }
}

use Edwinekr\OtelElkLaravel\Traits\LogsActivity;

class Product extends Model
{
    use LogsActivity;

    // Model will automatically log created, updated, deleted events
}

$product->logActivity('viewed', ['viewer_ip' => request()->ip()]);

use Edwinekr\OtelElkLaravel\Facades\Rum;

// Check if RUM is enabled
if (Rum::isEnabled()) {
    // Get the full RUM configuration
    $config = Rum::getConfig();
    
    // Get the initialization script as JSON string
    $initScript = Rum::getInitScript();
    
    // Get the CDN URL
    $cdnUrl = Rum::getCdnUrl();
    
    // Render the complete script HTML
    $html = Rum::renderScript();
}

use Edwinekr\OtelElkLaravel\Helpers\RumHelper;

// Get the full script HTML for custom implementations
$scriptHtml = RumHelper::renderScript();

'elastic_apm_rum' => [
    // ... other options
    'distributed_tracing_origins' => [
        'https://api.example.com',
        'https://backend.example.com',
    ],
],

// Store user in session
Session::put('user', [
    'id' => $user->id,
    'name' => $user->name,
    'email' => $user->email,
]);

// In routes/api.php
Route::middleware(['activity-log'])->group(function () {
    // Your routes
});

->withMiddleware(function (Middleware $middleware) {
    $middleware->append(\Edwinekr\OtelElkLaravel\Middleware\ActivityLogMiddleware::class);
})

'excluded_paths' => [
    'health',
    'ready',
    'livez',
    'metrics',
    '_debugbar/*',
    'telescope/*',
    'horizon/*',
    'sanctum/csrf-cookie',
    'api/internal/*',
],

'excluded_methods' => [
    'OPTIONS',
],

'sensitive_fields' => [
    'password',
    'password_confirmation',
    'token',
    'secret',
    'api_key',
    'credit_card',
    'cvv',
],
bash
php artisan vendor:publish --tag=otel-elk-config
bash
php artisan vendor:publish --tag=otel-elk-views