feat (recovery): Introduced panic recovery and monitoring mechanisms.

A new recovery package has been added, containing Recoverer and PanicMetrics, for capturing panics in goroutines.
It records stack trace information and provides statistical metrics. This mechanism is also integrated into the TCP connector and listener.
Enhance service stability and observability.
This commit is contained in:
Gouryella
2025-12-10 16:10:26 +08:00
parent 8e6f9c553a
commit f6f2c6fd5b
4 changed files with 241 additions and 10 deletions

View File

@@ -0,0 +1,110 @@
package recovery
import (
"fmt"
"runtime/debug"
"sync"
"sync/atomic"
"time"
"go.uber.org/zap"
)
type PanicMetrics struct {
totalPanics uint64
recentPanics []PanicRecord
mu sync.Mutex
logger *zap.Logger
alerter Alerter
}
type PanicRecord struct {
Location string
Timestamp time.Time
Value interface{}
Stack string
}
type Alerter interface {
SendAlert(title string, message string)
}
func NewPanicMetrics(logger *zap.Logger, alerter Alerter) *PanicMetrics {
return &PanicMetrics{
recentPanics: make([]PanicRecord, 0, 100),
logger: logger,
alerter: alerter,
}
}
func (pm *PanicMetrics) RecordPanic(location string, panicValue interface{}) {
atomic.AddUint64(&pm.totalPanics, 1)
pm.mu.Lock()
record := PanicRecord{
Location: location,
Timestamp: time.Now(),
Value: panicValue,
Stack: string(debug.Stack()),
}
pm.recentPanics = append(pm.recentPanics, record)
if len(pm.recentPanics) > 100 {
pm.recentPanics = pm.recentPanics[1:]
}
shouldAlert := pm.shouldAlertUnlocked()
pm.mu.Unlock()
if shouldAlert {
pm.sendAlert()
}
}
func (pm *PanicMetrics) shouldAlertUnlocked() bool {
threshold := time.Now().Add(-5 * time.Minute)
count := 0
for i := len(pm.recentPanics) - 1; i >= 0; i-- {
if pm.recentPanics[i].Timestamp.After(threshold) {
count++
} else {
break
}
}
rate := float64(count) / 5.0
return rate >= 2.0
}
func (pm *PanicMetrics) sendAlert() {
total := atomic.LoadUint64(&pm.totalPanics)
pm.mu.Lock()
threshold := time.Now().Add(-5 * time.Minute)
count := 0
for i := len(pm.recentPanics) - 1; i >= 0; i-- {
if pm.recentPanics[i].Timestamp.After(threshold) {
count++
} else {
break
}
}
rate := float64(count) / 5.0
pm.mu.Unlock()
pm.logger.Error("ALERT: High panic rate detected",
zap.Uint64("total_panics", total),
zap.Float64("rate_per_minute", rate),
)
if pm.alerter != nil {
message := "High panic rate detected: %.2f panics/minute (total: %d)"
pm.alerter.SendAlert(
"Drip: High Panic Rate",
fmt.Sprintf(message, rate, total),
)
}
}

View File

@@ -0,0 +1,79 @@
package recovery
import (
"runtime/debug"
"go.uber.org/zap"
)
type Recoverer struct {
logger *zap.Logger
metrics MetricsCollector
}
type MetricsCollector interface {
RecordPanic(location string, panicValue interface{})
}
func NewRecoverer(logger *zap.Logger, metrics MetricsCollector) *Recoverer {
return &Recoverer{
logger: logger,
metrics: metrics,
}
}
func (r *Recoverer) WrapGoroutine(name string, fn func()) func() {
return func() {
defer func() {
if p := recover(); p != nil {
r.logger.Error("goroutine panic recovered",
zap.String("goroutine", name),
zap.Any("panic", p),
zap.ByteString("stack", debug.Stack()),
)
if r.metrics != nil {
r.metrics.RecordPanic(name, p)
}
}
}()
fn()
}
}
func (r *Recoverer) SafeGo(name string, fn func()) {
go r.WrapGoroutine(name, fn)()
}
func (r *Recoverer) Recover(location string) {
if p := recover(); p != nil {
r.logger.Error("panic recovered",
zap.String("location", location),
zap.Any("panic", p),
zap.ByteString("stack", debug.Stack()),
)
if r.metrics != nil {
r.metrics.RecordPanic(location, p)
}
}
}
func (r *Recoverer) RecoverWithCallback(location string, callback func(panicValue interface{})) {
if p := recover(); p != nil {
r.logger.Error("panic recovered with callback",
zap.String("location", location),
zap.Any("panic", p),
zap.ByteString("stack", debug.Stack()),
)
if r.metrics != nil {
r.metrics.RecordPanic(location, p)
}
if callback != nil {
callback(p)
}
}
}