Skip to content

Commit e34b8bc

Browse files
committed
Fixed cube service
Signed-off-by: Vishal Rana <[email protected]>
1 parent 00af94d commit e34b8bc

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ func NewClient(accountID, apiKey string) *Client {
4242
}
4343

4444
// Cube returns the cube service.
45-
func (c *Client) Cube() (cube *Cube) {
45+
func (c *Client) Cube(accountID, apiKey string) (cube *Cube) {
4646
cube = &Cube{
4747
sling: c.sling.Path("/cube"),
4848
logger: c.logger,
49+
AccountID: accountID,
50+
APIKey: apiKey,
4951
BatchSize: 60,
5052
DispatchInterval: 60,
5153
}
5254
cube.resetRequests()
5355
go func() {
5456
d := time.Duration(cube.DispatchInterval) * time.Second
5557
for range time.Tick(d) {
56-
cube.Dispatch()
58+
cube.dispatch()
5759
}
5860
}()
5961
return

cube.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package labstack
33
import (
44
"fmt"
55
"net/http"
6+
"runtime"
67
"strconv"
78
"sync"
89
"sync/atomic"
@@ -21,6 +22,9 @@ type (
2122
mutex sync.RWMutex
2223
logger *log.Logger
2324

25+
// LabStack Account ID
26+
AccountID string
27+
2428
// LabStack API key
2529
APIKey string
2630

@@ -39,6 +43,7 @@ type (
3943

4044
// CubeRequest defines a request payload to be recorded.
4145
CubeRequest struct {
46+
recovered bool
4247
ID string `json:"id"`
4348
Time time.Time `json:"time"`
4449
Host string `json:"host"`
@@ -94,8 +99,8 @@ func (c *Cube) requestsLength() int {
9499
return len(c.requests)
95100
}
96101

97-
// Dispatch dispatches the requests batch.
98-
func (c *Cube) Dispatch() error {
102+
// dispatch dispatches the requests batch.
103+
func (c *Cube) dispatch() error {
99104
if len(c.requests) == 0 {
100105
return nil
101106
}
@@ -141,6 +146,25 @@ func (c *Cube) Start(r *http.Request, w http.ResponseWriter) (request *CubeReque
141146
return
142147
}
143148

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+
144168
// Stop stops recording an HTTP request.
145169
func (c *Cube) Stop(r *CubeRequest, status int, size int64) {
146170
atomic.AddInt64(&c.activeRequests, -1)
@@ -149,9 +173,9 @@ func (c *Cube) Stop(r *CubeRequest, status int, size int64) {
149173
r.Latency = int64(time.Now().Sub(r.Time))
150174

151175
// Dispatch batch
152-
if c.requestsLength() >= c.BatchSize || status >= 500 && status < 600 {
176+
if c.requestsLength() >= c.BatchSize || r.recovered {
153177
go func() {
154-
if err := c.Dispatch(); err != nil {
178+
if err := c.dispatch(); err != nil {
155179
c.logger.Error(err)
156180
}
157181
c.resetRequests()

0 commit comments

Comments
 (0)