Skip to content

Commit 32cbcd7

Browse files
committed
Pulse to bolt
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 628f896 commit 32cbcd7

File tree

4 files changed

+147
-144
lines changed

4 files changed

+147
-144
lines changed

bolt/bolt.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package bolt
2+
3+
import (
4+
"os"
5+
6+
"github.com/go-errors/errors"
7+
"github.com/go-resty/resty"
8+
"github.com/labstack/gommon/log"
9+
)
10+
11+
type (
12+
Bolt struct {
13+
Options
14+
// mutex sync.RWMutex
15+
client *resty.Client
16+
logger *log.Logger
17+
}
18+
19+
Options struct {
20+
App *App
21+
Device *Device
22+
}
23+
24+
App struct {
25+
Version string `json:"version"`
26+
}
27+
28+
Device struct {
29+
Hostname string `json:"hostname"`
30+
}
31+
)
32+
33+
var (
34+
b *Bolt
35+
)
36+
37+
// Severity
38+
var (
39+
SeverityInfo = Severity("info")
40+
SeverityWarn = Severity("warn")
41+
SeverityError = Severity("error")
42+
)
43+
44+
func Register(apiKey string) {
45+
RegisterWithOptions(apiKey, Options{})
46+
}
47+
48+
func RegisterWithOptions(apiKey string, options Options) {
49+
b = &Bolt{
50+
client: resty.New().
51+
SetHostURL("https://api.labstack.com").
52+
SetAuthToken(apiKey).
53+
SetHeader("User-Agent", "labstack/bolt"),
54+
logger: log.New("bolt"),
55+
}
56+
b.Options = options
57+
58+
// Defaults
59+
if b.Device == nil {
60+
b.Device = new(Device)
61+
}
62+
if b.Device.Hostname == "" {
63+
b.Device.Hostname, _ = os.Hostname()
64+
}
65+
}
66+
67+
func (b *Bolt) dispatch(err *errors.Error, data *Data) {
68+
// fmt.Println(err.ErrorStack())
69+
event := newEvent(err, data)
70+
r, e := b.client.R().
71+
SetBody(event).
72+
// SetError(err).
73+
Post("/bolt")
74+
if e != nil {
75+
b.logger.Error(err)
76+
return
77+
}
78+
if r.StatusCode() < 200 || r.StatusCode() >= 300 {
79+
b.logger.Errorf("Failed to send error report: %s", r.Body())
80+
}
81+
}
82+
83+
func Report(err error) {
84+
ReportWithData(err, new(Data))
85+
}
86+
87+
func ReportWithData(err error, data *Data) {
88+
if ee, ok := err.(*errors.Error); ok {
89+
b.dispatch(ee, data)
90+
} else {
91+
b.dispatch(errors.Wrap(err, 1), data)
92+
}
93+
}
94+
95+
func AutoReport() {
96+
if err := recover(); err != nil {
97+
ReportWithData(errors.Wrap(err, 1), new(Data))
98+
panic(err)
99+
}
100+
}
101+
102+
func AutoReportWithData(data *Data) {
103+
if err := recover(); err != nil {
104+
ReportWithData(errors.Wrap(err, 1), data)
105+
panic(err)
106+
}
107+
}
108+
109+
func Recover() {
110+
if err := recover(); err != nil {
111+
ReportWithData(errors.Wrap(err, 1), new(Data))
112+
}
113+
}
114+
115+
func RecoverWithData(data *Data) {
116+
if err := recover(); err != nil {
117+
ReportWithData(errors.Wrap(err, 1), data)
118+
}
119+
}

pulse/event.go renamed to bolt/event.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
package pulse
1+
package bolt
22

3-
import "github.com/go-errors/errors"
3+
import (
4+
"path/filepath"
5+
6+
"github.com/go-errors/errors"
7+
labstack "github.com/labstack/labstack-go"
8+
)
49

510
type (
611
Event struct {
7-
*Context
8-
App *App `json:"app" db:"app"`
9-
Device *Device `json:"device" db:"device"`
10-
Exception *Exception `json:"exception" db:"exception"`
12+
*Data
13+
App *App `json:"app"`
14+
Device *Device `json:"device"`
15+
Exception *Exception `json:"exception"`
1116
}
1217

13-
Context struct {
14-
Severity severity `json:"severity" db:"severity"`
15-
User *User `json:"user" db:"user"`
16-
Request *Request `json:"request" db:"request"`
17-
Data Data `json:"data" db:"data"`
18+
Data struct {
19+
Severity Severity `json:"severity"`
20+
User *User `json:"user"`
21+
Request *Request `json:"request"`
22+
Tags []string `json:"tags"`
23+
Extra labstack.Map `json:"extra"`
1824
}
1925

20-
severity string
26+
Severity string
2127

2228
User struct {
2329
ID string `json:"id"`
@@ -26,8 +32,6 @@ type (
2632
Request struct {
2733
}
2834

29-
Data map[string]interface{}
30-
3135
Exception struct {
3236
Class string `json:"class"`
3337
Message string `json:"message"`
@@ -43,11 +47,11 @@ type (
4347
}
4448
)
4549

46-
func newEvent(err *errors.Error, ctx *Context) (e *Event) {
50+
func newEvent(err *errors.Error, data *Data) (e *Event) {
4751
e = &Event{
48-
Context: ctx,
49-
App: p.App,
50-
Device: p.Device,
52+
Data: data,
53+
App: b.App,
54+
Device: b.Device,
5155
Exception: &Exception{
5256
Class: err.TypeName(),
5357
Message: err.Error(),
@@ -62,7 +66,7 @@ func newEvent(err *errors.Error, ctx *Context) (e *Event) {
6266

6367
for i, f := range err.StackFrames() {
6468
e.Exception.StackTrace[i] = &StackFrame{
65-
File: f.File,
69+
File: filepath.Base(f.File),
6670
Line: f.LineNumber,
6771
Function: f.Name,
6872
}

labstack.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package labstack
2+
3+
type (
4+
Map map[string]interface{}
5+
)

pulse/pulse.go

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)