๐Ÿ”ง DevOps

Observability

Observability

The ability to understand a system's internal state from the outside. Logs, metrics, traces.

๐Ÿ“– Detailed Explanation

Observability is the ability to understand a system's internal state through the external outputs it generates. A concept derived from control theory, it has become a core capability for understanding system behavior and diagnosing problems in distributed systems and microservices environments.

The three core pillars (Three Pillars) of Observability are Metrics, Logs, and Traces. Metrics are numerical data over time (CPU utilization, request count), logs are text records of individual events, and traces track the propagation path of a request through a distributed system. All three must be analyzed together to see the full picture.

Monitoring and Observability are different. Monitoring detects "known problems" (an alert when CPU exceeds 90%). Observability lets you explore "unknown problems" (Why are only certain users slow? Which combination of requests causes errors?). Because unexpected problems occur in complex systems, Observability is essential.

A modern Observability stack collects data with OpenTelemetry and visualizes it with tools such as Grafana/Jaeger/Kibana. Commercial services include Datadog, New Relic, Honeycomb, and Dynatrace. The ideal is a workflow that interconnects the three pillars, drilling down from a metric anomaly โ†’ related traces โ†’ the corresponding logs.

๐Ÿ’ป Code Example

# OpenTelemetry ํ†ตํ•ฉ ์„ค์ • - ๋ฉ”ํŠธ๋ฆญ, ๋กœ๊ทธ, ํŠธ๋ ˆ์ด์Šค ํ†ตํ•ฉ
from opentelemetry import trace, metrics
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
import structlog

# ๊ณตํ†ต ๋ฆฌ์†Œ์Šค ์ •์˜
resource = Resource.create({
    "service.name": "order-service",
    "service.version": "1.2.3",
    "deployment.environment": "production"
})

# Tracer ์„ค์ •
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)

# Meter ์„ค์ •
metrics.set_meter_provider(MeterProvider(resource=resource))
meter = metrics.get_meter(__name__)

# ์ปค์Šคํ…€ ๋ฉ”ํŠธ๋ฆญ
order_counter = meter.create_counter(
    "orders_total",
    description="Total number of orders"
)
order_latency = meter.create_histogram(
    "order_processing_seconds",
    description="Order processing latency"
)

# ๊ตฌ์กฐํ™”๋œ ๋กœ๊ฑฐ (trace_id ์ž๋™ ํฌํ•จ)
logger = structlog.get_logger()

def process_order(order_id: str):
    # ํŠธ๋ ˆ์ด์Šค ์‹œ์ž‘
    with tracer.start_as_current_span("process_order") as span:
        span.set_attribute("order.id", order_id)

        # ๋กœ๊ทธ์— trace_id ์ž๋™ ์—ฐ๊ฒฐ
        ctx = trace.get_current_span().get_span_context()
        log = logger.bind(
            trace_id=format(ctx.trace_id, '032x'),
            span_id=format(ctx.span_id, '016x'),
            order_id=order_id
        )

        log.info("order_processing_started")

        with tracer.start_as_current_span("validate_payment"):
            # ๊ฒฐ์ œ ๊ฒ€์ฆ ๋กœ์ง...
            pass

        # ๋ฉ”ํŠธ๋ฆญ ๊ธฐ๋ก
        order_counter.add(1, {"status": "success", "type": "online"})
        order_latency.record(0.235, {"step": "total"})

        log.info("order_processing_completed")
# Grafana ๋Œ€์‹œ๋ณด๋“œ์—์„œ ์„ธ ๊ธฐ๋‘ฅ ํ†ตํ•ฉ ์ฟผ๋ฆฌ

# 1. ๋ฉ”ํŠธ๋ฆญ: ๋ถ„๋‹น ์ฃผ๋ฌธ ์„ฑ๊ณต๋ฅ  (Prometheus/PromQL)
sum(rate(orders_total{status="success"}[5m]))
/
sum(rate(orders_total[5m])) * 100

# 2. ๋กœ๊ทธ: ์—๋Ÿฌ ๋กœ๊ทธ ๊ฒ€์ƒ‰ (Loki/LogQL)
{app="order-service"}
|= "error"
| json
| trace_id != ""  # trace_id๊ฐ€ ์žˆ๋Š” ๋กœ๊ทธ๋งŒ

# 3. ํŠธ๋ ˆ์ด์Šค: ๋А๋ฆฐ ์š”์ฒญ ๋ถ„์„ (Tempo/TraceQL)
{resource.service.name="order-service" && duration > 1s}

# Grafana์—์„œ Exemplar ์—ฐ๊ฒฐ ์„ค์ •
# ๋ฉ”ํŠธ๋ฆญ ํŒจ๋„์—์„œ ์ด์ƒ์  ํด๋ฆญ -> ํ•ด๋‹น trace๋กœ ์ด๋™ -> ๊ด€๋ จ ๋กœ๊ทธ ํ™•์ธ

# OpenTelemetry Collector ์„ค์ •
receivers:
  otlp:
    protocols:
      grpc: { endpoint: "0.0.0.0:4317" }
processors:
  batch: {}
exporters:
  prometheus: { endpoint: "0.0.0.0:8889" }
  loki: { endpoint: "http://loki:3100/loki/api/v1/push" }
  otlp: { endpoint: "tempo:4317" }
service:
  pipelines:
    metrics: { receivers: [otlp], processors: [batch], exporters: [prometheus] }
    logs: { receivers: [otlp], processors: [batch], exporters: [loki] }
    traces: { receivers: [otlp], processors: [batch], exporters: [otlp] }

๐Ÿ—ฃ๏ธ How It's Said in Practice

SRE: "It took us 2 hours to find the cause of the last outage. Logs and metrics weren't connected, so we had to compare them one by one, matching up time windows."

Developer: "Wouldn't they connect right away if we embedded trace_id in the logs?"

Senior: "Right. Let's adopt OpenTelemetry and connect trace โ†’ log โ†’ metrics all through trace_id. In Grafana, if you click a metric anomaly point, you can jump straight to the corresponding trace."

Interviewer: "Please explain the difference between Observability and Monitoring."

Candidate: "Monitoring watches predefined metrics. It deals with known-unknowns, such as 'an alert when CPU exceeds 90%.' Observability explores the data the system generates to find unknown-unknowns. For example, you can analyze by slicing the data to find 'why only users of a specific browser in a specific region get errors.' If you interconnect the three pillars, you can drill down from a metric anomaly to traces and logs."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "This function calls an external API, so please add a span. Later, when it's slow, we'll be able to tell where the bottleneck is."

Author: "I'll wrap it with with tracer.start_as_current_span('external_api_call'), and also record the API response code as a span attribute."

โš ๏ธ Cautions

๐Ÿ“š Learn More