📦 Overview: Opt-in tracing, metrics & structured logging for Agentium.
🚀 Patterns
- Quick Attach:
instrument(agent, { exporters: ["console", "langfuse"] }) - Bus-Level Hook:
instrumentBus(team.eventBus)➔ broad capture across workflows. - Shorthands:
"console" | "langfuse" | "otel" | "json-file"auto-map to env vars. - Custom Sinks:
new CallbackExporter(trace => ...)for proprietary backends. - Metric Streaming:
metrics.toPrometheus()or.stream()for timeseries ingestion. - Rich Context:
structuredLogs: "json"injectstraceId/spanIdinto logs.
🚨 Anti-patterns
- Missing Flush: Process exit without
await obs.tracer.flush()➔ trace data loss. - Hardcoded Secrets: Explicit
publicKey/secretKey➔ strictly useLANGFUSE_*env vars. - Fragmented Setup: Manual
new Tracer()+new MetricsCollector()➔ useinstrument(). - Zombie Hooks: Re-instrumenting without
obs.detach()➔ memory leaks/duplicate spans.
💡 Best Practices
- 🔧 Env-Driven Config: Map
OTEL_EXPORTER_OTLP_ENDPOINT&LANGFUSE_BASE_URLfor zero-code env parity. - 🔧 Track Economics: Query
MetricsSnapshotfortotal_cost_usd&cached_tokens/reasoning_tokens. - 🔧 Quality Signals: Monitor
correctionRateandavgCritiqueScorefor agent evaluation. - 🔧 Graceful Teardown: Bind
process.on('SIGTERM')toobs.tracer.shutdown(). - 🔧 Hybrid Exporters:
["console", "json-file"](Dev) vs.["otel", "langfuse"](Prod).