Event-Driven Logging Architecture
This directory contains the event-driven logging infrastructure that decouples usage tracking from the gateway service using NATS JetStream.Architecture
Components
1. Event Publisher (pkg/events/)
- types.go: Event schemas (ToolExecutionEvent, TunnelStatusEvent, PolicyBlockEvent)
- publisher.go: NATS JetStream publisher with automatic reconnection
- mock.go: Mock publisher for testing
2. Gateway (internal/gateway/usage_tracker.go)
- Publishes tool execution events to NATS asynchronously
- Non-blocking, fire-and-forget pattern
- No local batching (moved to consumer)
3. Log Consumer Service (cmd/log-consumer/, internal/logconsumer/)
- Subscribes to NATS usage log stream
- Batches events (100 entries or 5 seconds)
- Writes to PostgreSQL using existing database methods
- Durable consumer with manual acknowledgment
Benefits
✅ Reliability: Events persist in NATS even if gateway crashes ✅ Decoupling: Gateway and logging are independent services ✅ Scalability: Consumer can be horizontally scaled ✅ Zero Data Loss: Durable consumer with acknowledgments ✅ Observability: NATS monitoring endpointQuick Start
Docker Compose (Recommended)
Local Development
Configuration
Gateway
Log Consumer
Testing
Monitoring
NATS Monitoring
- HTTP endpoint: http://localhost:8222
- Check streams: http://localhost:8222/jsz
- Check connections: http://localhost:8222/connz
JetStream Streams
AGENTICOS_USAGE- Tool usage logsAGENTICOS_TUNNEL- Tunnel state changes (future)AGENTICOS_POLICY- Policy blocks (future)
Future Extensions
This architecture is ready for:- Alert Engine - Subscribe to events, evaluate OPA rules, trigger alerts
- Notification Worker - Route alerts to Slack, PagerDuty, webhooks
- Prometheus Metrics - Track throughput, lag, error rates
- Horizontal Scaling - Run multiple log-consumer instances
Troubleshooting
Gateway can’t connect to NATS
Events not reaching database
Performance tuning
Migration from Old Architecture
The old in-memory channel approach has been completely replaced. Key changes:- ❌ Removed:
worker(),flush(),Start(),Stop()methods - ✅ Added: NATS publisher, event schemas, log-consumer service
- ✅ Same: Database schema, API unchanged, backward compatible
IMPLEMENTATION_SUMMARY.md for full details.