Skip to content

Commit af7e9c6

Browse files
committed
Added uptime, cpu & mem
Signed-off-by: Vishal Rana <[email protected]>
1 parent 806693b commit af7e9c6

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package labstack
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/go-resty/resty"
78
"github.com/labstack/gommon/log"
9+
"github.com/shirou/gopsutil/process"
810
)
911

1012
type (
@@ -46,8 +48,10 @@ func (c *Client) error(r *resty.Response) bool {
4648
}
4749

4850
func (c *Client) Cube() *Cube {
51+
p, _ := process.NewProcess(int32(os.Getpid()))
4952
return &Cube{
50-
client: c,
53+
process: p,
54+
client: c,
5155
}
5256
}
5357

cube.go

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import (
88
"sync"
99
"sync/atomic"
1010
"time"
11+
12+
"github.com/shirou/gopsutil/process"
1113
)
1214

1315
type (
1416
// Cube defines the LabStack cube service.
1517
Cube struct {
18+
process *process.Process
1619
client *Client
1720
requests []*Request
1821
activeRequests int64
@@ -43,25 +46,27 @@ type (
4346

4447
// Request defines a request payload to be corded.
4548
Request struct {
46-
ID string `json:"id"`
47-
Time time.Time `json:"time"`
48-
Node string `json:"node"`
49-
Group string `json:"group"`
50-
Tags []string `json:"tags,omitempty"`
51-
Host string `json:"host"`
52-
Path string `json:"path"`
53-
Method string `json:"method"`
54-
Status int `json:"status"`
55-
BytesIn int64 `json:"bytes_in"`
56-
BytesOut int64 `json:"bytes_out"`
57-
Latency int64 `json:"latency"`
58-
ClientID string `json:"client_id"`
59-
RemoteIP string `json:"remote_ip"`
60-
UserAgent string `json:"user_agent"`
61-
Active int64 `json:"active"`
62-
// TODO: CPU, Uptime, Memory
63-
Error string `json:"error"`
64-
StackTrace string `json:"stack_trace"`
49+
ID string `json:"id"`
50+
Time time.Time `json:"time"`
51+
Node string `json:"node"`
52+
Group string `json:"group"`
53+
Tags []string `json:"tags,omitempty"`
54+
Host string `json:"host"`
55+
Path string `json:"path"`
56+
Method string `json:"method"`
57+
Status int `json:"status"`
58+
BytesIn int64 `json:"bytes_in"`
59+
BytesOut int64 `json:"bytes_out"`
60+
Latency int64 `json:"latency"`
61+
ClientID string `json:"client_id"`
62+
RemoteIP string `json:"remote_ip"`
63+
UserAgent string `json:"user_agent"`
64+
Active int64 `json:"active"`
65+
Error string `json:"error"`
66+
StackTrace string `json:"stack_trace"`
67+
Uptime int64 `json:"uptime"`
68+
CPUPercent float32 `json:"cpu_percent"`
69+
MemoryPercent float32 `json:"memory_percent"`
6570
}
6671
)
6772

@@ -131,17 +136,23 @@ func (c *Cube) Start(r *http.Request, w http.ResponseWriter) (req *Request) {
131136
atomic.AddInt64(&c.started, 1)
132137
}
133138

139+
start, _ := c.process.CreateTime()
140+
cpu, _ := c.process.CPUPercent()
141+
mem, _ := c.process.MemoryPercent()
134142
req = &Request{
135-
ID: RequestID(r, w),
136-
Time: time.Now(),
137-
Node: c.Node,
138-
Group: c.Group,
139-
Tags: c.Tags,
140-
Host: r.Host,
141-
Path: r.URL.Path,
142-
Method: r.Method,
143-
UserAgent: r.UserAgent(),
144-
RemoteIP: RealIP(r),
143+
ID: RequestID(r, w),
144+
Time: time.Now(),
145+
Node: c.Node,
146+
Group: c.Group,
147+
Tags: c.Tags,
148+
Host: r.Host,
149+
Path: r.URL.Path,
150+
Method: r.Method,
151+
UserAgent: r.UserAgent(),
152+
RemoteIP: RealIP(r),
153+
Uptime: time.Now().Unix() - start,
154+
CPUPercent: float32(cpu),
155+
MemoryPercent: mem,
145156
}
146157
req.ClientID = req.RemoteIP
147158
atomic.AddInt64(&c.activeRequests, 1)

0 commit comments

Comments
 (0)