Files
2nd/10_Wiki/Topic_Programming/Coding/Observability_OpenTelemetry.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

131 lines
4.5 KiB
Markdown

---
id: observability-opentelemetry
title: OpenTelemetry — 통합 추적/메트릭/로그
category: Coding
status: draft
source_trust_level: B
verification_status: conceptual
created_at: 2026-05-09
updated_at: 2026-05-09
tags: [observability, opentelemetry, tracing, vibe-coding]
tech_stack: { language: "TypeScript / OpenTelemetry SDK", applicable_to: ["Backend"] }
applied_in: []
aliases: [otel, distributed tracing, span, instrumentation]
---
# OpenTelemetry
> Vendor 중립 표준. 한 SDK 로 trace + metrics + logs 수집 → OTLP → Jaeger / Tempo / Datadog / Honeycomb 어디든. **auto-instrumentation 으로 80% 무료**, 비즈니스 span 만 직접.
## 📖 핵심 개념
- **Trace**: 요청 1건이 만든 span 트리.
- **Span**: 한 작업 (HTTP, DB query, function call).
- **Context propagation**: trace-id / span-id 가 외부 호출 / 큐로 이어짐.
- **Resource**: service name / version / instance.
- **Exporter**: OTLP gRPC/HTTP 가 표준.
## 💻 코드 패턴
### Node 부팅 시 셋업
```ts
// telemetry.ts (앱 import 보다 먼저)
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { Resource } from '@opentelemetry/resources';
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
const sdk = new NodeSDK({
resource: new Resource({
[ATTR_SERVICE_NAME]: 'user-service',
[ATTR_SERVICE_VERSION]: process.env.GIT_SHA,
}),
traceExporter: new OTLPTraceExporter({ url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT }),
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({ url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT }),
exportIntervalMillis: 30_000,
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
process.on('SIGTERM', () => sdk.shutdown());
```
`node --require ./telemetry.ts app.ts` 로 실행.
자동으로 잡히는 것:
- HTTP server / client
- Express / Fastify / Koa
- pg / mysql / redis
- aws-sdk
- gRPC
### 비즈니스 span 직접
```ts
import { trace } from '@opentelemetry/api';
const tracer = trace.getTracer('user-service');
async function processOrder(orderId: string) {
return tracer.startActiveSpan('order.process', async (span) => {
span.setAttribute('order.id', orderId);
try {
const o = await fetchOrder(orderId); // 자동 child span
await chargePayment(o); // 자동 child span
span.setAttribute('order.amount', o.total);
return o;
} catch (e) {
span.recordException(e as Error);
span.setStatus({ code: 2 /* ERROR */ });
throw e;
} finally {
span.end();
}
});
}
```
### Span 안의 baggage (메타데이터 전파)
```ts
import { propagation, context } from '@opentelemetry/api';
const baggage = propagation.createBaggage({
user_id: { value: userId },
feature_flag_x: { value: 'on' },
});
context.with(propagation.setBaggage(context.active(), baggage), () => {
// 이 안의 모든 외부 호출이 user_id 전파
});
```
## 🤔 의사결정 기준
| 도입 단계 | 권장 |
|---|---|
| 모놀리스 단일 서비스 | auto-instrumentation 만 |
| 마이크로서비스 (>3) | auto + 비즈니스 span |
| 매우 큰 트래픽 (비용) | head sampling (1%) + tail sampling (errors) |
| Lambda / Edge | OpenTelemetry Lambda Layer |
| 자체 인프라 | OpenTelemetry Collector + Tempo/Jaeger |
| SaaS (Datadog/Honeycomb) | OTLP 호환 |
## ❌ 안티패턴
- **모든 함수에 span**: 폭증. 큰 단위 (request, transaction, queue job).
- **span attribute 에 PII**: 같은 redact 정책. 민감 X.
- **수동 trace-id 전파**: SDK 가 자동. 직접 설정 시 충돌.
- **sampling 100% prod**: 비용 폭증. 1-10% + tail sampling for errors.
- **resource attribute 없음**: 어떤 서비스 / 버전인지 모름.
- **shutdown 누락**: deploy 시 마지막 span 손실. SIGTERM handler.
- **로그와 trace 별개**: trace_id 를 로그에 같이 출력 → exemplar 로 그래프 점프.
## 🤖 LLM 활용 힌트
- "auto-instrumentation 먼저, 비즈니스 span 만 직접" 명시.
- service.name / version / env 리소스 필수.
- shutdown handler.
## 🔗 관련 문서
- [[Observability_Correlation_IDs]]
- [[Observability_RED_USE_Metrics]]