Skip to content

Commit 628f896

Browse files
committed
Basic pulse schema
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent fce2781 commit 628f896

File tree

4 files changed

+154
-49
lines changed

4 files changed

+154
-49
lines changed

Gopkg.lock

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@
4848
[prune]
4949
go-tests = true
5050
unused-packages = true
51+
52+
[[constraint]]
53+
name = "github.com/go-errors/errors"
54+
version = "1.0.1"

pulse/event.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package pulse
2+
3+
import "github.com/go-errors/errors"
4+
5+
type (
6+
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"`
11+
}
12+
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+
}
19+
20+
severity string
21+
22+
User struct {
23+
ID string `json:"id"`
24+
}
25+
26+
Request struct {
27+
}
28+
29+
Data map[string]interface{}
30+
31+
Exception struct {
32+
Class string `json:"class"`
33+
Message string `json:"message"`
34+
StackTrace []*StackFrame `json:"stack_trace"`
35+
}
36+
37+
StackFrame struct {
38+
File string `json:"file"`
39+
Line int `json:"line"`
40+
Column int `json:"column"`
41+
Function string `json:"function"`
42+
Code map[int]string `json:"code"`
43+
}
44+
)
45+
46+
func newEvent(err *errors.Error, ctx *Context) (e *Event) {
47+
e = &Event{
48+
Context: ctx,
49+
App: p.App,
50+
Device: p.Device,
51+
Exception: &Exception{
52+
Class: err.TypeName(),
53+
Message: err.Error(),
54+
StackTrace: make([]*StackFrame, len(err.StackFrames())),
55+
},
56+
}
57+
58+
// Defaults
59+
if e.Severity == "" {
60+
e.Severity = SeverityWarn
61+
}
62+
63+
for i, f := range err.StackFrames() {
64+
e.Exception.StackTrace[i] = &StackFrame{
65+
File: f.File,
66+
Line: f.LineNumber,
67+
Function: f.Name,
68+
}
69+
}
70+
71+
return
72+
}

pulse/pulse.go

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package pulse
22

33
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/go-errors/errors"
48
"github.com/go-resty/resty"
59
"github.com/labstack/gommon/log"
610
)
@@ -14,88 +18,108 @@ type (
1418
}
1519

1620
Options struct {
21+
App *App
1722
Device *Device
1823
}
1924

20-
User struct {
25+
App struct {
26+
Version string
2127
}
2228

2329
Device struct {
30+
Hostname string
2431
}
32+
)
2533

26-
Data struct {
27-
}
28-
29-
Context struct {
30-
user *User
31-
data *Data
32-
}
33-
34-
// APIError struct {
35-
// Code int `json:"code"`
36-
// Message string `json:"message"`
37-
// }
34+
var (
35+
p *Pulse
3836
)
3937

38+
// Severity
4039
var (
41-
global *Pulse
40+
SeverityInfo = severity("info")
41+
SeverityWarn = severity("warn")
42+
SeverityError = severity("error")
4243
)
4344

4445
func Register(apiKey string) {
4546
RegisterWithOptions(apiKey, Options{})
4647
}
4748

4849
func RegisterWithOptions(apiKey string, options Options) {
49-
global = &Pulse{
50+
p = &Pulse{
5051
client: resty.New().
5152
SetHostURL("https://api.labstack.com").
5253
SetAuthToken(apiKey).
5354
SetHeader("User-Agent", "labstack/pulse"),
5455
logger: log.New("pulse"),
5556
}
56-
global.Options = options
57+
p.Options = options
5758

5859
// Defaults
60+
if p.Device == nil {
61+
p.Device = new(Device)
62+
}
63+
if p.Device.Hostname == "" {
64+
p.Device.Hostname, _ = os.Hostname()
65+
}
5966
}
6067

61-
func (p *Pulse) Report() *Context {
62-
return new(Context)
68+
func (p *Pulse) dispatch(err *errors.Error, ctx *Context) {
69+
fmt.Println(err.ErrorStack())
70+
// event := newEvent(err, ctx)
71+
// b, _ := json.MarshalIndent(event, "", " ")
72+
// fmt.Printf("%s", b)
73+
// for _, f := range e.StackFrames() {
74+
// fmt.Println(f.File, f.LineNumber, f.Name)
75+
// }
76+
// res, err := p.client.R().
77+
// SetBody(event).
78+
// // SetError(err).
79+
// Post("/pulse")
80+
// if err != nil {
81+
// p.logger.Error(err)
82+
// return
83+
// }
84+
// if res.StatusCode() < 200 || res.StatusCode() >= 300 {
85+
// p.logger.Error(res.Body())
86+
// }
6387
}
6488

65-
func (p *Pulse) AutoReport() {
89+
func Report(err error) {
90+
ReportWithContext(err, new(Context))
91+
}
92+
93+
func ReportWithContext(err error, ctx *Context) {
94+
if ee, ok := err.(*errors.Error); ok {
95+
p.dispatch(ee, ctx)
96+
} else {
97+
p.dispatch(errors.Wrap(err, 1), ctx)
98+
}
6699
}
67100

68-
func (p *Pulse) Recover() {
101+
func AutoReport() {
102+
if err := recover(); err != nil {
103+
ReportWithContext(errors.Wrap(err, 1), new(Context))
104+
panic(err)
105+
}
69106
}
70107

71-
func (c *Context) SetUser(u *User) *Context {
72-
c.user = u
73-
return c
108+
func AutoReportWithContext(ctx *Context) {
109+
if err := recover(); err != nil {
110+
ReportWithContext(errors.Wrap(err, 1), ctx)
111+
panic(err)
112+
}
74113
}
75114

76-
func (c *Context) SetData(d *Data) *Context {
77-
c.data = d
78-
return c
115+
func Recover() {
116+
if err := recover(); err != nil {
117+
ReportWithContext(errors.Wrap(err, 1), new(Context))
118+
}
79119
}
80120

81-
// // Dispatch dispatches the requests batch.
82-
// func (c *Cube) Dispatch() {
83-
// if len(c.requests) == 0 {
84-
// return
85-
// }
86-
87-
// // err := new(APIError)
88-
// res, err := c.client.R().
89-
// SetBody(c.readRequests()).
90-
// // SetError(err).
91-
// Post("/cube")
92-
// if err != nil {
93-
// c.logger.Error(err)
94-
// return
95-
// }
96-
// if res.StatusCode() < 200 || res.StatusCode() >= 300 {
97-
// c.logger.Error(res.Body())
98-
// }
99-
100-
// c.resetRequests()
101-
// }
121+
func RecoverWithContext(ctx *Context) {
122+
if err := recover(); err != nil {
123+
ReportWithContext(errors.Wrap(err, 1), ctx)
124+
}
125+
}

0 commit comments

Comments
 (0)