Skip to content
advanced Phase 83 · Observability Basics

Observability Fundamentals

Observability fundamentals covering logs, metrics, traces, and the three pillars of observability

45m
0 problems
Topic Progress 0%

Three Pillars of Observability

Observability Pillars

┌─────────────────────────────────────────┐
│           Observability                  │
├─────────────┬─────────────┬─────────────┤
│    Logs     │   Metrics   │   Traces    │
│             │             │             │
│ What        │ How many    │ Where       │
│ happened?   │ / fast?     │ time spent? │
│             │             │             │
│ Events      │ Numbers     │ Flows       │
└─────────────┴─────────────┴─────────────┘

Logs vs Metrics vs Traces

Logs: Discrete events with context
  - "User 123 placed order for $50"
  - "Payment failed: gateway timeout"

Metrics: Numerical measurements over time
  - Requests per second: 1500
  - P99 latency: 450ms
  - Error rate: 0.5%

Traces: Request flow through distributed system
  - Order API → Cart Service → Payment → Inventory
  - Shows latency per service

Logging in Magento

Magento Logging

// PSR-3 logging
use Psr\Log\LoggerInterface;

class OrderService {
    public function __construct(
        private LoggerInterface $logger
    ) {}

    public function processOrder(Order $order) {
        $this->logger->info('Processing order', [
            'order_id' => $order->getId(),
            'customer_id' => $order->getCustomerId(),
            'total' => $order->getTotal()
        ]);

        try {
            $this->paymentService->charge($order);
        } catch (Exception $e) {
            $this->logger->error('Payment failed', [
                'order_id' => $order->getId(),
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            throw $e;
        }
    }
}

Log Levels

Level   | When to Use                    | Example
────────|────────────────────────────────|────────────────────
DEBUG   | Detailed diagnostic info       | SQL query executed
INFO    | Normal operations             | Order placed
WARNING | Unexpected but handled         | Slow query detected
ERROR   | Operation failed               | Payment failed
CRITICAL| System-level failure           | Database unreachable

Metrics Collection

Key Metrics Categories

RED Method:
  Rate: Requests per second
  Errors: Error rate
  Duration: Latency (p50, p95, p99)

USE Method:
  Utilization: CPU, memory, disk usage
  Saturation: Queue depth, connection count
  Errors: Error counts per resource

Magento Metrics

// Custom metrics with Prometheus
use Prometheus\CollectorRegistry;

$registry = CollectorRegistry::getDefault();

// Counter: total orders
$ordersTotal = $registry->registerCounter(
    'magento_orders_total',
    'Total orders placed'
);
$ordersTotal->inc();

// Histogram: order value
$orderValue = $registry->registerHistogram(
    'magento_order_value',
    'Order value distribution',
    [], [10, 50, 100, 500, 1000]
);
$orderValue->observe($order->getTotal());

// Gauge: active carts
$activeCarts = $registry->registerGauge(
    'magento_active_carts',
    'Number of active shopping carts'
);
$activeCarts->set($cartCount);

Infrastructure Metrics

# System metrics
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}')
mem_usage=$(free | grep Mem | awk '{printf "%.2f", $3/$2 * 100}')
disk_usage=$(df -h / | tail -1 | awk '{print $5}')

# Application metrics
curl -s http://localhost:9090/metrics | grep magento

Distributed Tracing

Tracing Architecture

┌─────────────────────────────────────────┐
│ Request: POST /checkout                 │
│                                         │
│  API Gateway ──▶ Order Service          │
│  (10ms)          (50ms)                 │
│                     │                   │
│                     ▼                   │
│                Payment Service          │
│                (200ms)                  │
│                     │                   │
│                     ▼                   │
│                Inventory Service        │
│                (30ms)                   │
│                                         │
│  Total trace: 290ms                     │
└─────────────────────────────────────────┘

OpenTelemetry Integration

// Initialize tracer
use OpenTelemetry\SDK\Trace\TracerProvider;

$tracerProvider = new TracerProvider();
$tracer = $tracerProvider->getTracer('magento');

// Create span
$span = $tracer->spanBuilder('process_order')
    ->setAttribute('order.id', $orderId)
    ->startSpan();

try {
    $scope = $tracer->withActiveSpan($span);
    // Process order...
    $span->setStatus(StatusCode::OK);
} catch (Exception $e) {
    $span->setStatus(StatusCode::ERROR, $e->getMessage());
    throw $e;
} finally {
    $span->end();
}

Quiz

1. What are the three pillars of observability?

Question 1 options

2. What do traces show in distributed systems?

Question 2 options

3. Which metric method focuses on Rate, Errors, and Duration?

Question 3 options

Flashcards

Question

Three pillars of observability?

Answer

Logs (events), Metrics (numbers), Traces (flows)

Question

RED method?

Answer

Rate, Errors, Duration - application-level metrics

Question

USE method?

Answer

Utilization, Saturation, Errors - resource-level metrics

Question

Traces purpose?

Answer

Track request flow and time spent in distributed systems

Revision Notes

Key Takeaways

  • 1. Logs provide discrete event details with context
  • 2. Metrics provide numerical measurements over time
  • 3. Traces show request flow through distributed systems
  • 4. RED method: Rate, Errors, Duration for application metrics
  • 5. USE method: Utilization, Saturation, Errors for resources

Interview Tips

  • Explain the three pillars and when to use each
  • Discuss RED vs USE metrics frameworks
  • Describe distributed tracing and its benefits

Cheat Sheet

Observability:
  Logs: What happened (events)
  Metrics: How many/fast (numbers)
  Traces: Where time spent (flows)

Metrics:
  RED: Rate, Errors, Duration
  USE: Utilization, Saturation, Errors

Tracing:
  OpenTelemetry: Standard framework
  Spans: Individual service calls
  Trace: Complete request flow