@@ -3,6 +3,7 @@ package labstack
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "runtime"
6
7
"strconv"
7
8
"sync"
8
9
"sync/atomic"
21
22
mutex sync.RWMutex
22
23
logger * log.Logger
23
24
25
+ // LabStack Account ID
26
+ AccountID string
27
+
24
28
// LabStack API key
25
29
APIKey string
26
30
39
43
40
44
// CubeRequest defines a request payload to be recorded.
41
45
CubeRequest struct {
46
+ recovered bool
42
47
ID string `json:"id"`
43
48
Time time.Time `json:"time"`
44
49
Host string `json:"host"`
@@ -94,8 +99,8 @@ func (c *Cube) requestsLength() int {
94
99
return len (c .requests )
95
100
}
96
101
97
- // Dispatch dispatches the requests batch.
98
- func (c * Cube ) Dispatch () error {
102
+ // dispatch dispatches the requests batch.
103
+ func (c * Cube ) dispatch () error {
99
104
if len (c .requests ) == 0 {
100
105
return nil
101
106
}
@@ -141,6 +146,25 @@ func (c *Cube) Start(r *http.Request, w http.ResponseWriter) (request *CubeReque
141
146
return
142
147
}
143
148
149
+ // Recover handles a panic
150
+ func (c * Cube ) Recover (cr * CubeRequest ) {
151
+ var err error
152
+
153
+ if r := recover (); r != nil {
154
+ switch r := r .(type ) {
155
+ case error :
156
+ err = r
157
+ default :
158
+ err = fmt .Errorf ("%v" , r )
159
+ }
160
+ stack := make ([]byte , 4 << 10 ) // 4 KB
161
+ length := runtime .Stack (stack , false )
162
+ cr .Error = err .Error ()
163
+ cr .StackTrace = string (stack [:length ])
164
+ cr .recovered = true
165
+ }
166
+ }
167
+
144
168
// Stop stops recording an HTTP request.
145
169
func (c * Cube ) Stop (r * CubeRequest , status int , size int64 ) {
146
170
atomic .AddInt64 (& c .activeRequests , - 1 )
@@ -149,9 +173,9 @@ func (c *Cube) Stop(r *CubeRequest, status int, size int64) {
149
173
r .Latency = int64 (time .Now ().Sub (r .Time ))
150
174
151
175
// Dispatch batch
152
- if c .requestsLength () >= c .BatchSize || status >= 500 && status < 600 {
176
+ if c .requestsLength () >= c .BatchSize || r . recovered {
153
177
go func () {
154
- if err := c .Dispatch (); err != nil {
178
+ if err := c .dispatch (); err != nil {
155
179
c .logger .Error (err )
156
180
}
157
181
c .resetRequests ()
0 commit comments