Files
2nd/10_Wiki/Topics/Architecture/Cross-Cutting Concerns.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

172 lines
5.3 KiB
Markdown

---
id: wiki-2026-0508-cross-cutting-concerns
title: Cross-Cutting Concerns
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Cross Cutting Concerns, AOP Concerns, 횡단 관심사]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [aop, architecture, separation-of-concerns]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: typescript
framework: nestjs
---
# Cross-Cutting Concerns
## 매 한 줄
> **"매 module 매 흩어지는 functionality (logging, auth, tx) 의 횡단"**. AOP (Aspect-Oriented Programming, Kiczales 1997) 의 motivation. 2026 현재 NestJS interceptors, Spring AOP, OpenTelemetry auto-instrumentation, Sidecar (Istio) 매 modern realization.
## 매 핵심
### 매 정의
- 단일 module 의 boundary 를 넘어 매 application 매 widely-scattered concern.
- Tangled with business logic → SoC (Separation of Concerns) 위반.
- Solution: 매 aspect, decorator, middleware, sidecar, interceptor 로 추출.
### 매 typical concerns
- **Logging / tracing** — 매 method entry/exit log.
- **Authentication / authorization** — 매 endpoint 매 token check.
- **Transaction management** — DB tx begin/commit/rollback.
- **Caching** — function result memoization.
- **Validation** — input schema check.
- **Rate limiting** — request throttling.
- **Metrics / observability** — counter, histogram.
- **Error handling** — global exception mapper.
- **i18n** — locale-aware response.
- **Audit** — who/when/what.
### 매 응용
1. Web framework middleware (Express, NestJS).
2. Spring `@Transactional` / `@Cacheable`.
3. Service mesh (Istio) — mTLS, retry, circuit-break 매 sidecar.
4. OpenTelemetry auto-instrumentation 매 tracing 매 zero code change.
## 💻 패턴
### NestJS interceptor — logging + timing
```typescript
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(ctx: ExecutionContext, next: CallHandler) {
const start = Date.now();
const req = ctx.switchToHttp().getRequest();
return next.handle().pipe(
tap(() => log.info({ url: req.url, ms: Date.now() - start }))
);
}
}
```
### NestJS guard — authz
```typescript
@Injectable()
export class RolesGuard implements CanActivate {
canActivate(ctx: ExecutionContext): boolean {
const required = Reflect.getMetadata('roles', ctx.getHandler());
const user = ctx.switchToHttp().getRequest().user;
return required.some(r => user.roles.includes(r));
}
}
```
### Spring AOP — `@Transactional`
```java
@Service
public class OrderService {
@Transactional
public Order place(Order o) {
repository.save(o);
inventory.decrement(o.itemId);
// throw → rollback
}
}
```
### Python decorator — caching
```python
from functools import lru_cache
@lru_cache(maxsize=1024)
def expensive_lookup(key: str) -> Result:
return db.query(key)
```
### TypeScript decorator (TC39 stage 3) — metrics
```typescript
function timed<This, Args extends any[], Return>(
target: (this: This, ...args: Args) => Return,
ctx: ClassMethodDecoratorContext
) {
return function (this: This, ...args: Args): Return {
const start = performance.now();
try { return target.apply(this, args); }
finally { metrics.histogram(ctx.name, performance.now() - start); }
};
}
class API {
@timed
fetchUser(id: string) { /* ... */ }
}
```
### OpenTelemetry — zero-code tracing
```python
# auto-instrumentation, no code changes
# pip install opentelemetry-distro opentelemetry-instrumentation-fastapi
# opentelemetry-instrument --traces_exporter otlp uvicorn app:app
```
### Service mesh (Istio) — mTLS via sidecar
```yaml
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata: { name: default, namespace: prod }
spec:
mtls: { mode: STRICT }
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Single service, JS/TS | NestJS interceptor / Express middleware |
| JVM, mature DI | Spring AOP / aspectj |
| Polyglot microservices | Service mesh (Istio / Linkerd) |
| Tracing only | OpenTelemetry auto-instrumentation |
| Function-level caching | language-native decorator |
**기본값**: framework-native (interceptor/middleware/decorator) for in-process; service mesh for network-level.
## 🔗 Graph
- 부모: [[Separation of Concerns]] · [[Software Architecture]]
- 변형: [[Aspect-Oriented Programming (AOP)]]
- 응용: [[NestJS]] · [[Istio]] · [[OpenTelemetry]]
- Adjacent: [[Dependency Injection (의존성 주입)]]
## 🤖 LLM 활용
**언제**: identify cross-cutting concern in legacy code, refactor to aspect/middleware.
**언제 X**: design-time SoC decision (need human architectural taste).
## ❌ 안티패턴
- **Concern duplication**: 매 controller 매 try/catch + log + auth 매 copy-paste.
- **Aspect overuse**: business logic 까지 aspect 로 → magic, debugging hell.
- **Order dependency 무시**: interceptor chain 의 순서 의존 → fragile.
- **Side-effect in interceptor**: DB write 매 logging interceptor → tx boundary 깨짐.
## 🧪 검증 / 중복
- Verified (Kiczales et al. 1997 *AOP*, Spring/NestJS docs 2026).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full content covering interceptors, decorators, service mesh |