Course contentsShow
← Cloud and Infrastructure
Lesson 1970 of 2,98942. Observability - MetricsFree lesson

Binary Operators and Arithmetic

Perform mathematical operations between metrics using arithmetic, comparison, and logical operators.

Binary Operators and Arithmetic

You'll learn how to perform mathematical operations between metrics using PromQL's arithmetic, comparison, and logical operators to create powerful derived metrics.

What Are Binary Operators?

Binary operators in PromQL let you combine two metrics (or a metric and a scalar) using math or logic. Think of them like calculator buttons, but for entire time-series datasets. Instead of adding 2 + 3, you're adding cpu_usage + memory_overhead across thousands of servers simultaneously.

Arithmetic Operators

PromQL supports standard math operations between metrics:

  • + Addition: node_memory_used + node_memory_cached
  • - Subtraction: node_memory_total - node_memory_available
  • * Multiplication: request_count * avg_request_duration
  • / Division: success_requests / total_requests
  • % Modulo: total_bytes % 1024
  • ^ Power: base_value ^ 2

These operations happen element-wise on matching label sets. If two metrics share the same labels, Prometheus performs the operation; otherwise, the series are ignored.

Comparison Operators

Compare metrics to create boolean results (1 for true, 0 for false):

  • memory_used > memory_threshold
  • response_time <= 500
  • error_rate == 0
  • disk_usage != 100

Logical Operators

Combine boolean expressions:

  • and: Both conditions true
  • or: Either condition true
  • unless: Left true AND right false

Example: (cpu_usage > 80) and (memory_usage > 80) finds nodes stressed on both resources.

Key Takeaway: Binary operators transform raw metrics into meaningful insights by performing math, comparisons, and logic across time-series data, enabling you to calculate ratios, detect thresholds, and combine conditions for sophisticated monitoring queries.