๐ ๋น ๋ฅธ ๋ช ๋ น์ดยท์ค์ ์กฐํ๊ฐ ํ์ํ์ ๊ฐ์?
์ค๋ฌด ๋ ํผ๋ฐ์ค๋ฅผ ์ํ OpenTelemetry ์นํธ์ํธ ๋ฐ๋ก๊ฐ๊ธฐ โ
0. ๐ ์ ๋ชจ๋ํฐ๋ง์ ๋์ด โ๊ด์ธก์ฑ(Observability)โ์ธ๊ฐ์?
๋จ์ ๋ชจ๋ํฐ๋ง์ด โ์ง๊ธ CPU๊ฐ 90%๋คโ์ฒ๋ผ **์ด๋ฏธ ์๋ ์งํ(Known Unknowns)**๋ฅผ ๊ฐ์ํ๋ค๋ฉด, **๊ด์ธก์ฑ(Observability)**์ ๋ถ์ฐ ๋ง์ดํฌ๋ก์๋น์ค(MSA) ํ๊ฒฝ์์ โ์ ํน์ ์ฌ์ฉ์์ ๊ฒฐ์ ์์ฒญ๋ง 3์ด๊ฐ ๊ฑธ๋ ธ๋๊ฐ?โ๋ผ๋ **์์ธกํ์ง ๋ชปํ ์์ธ(Unknown Unknowns)**์ Traces(์ถ์ ) + Metrics(๋ฉํธ๋ฆญ) + Logs(๋ก๊ทธ) 3๋ ์ ํธ(Signals)์ ์๊ด๊ด๊ณ๋ก ์ฆ์ ๋ฐํ๋ด๋ ๋ฅ๋ ฅ์ ๋๋ค.
| ๊ด์ธก์ฑ 3๋ ์ถ | ํต์ฌ ์ง๋ฌธ | ์ ์ฅ์ (Grafana LGTM ์คํ) |
|---|---|---|
| Metrics (๋ฉํธ๋ฆญ) | โ์ ์ฒด ์๋ฌ์จ๊ณผ p99 ์ง์ฐ์๊ฐ์ด ํ๊ณ ์๋๊ฐ?โ | Prometheus v3.16+ |
| Traces (ํธ๋ ์ด์ค) | โAPI Gateway โ Order โ Payment ์ค ์ด๋์ ๋ณ๋ชฉ์ด ๋ฌ๋๊ฐ?โ | Grafana Tempo v3.1+ |
| Logs (๋ก๊ทธ) | โ๊ทธ ์๊ฐ ํด๋น ์์ฒญ์ด ๋ฑ์ ์ ํํ ์์ธ ๋ฉ์์ง๋ ๋ฌด์์ธ๊ฐ?โ | Grafana Loki v3.7+ |
1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11 (WSL2), macOS 13+, Linux | Ubuntu 24.04 LTS / Docker Desktop | LGTM ์คํ + OTel Collector ์ปจํ ์ด๋ ๊ตฌ๋ |
| CPU ์ํคํ ์ฒ | 2์ฝ์ด ์ด์ | 4์ฝ์ด ~ 8์ฝ์ด ์ด์ | Prometheus TSDB, Tempo, Loki, App ๋์ ์คํ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | ์ต์ 6 GB ์ด์ | 16 GB ์ด์ | Docker Desktop ํ ๋น ๋ฉ๋ชจ๋ฆฌ ์ต์ 6GB ๊ถ์ฅ |
| ๋์คํฌ (Storage) | 15 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 30 GB+ NVMe SSD | ์๊ณ์ด ๋ฉํธ๋ฆญ ๋ฐ ํธ๋ ์ด์ค ๋ธ๋ก ์ ์ฅ |
| ๋ฐํ์ / ๋๊ตฌ | Docker Engine 26+, Python 3.12+ (๋๋ Node 22+) | Docker Compose v2.30+, curl | otel/opentelemetry-collector-contrib:0.165.0 ๊ธฐ์ค |
[!IMPORTANT] ๊ณต์ ๋ฒค๋ ์ต์ ๋ฒ์ ์ ๋ณด (2026๋ 11์ ๊ธฐ์ค):
- OpenTelemetry Collector Contrib: v0.165.0
- Prometheus: v3.16.0 (Native OTLP Ingestion ์ง์)
- Grafana Tempo: v3.1.0 | Grafana Loki: v3.7.0
๐ญ ํ์คํ ๊ด์ธก์ฑ ํ์ดํ๋ผ์ธ ๋ฉํ ๋ชจ๋ธ (ASCII Diagram)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ OpenTelemetry + Grafana LGTM Observability Stack โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ [ Service A (Gateway) ] โโ(W3C traceparent)โโโถ [ Service B (Order) ] โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โผ OTLP (gRPC :4317 / HTTP :4318) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ OpenTelemetry Collector (v0.165.0) โ โ
โ โ Receivers(otlp) โโถ Processors(batch, memory_limiter, tail_samp) โ โ
โ โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโ โ
โ โผ โผ โผ โ
โ [ Tempo v3.1 ] [ Prometheus v3.16 ] [ Loki v3.7 ] โ
โ (๋ถ์ฐ ํธ๋ ์ด์ค) (RED/USE ๋ฉํธ๋ฆญ) (TraceID ๊ตฌ์กฐํ ๋ก๊ทธ)โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โผ โ
โ [ Grafana ํตํฉ ๋์๋ณด๋ (:3000) ] โ
โ โข Exemplar ํด๋ฆญ โ Trace โ ๊ด๋ จ Logs 1ํด๋ฆญ ์ ํ! โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2. [1๊ฐ] Docker Compose๋ก OTel Collector & LGTM ํ์คํ ๊ตฌ์ถ
๊ฐ์ฅ ๋จผ์ ๋ก์ปฌ ํ๊ฒฝ์ OpenTelemetry Collector + Tempo + Loki + Prometheus + Grafana ์ ์ฒด ๊ด์ธก์ฑ ๋ฐฑ์๋๋ฅผ ํ ๋ฒ์ ๊ธฐ๋ํฉ๋๋ค.
1๋จ๊ณ: ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ ๋ฐ OTel Collector ์ค์ ์์ฑ
mkdir otel-grafana-lab && cd otel-grafana-lab
otel-collector-config.yaml ํ์ผ์ ์์ฑํฉ๋๋ค:
# otel-collector-config.yaml
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
memory_limiter:
check_interval: 1s
limit_mib: 512
batch:
timeout: 2s
send_batch_size: 1024
exporters:
# 1. Traces -> Grafana Tempo
otlp/tempo:
endpoint: tempo:4317
tls:
insecure: true
# 2. Metrics -> Prometheus (OTLP HTTP Receiver ํ์ฉ)
otlphttp/prometheus:
endpoint: http://prometheus:9090/api/v1/otlp
tls:
insecure: true
# 3. Logs -> Grafana Loki (๋ค์ดํฐ๋ธ OTLP ์๋ํฌ์ธํธ)
otlphttp/loki:
endpoint: http://loki:3100/otlp
tls:
insecure: true
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp/tempo]
metrics:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/prometheus]
logs:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/loki]
2๋จ๊ณ: prometheus.yaml, tempo.yaml ๋ฐ compose.yaml ์์ฑ
# prometheus.yaml
global:
scrape_interval: 15s
# OTLP ์์ ๋ฐ Exemplar ํ์ฑํ
storage:
tsdb:
out_of_order_time_window: 30m
# tempo.yaml
server:
http_listen_port: 3200
distributor:
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
storage:
trace:
backend: local
local:
path: /var/tempo/traces
# compose.yaml
services:
otel-collector:
image: otel/opentelemetry-collector-contrib:0.165.0
command: ["--config=/etc/otelcol-contrib/config.yaml"]
volumes:
- ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml
ports:
- "4317:4317" # OTLP gRPC
- "4318:4318" # OTLP HTTP
depends_on: [tempo, prometheus, loki]
tempo:
image: grafana/tempo:3.1.0
command: ["-config.file=/etc/tempo.yaml"]
volumes:
- ./tempo.yaml:/etc/tempo.yaml
ports:
- "3200:3200"
loki:
image: grafana/loki:3.7.0
command: ["-config.file=/etc/loki/local-config.yaml"]
ports:
- "3100:3100"
prometheus:
image: prom/prometheus:v3.16.0
command:
- --config.file=/etc/prometheus/prometheus.yaml
- --web.enable-otlp-receiver
- --enable-feature=exemplar-storage
volumes:
- ./prometheus.yaml:/etc/prometheus/prometheus.yaml
ports:
- "9090:9090"
grafana:
image: grafana/grafana:11.4.0
environment:
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
ports:
- "3000:3000"
depends_on: [tempo, prometheus, loki]
# ์ ์ฒด LGTM + OTel ์คํ ๊ธฐ๋
docker compose up -d
# ์ํ ํ์ธ (๋ชจ๋ Up ์ํ์ฌ์ผ ํจ)
docker compose ps
3. [2๊ฐ] OTel Collector ํ์ดํ๋ผ์ธ & Tail-Based Sampling ์ค๊ณ
ํ๋ก๋์
ํธ๋ํฝ์ด ์ด๋น ์๋ง ๊ฑด์ ๋ฌํ ๋ ๋ชจ๋ ์ ์(200 OK) ํธ๋ ์ด์ค๋ฅผ ์ ์ฅํ๋ฉด ์คํ ๋ฆฌ์ง ๋น์ฉ์ด ํญ๋ฐํฉ๋๋ค.
์ค๋ฌด์์๋ ์๋ฌ(ERROR)๊ฐ ๋ฐ์ํ๊ฑฐ๋ ์ง์ฐ์๊ฐ์ด 500ms ์ด์์ธ ๋๋ฆฐ ์์ฒญ๋ง 100% ๋ณด์กดํ๊ณ , ์ ์ ์์ฒญ์ 5%๋ง ์ํ๋งํ๋ Tail-Based Sampling์ Collector์ ์ ์ฉํฉ๋๋ค.
# otel-collector-config.yaml (processors ์น์
ํ์ฅ ์์)
processors:
memory_limiter:
check_interval: 1s
limit_mib: 1024
# ๊ฐ์ธ์ ๋ณด(PII) ๋ง์คํน: ์ด๋ฉ์ผ/ํ ํฐ ์์ฑ ์ ๊ฑฐ
attributes/redact:
actions:
- key: http.request.header.authorization
action: delete
- key: user.email
action: hash
# Tail-Based Sampling: ์๋ฌ ๋ฐ 500ms ์ด๊ณผ ์ง์ฐ ํธ๋ ์ด์ค๋ 100% ์์ง
tail_sampling:
decision_wait: 10s
num_traces: 50000
policies:
- name: always-sample-errors
type: status_code
status_code: { status_codes: [ERROR] }
- name: slow-requests
type: latency
latency: { threshold_ms: 500 }
- name: probabilistic-normal
type: probabilistic
probabilistic: { sampling_percentage: 5 }
4. [3๊ฐ] FastAPI ๋ง์ดํฌ๋ก์๋น์ค ๊ณ์ธก & W3C ๋ถ์ฐ ํธ๋ ์ด์ฑ ์ ํ
๋ ๊ฐ์ ์๋น์ค(gateway-service โ order-service)๊ฐ HTTP๋ก ํต์ ํ ๋, OpenTelemetry SDK๊ฐ ์๋์ผ๋ก traceparent HTTP ํค๋๋ฅผ ์ฃผ์
ยท์ถ์ถํ์ฌ ํ๋์ ํตํฉ๋ Trace ํธ๋ฆฌ๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
1๋จ๊ณ: ์์กด์ฑ ์ค์น
pip install fastapi uvicorn httpx \
opentelemetry-distro opentelemetry-exporter-otlp \
opentelemetry-instrumentation-fastapi opentelemetry-instrumentation-httpx
2๋จ๊ณ: gateway.py (8000 ํฌํธ) & order.py (8001 ํฌํธ) ์์ฑ
# order.py (ํฌํธ 8001)
import logging, random, time
from fastapi import FastAPI, HTTPException
from opentelemetry import trace
app = FastAPI(title="order-service")
tracer = trace.get_tracer(__name__)
logger = logging.getLogger("order")
logging.basicConfig(level=logging.INFO)
@app.post("/orders")
async def create_order(item_id: str = "SKU-100"):
# ์ปค์คํ
Span ์์ฑ์ผ๋ก DB/๊ฒฐ์ ๊ตฌ๊ฐ ์ธ๋ถํ
with tracer.start_as_current_span("db.insert_order") as span:
span.set_attribute("order.item_id", item_id)
delay = random.uniform(0.05, 0.35)
time.sleep(delay)
ctx = span.get_span_context()
trace_id = format(ctx.trace_id, "032x")
logger.info(f"์ฃผ๋ฌธ ์ฒ๋ฆฌ ์๋ฃ item={item_id} trace_id={trace_id}")
if random.random() < 0.15:
span.set_status(trace.StatusCode.ERROR, "์ฌ๊ณ ๋ถ์กฑ ์ค๋ฅ")
raise HTTPException(status_code=500, detail="Out of stock")
return {"status": "created", "item_id": item_id, "trace_id": trace_id}
# gateway.py (ํฌํธ 8000)
import httpx
from fastapi import FastAPI
app = FastAPI(title="gateway-service")
@app.post("/checkout")
async def checkout(item_id: str = "SKU-100"):
# httpx ์๋ ๊ณ์ธก์ด traceparent ํค๋๋ฅผ order-service๋ก ์๋ ์ ํ
async with httpx.AsyncClient() as client:
resp = await client.post(f"http://127.0.0.1:8001/orders?item_id={item_id}")
return {"gateway_status": resp.status_code, "downstream": resp.json()}
3๋จ๊ณ: ์ฝ๋ ์์ ์์ด opentelemetry-instrument๋ก ์คํ & ๋ถํ ํ
์คํธ
# ํฐ๋ฏธ๋ 1: order-service ์คํ
OTEL_SERVICE_NAME=order-service \
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 \
OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
OTEL_LOGS_EXPORTER=otlp \
opentelemetry-instrument uvicorn order:app --port 8001
# ํฐ๋ฏธ๋ 2: gateway-service ์คํ
OTEL_SERVICE_NAME=gateway-service \
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 \
OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
OTEL_LOGS_EXPORTER=otlp \
opentelemetry-instrument uvicorn gateway:app --port 8000
# ํฐ๋ฏธ๋ 3: ํ
์คํธ ํธ๋ํฝ 20๊ฑด ๋ฐ์ก
for i in {1..20}; do curl -s -X POST "http://localhost:8000/checkout?item_id=SKU-$i"; echo ""; done
5. [4๊ฐ] Grafana ํตํฉ ์๊ด๋ถ์ (Trace-to-Logs & RED ๋ฉํธ๋ฆญ)
์ด์ ๋ธ๋ผ์ฐ์ ์์ http://localhost:3000 ์ผ๋ก ์ ์ํ์ฌ Grafana ๋ฐ์ดํฐ ์์ค๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
1๋จ๊ณ: Grafana Data Sources 3์ข ๋ฑ๋ก
Connections โ Data sources โ Add data source์์ ์๋ 3๊ฐ๋ฅผ ์ถ๊ฐํฉ๋๋ค:
- Prometheus: URL
http://prometheus:9090 - Tempo: URL
http://tempo:3200- Trace to logs ์ค์ : Data source =
Loki, Filter by Trace ID ํ์ฑํ (trace_id)
- Trace to logs ์ค์ : Data source =
- Loki: URL
http://loki:3100- Derived fields ์ค์ : Regex
trace_id=(\w+), Internal link =Tempo
- Derived fields ์ค์ : Regex
2๋จ๊ณ: ์ค๋ฌด ํต์ฌ PromQL (RED ๋ฉํธ๋ฆญ ๋์๋ณด๋)
# 1. Rate (์ด๋น ์์ฒญ ์ RPS)
sum(rate(http_server_duration_milliseconds_count[1m])) by (service_name)
# 2. Errors (5xx ์๋ฌ์จ %)
sum(rate(http_server_duration_milliseconds_count{http_status_code=~"5.."}[5m]))
/
sum(rate(http_server_duration_milliseconds_count[5m])) * 100
# 3. Duration (p95 / p99 ์๋ต ์ง์ฐ์๊ฐ)
histogram_quantile(0.99, sum(rate(http_server_duration_milliseconds_bucket[5m])) by (le, service_name))
3๋จ๊ณ: 1ํด๋ฆญ ์ฅ์ ์ถ์ (Explore)
- Explore โ Tempo์์
Service Name = gateway-service,Status = Error๋ก ๊ฒ์ํฉ๋๋ค. - ๋นจ๊ฐ์ ์๋ฌ Trace๋ฅผ ํด๋ฆญํด
gateway-service โ order-service โ db.insert_order์ํฐํด(Waterfall)์์ ๋ณ๋ชฉ ๊ตฌ๊ฐ์ ํ์ธํฉ๋๋ค. - Span ์ฐ์ธก์ Logs for this span ๋ฒํผ์ ํด๋ฆญํ๋ฉด Loki์์ ํด๋น
trace_id์ ์ ํํ ์๋ฌ ๋ก๊ทธ๋ง ์ฆ์ ํํฐ๋ง๋ฉ๋๋ค!
6. ์ค๋ฌด ์ฃผ์์ฌํญ & ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
- ๋ฉ๋ชจ๋ฆฌ ์ ํ(
memory_limiter) ์ต์ฐ์ ๋ฐฐ์น: OTel Collector ํ์ดํ๋ผ์ธ์processors์ฒซ ๋ฒ์งธ ์์์ ๋ฐ๋์memory_limiter๋ฅผ ๋์ด ํธ๋ํฝ ํญ์ฃผ ์ OOM Kill์ ๋ฐฉ์งํ์ธ์. - ๋ฏผ๊ฐ ์ ๋ณด(PII) ํํฐ๋ง: HTTP Authorization ํค๋, ์ฃผ๋ฏผ๋ฒํธ, ์นด๋๋ฒํธ ๋ฑ์ด Span Attribute๋ Log์ ํ๋ฌธ์ผ๋ก ๋จ์ง ์๋๋ก Collector
attributes/redactionํ๋ก์ธ์๋ก ๋ง์คํนํ์ธ์. - ์นด๋๋๋ฆฌํฐ(Cardinality) ํญ๋ฐ ์ฃผ์: Prometheus ๋ฉํธ๋ฆญ ๋ผ๋ฒจ(
user_id,order_id, UUID ํฌํจ URL ๊ฒฝ๋ก ๋ฑ)์ ๋ฌดํํ ์ฆ๊ฐํ๋ ๊ฐ์ ๋ฃ์ผ๋ฉด TSDB ๋ฉ๋ชจ๋ฆฌ๊ฐ ๊ณ ๊ฐ๋ฉ๋๋ค. ๊ณ ์ ID๋ Trace Attribute๋ Log์๋ง ๊ธฐ๋กํ์ธ์. - ๋น๋๊ธฐ ํ(
Kafka/RabbitMQ) ์ปจํ ์คํธ ์ ํ: HTTP๋ฟ๋ง ์๋๋ผ ๋ฉ์์ง ํ๋ฅผ ๊ฑฐ์น๋ MSA์์๋ Producer ๋ฉ์์ง ํค๋์traceparent๋ฅผ ์ฃผ์ ํ๊ณ Consumer์์ ์ถ์ถํด์ผ ์ ์ฒด ๋น๋๊ธฐ ํ์ดํ๋ผ์ธ์ด ํ๋์ Trace๋ก ์ด์ด์ง๋๋ค.
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์