C.W.K.
Stream
Lesson 04 of 05 · published

Logging 과 Observability

~22 min · logging, tracing, mcp-logging, metrics

Level 0호기심 많은 독자
0 XP0/48 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Production server 가 observability 셋 카테고리 필요. 각자 일찍 추가 싸고 나중 retrofit 고통.

  1. Server 쪽 구조화 log. Stderr (또는 sidecar 파일) 에 JSON-line 으로 최소: timestamp, request id, method, latency, success/error. 사고 조사 source of truth.
  2. Protocol 레벨 logging. MCP 가 logging capability 정의: 양쪽 광고 시 client 가 log 구독 가능 + server 가 notifications/message event 로 emit. Host 쪽에서 integration 이슈 디버깅 시 유용.
  3. Metric 과 tracing. tools/call name + outcome 별 counter; latency histogram; tool 의 HTTP 호출 통해 propagate 하는 request 당 tracing span. OpenTelemetry 가 안전 디폴트; per-tool counter 가 가장 유용한 single 차트.

새 운영자 잡히는 부분: security track 의 audit log. Audit log 는 observability log 와 구별 — append-only, immutable, 디버깅 아니라 컴플라이언스/포렌식용. 두 stream 분리 (다른 파일, 다른 rotation policy) — 일상 log-purge cron 에 audit 데이터 잃지 않게.

Code

Tool wrapper 의 구조화 per-request logging·python
import logging, time, json, sys

logger = logging.getLogger("mcp")
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(handler)

def trace(method):
    async def wrapped(*args, **kwargs):
        start = time.perf_counter()
        ok, err = True, None
        try:
            return await method(*args, **kwargs)
        except Exception as e:
            ok, err = False, repr(e); raise
        finally:
            logger.info(json.dumps({
                "ts": time.time(), "tool": method.__name__,
                "latency_ms": round((time.perf_counter()-start)*1000, 2),
                "ok": ok, "error": err,
            }))
    return wrapped
Client 에서 MCP protocol log 구독·python
async with ClientSession(read, write) as s:
    init = await s.initialize()
    if init.capabilities.get("logging"):
        await s.set_logging_level("info")
        # Session 이 notifications/message event emit; sink 로 pipe.

External links

Exercise

최소 server 에 per-tool latency log line 추가. 각 tool 10 번 호출. Log 를 jq 로 파이프해서 tool 별 평균 latency 계산. 보이는 숫자가 시작 baseline — production 가까이 가면 첫 build 할 차트.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.