Skip to content

Commit fce2781

Browse files
committed
Started on pulse
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 39ee533 commit fce2781

File tree

2 files changed

+114
-12
lines changed

2 files changed

+114
-12
lines changed

cube/cube.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func NewWithOptions(apiKey string, options Options) *Cube {
9393
if c.DispatchInterval == 0 {
9494
c.DispatchInterval = 60
9595
}
96+
c.requests = make([]*Request, 0, c.BatchSize)
9697

9798
// Dispatch daemon
9899
go func() {
@@ -118,19 +119,13 @@ func NewWithOptions(apiKey string, options Options) *Cube {
118119
return c
119120
}
120121

121-
func (c *Cube) resetRequests() {
122-
c.mutex.Lock()
123-
defer c.mutex.Unlock()
124-
c.requests = make([]*Request, 0, c.BatchSize)
125-
}
126-
127-
func (c *Cube) appendRequest(r *Request) {
122+
func (c *Cube) addRequest(r *Request) {
128123
c.mutex.Lock()
129124
defer c.mutex.Unlock()
130125
c.requests = append(c.requests, r)
131126
}
132127

133-
func (c *Cube) listRequests() []*Request {
128+
func (c *Cube) readRequests() []*Request {
134129
c.mutex.Lock()
135130
defer c.mutex.Unlock()
136131
requests := make([]*Request, len(c.requests))
@@ -146,6 +141,12 @@ func (c *Cube) requestsLength() int {
146141
return len(c.requests)
147142
}
148143

144+
func (c *Cube) resetRequests() {
145+
c.mutex.Lock()
146+
defer c.mutex.Unlock()
147+
c.requests = make([]*Request, 0, c.BatchSize)
148+
}
149+
149150
// Dispatch dispatches the requests batch.
150151
func (c *Cube) Dispatch() {
151152
if len(c.requests) == 0 {
@@ -154,7 +155,7 @@ func (c *Cube) Dispatch() {
154155

155156
// err := new(APIError)
156157
res, err := c.client.R().
157-
SetBody(c.listRequests()).
158+
SetBody(c.readRequests()).
158159
// SetError(err).
159160
Post("/cube")
160161
if err != nil {
@@ -172,22 +173,22 @@ func (c *Cube) Dispatch() {
172173
func (c *Cube) Start(r *Request) {
173174
atomic.AddInt64(&c.activeRequests, 1)
174175

175-
r.Time = time.Now().UnixNano() / 1000
176+
r.Time = time.Now().UnixNano() / 1e3
176177
r.Active = c.activeRequests
177178
r.Node = c.Node
178179
r.Uptime = c.uptime
179180
r.CPU = c.cpu
180181
r.Memory = c.memory
181182
r.Tags = c.Tags
182183

183-
c.appendRequest(r)
184+
c.addRequest(r)
184185
}
185186

186187
// Stop stops recording an HTTP request.
187188
func (c *Cube) Stop(r *Request) {
188189
atomic.AddInt64(&c.activeRequests, -1)
189190

190-
r.Latency = time.Now().UnixNano()/1000 - r.Time
191+
r.Latency = time.Now().UnixNano()/1e3 - r.Time
191192

192193
// Dispatch batch
193194
if c.requestsLength() >= c.BatchSize {

pulse/pulse.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package pulse
2+
3+
import (
4+
"github.com/go-resty/resty"
5+
"github.com/labstack/gommon/log"
6+
)
7+
8+
type (
9+
Pulse struct {
10+
Options
11+
// mutex sync.RWMutex
12+
client *resty.Client
13+
logger *log.Logger
14+
}
15+
16+
Options struct {
17+
Device *Device
18+
}
19+
20+
User struct {
21+
}
22+
23+
Device struct {
24+
}
25+
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+
// }
38+
)
39+
40+
var (
41+
global *Pulse
42+
)
43+
44+
func Register(apiKey string) {
45+
RegisterWithOptions(apiKey, Options{})
46+
}
47+
48+
func RegisterWithOptions(apiKey string, options Options) {
49+
global = &Pulse{
50+
client: resty.New().
51+
SetHostURL("https://api.labstack.com").
52+
SetAuthToken(apiKey).
53+
SetHeader("User-Agent", "labstack/pulse"),
54+
logger: log.New("pulse"),
55+
}
56+
global.Options = options
57+
58+
// Defaults
59+
}
60+
61+
func (p *Pulse) Report() *Context {
62+
return new(Context)
63+
}
64+
65+
func (p *Pulse) AutoReport() {
66+
}
67+
68+
func (p *Pulse) Recover() {
69+
}
70+
71+
func (c *Context) SetUser(u *User) *Context {
72+
c.user = u
73+
return c
74+
}
75+
76+
func (c *Context) SetData(d *Data) *Context {
77+
c.data = d
78+
return c
79+
}
80+
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+
// }

0 commit comments

Comments
 (0)