Overview
go_logs includes a zero-overhead metrics system that tracks logging activity in real-time. Metrics are always enabled and use atomic operations for thread-safe, lock-free counting. Implementation:~/workspace/source/metrics.go
Quick Start
Accessing Metrics
From~/workspace/source/logger_impl.go:377:
Metrics Structure
From~/workspace/source/metrics.go:13-37:
atomic.Int64 for thread-safe, lock-free operations.
Available Metrics
Total Count
From~/workspace/source/metrics.go:61-63:
Count by Level
From~/workspace/source/metrics.go:53-58:
Dropped Count
From~/workspace/source/metrics.go:66-69:
Increment Operations
Automatic Increment
From~/workspace/source/logger_impl.go:164-166:
~/workspace/source/metrics.go:45-50:
Increment Dropped
From~/workspace/source/metrics.go:72-74:
Snapshots
From~/workspace/source/metrics.go:89-99:
Using Snapshots
- Periodic metrics collection
- Comparing metrics over time
- Exporting to monitoring systems
Reset Metrics
From~/workspace/source/metrics.go:79-85:
- Testing
- Periodic metrics collection (snapshot then reset)
- Application lifecycle management
Shared Metrics (Parent/Child Loggers)
From~/workspace/source/logger_impl.go:272:
Examples
Periodic Metrics Reporting
Prometheus Integration
Health Check Endpoint
Alerting on Error Spikes
Testing with Metrics
Performance
Zero Overhead Design
From~/workspace/source/metrics.go:13-37:
- Uses
atomic.Int64for lock-free operations - No mutex contention
- No heap allocations
- ~1-2ns per increment (negligible)
Thread Safety
All operations are thread-safe:Increment()uses atomic addCount()andTotal()use atomic load- No locks required
- Safe for concurrent access from multiple goroutines
MetricsGetter Interface
From~/workspace/source/metrics.go:123-125:
Level to Index Mapping
From~/workspace/source/metrics.go:102-119:
Level to array index for byLevel[6].
Best Practices
Monitor Error Rates
Periodic Snapshots
Use in Health Checks
Export to Monitoring Systems
See Also
- Hooks - Use hooks for custom metrics collection
- Production Deployment - Monitoring and observability
- Logger API - Logger interface methods