-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.go
143 lines (120 loc) · 2.69 KB
/
metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package shrinkmap
import (
"sync"
"time"
)
// ErrorRecord represents a single error or panic occurrence
type ErrorRecord struct {
Timestamp time.Time
Error interface{} // panic 값이나 error 둘 다 저장 가능
Stack string // 스택 트레이스 저장
}
// Metrics tracks performance and error metrics of the map
type Metrics struct {
mu sync.RWMutex
totalShrinks int64
lastShrinkDuration time.Duration
totalItemsProcessed int64
peakSize int32
shrinkPanics int64
lastPanicTime time.Time
lastError *ErrorRecord
errorHistory []ErrorRecord
totalErrors int64
}
func (m *Metrics) TotalShrinks() int64 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.totalShrinks
}
func (m *Metrics) LastShrinkDuration() time.Duration {
m.mu.RLock()
defer m.mu.RUnlock()
return m.lastShrinkDuration
}
func (m *Metrics) TotalItemsProcessed() int64 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.totalItemsProcessed
}
func (m *Metrics) PeakSize() int32 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.peakSize
}
func (m *Metrics) RecordError(err error, stack string) {
m.mu.Lock()
defer m.mu.Unlock()
record := ErrorRecord{
Timestamp: time.Now(),
Error: err,
Stack: stack,
}
m.lastError = &record
m.totalErrors++
if len(m.errorHistory) >= 10 {
m.errorHistory = m.errorHistory[1:]
}
m.errorHistory = append(m.errorHistory, record)
}
func (m *Metrics) RecordPanic(r interface{}, stack string) {
m.mu.Lock()
defer m.mu.Unlock()
record := ErrorRecord{
Timestamp: time.Now(),
Error: r,
Stack: stack,
}
m.lastError = &record
m.shrinkPanics++
m.lastPanicTime = time.Now()
if len(m.errorHistory) >= 10 {
m.errorHistory = m.errorHistory[1:]
}
m.errorHistory = append(m.errorHistory, record)
}
func (m *Metrics) TotalPanics() int64 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.shrinkPanics
}
func (m *Metrics) LastPanicTime() time.Time {
m.mu.RLock()
defer m.mu.RUnlock()
return m.lastPanicTime
}
func (m *Metrics) LastError() *ErrorRecord {
m.mu.RLock()
defer m.mu.RUnlock()
if m.lastError == nil {
return nil
}
cp := *m.lastError
return &cp
}
func (m *Metrics) ErrorHistory() []ErrorRecord {
m.mu.RLock()
defer m.mu.RUnlock()
history := make([]ErrorRecord, len(m.errorHistory))
copy(history, m.errorHistory)
return history
}
func (m *Metrics) TotalErrors() int64 {
m.mu.RLock()
defer m.mu.RUnlock()
return m.totalErrors
}
// Reset resets all metrics
func (m *Metrics) Reset() {
m.mu.Lock()
defer m.mu.Unlock()
m.totalShrinks = 0
m.lastShrinkDuration = 0
m.totalItemsProcessed = 0
m.peakSize = 0
m.shrinkPanics = 0
m.lastPanicTime = time.Time{}
m.lastError = nil
m.errorHistory = nil
m.totalErrors = 0
}