-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathmetrics.go
More file actions
122 lines (109 loc) · 3.25 KB
/
Copy pathmetrics.go
File metadata and controls
122 lines (109 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package boxlite
/*
#include "bridge.h"
*/
import "C"
import (
"context"
"runtime/cgo"
)
// RuntimeMetrics holds aggregate runtime metrics.
type RuntimeMetrics struct {
BoxesCreatedTotal int
BoxesFailedTotal int
RunningBoxes int
TotalCommandsExecuted int
TotalExecErrors int
}
// BoxMetrics holds per-box metrics.
type BoxMetrics struct {
CPUPercent float64
MemoryBytes int64
CommandsExecuted int
ExecErrors int
BytesSent int64
BytesReceived int64
CreateDurationMs int64
BootDurationMs int64
NetworkBytesSent int64
NetworkBytesReceived int64
NetworkTCPConns int
NetworkTCPErrors int
}
// Metrics returns aggregate runtime metrics.
func (r *Runtime) Metrics(ctx context.Context) (*RuntimeMetrics, error) {
r.ensureDrainRunning()
ch := make(chan runtimeMetricsResult, 1)
h := registerHandleForDispatch(cgo.NewHandle(ch))
var cerr C.CBoxliteError
code := C.boxlite_runtime_metrics(r.handle, C.cbRuntimeMetrics(), handleToPtr(h), &cerr)
if code != C.Ok {
deleteHandleForDispatch(h)
return nil, freeError(&cerr)
}
select {
case res := <-ch:
if res.err != nil {
return nil, res.err
}
return res.value, nil
case <-ctx.Done():
drainAndDelete(ch, h, r.closing)
return nil, ctx.Err()
case <-r.closing:
drainAndDelete(ch, h, r.closing)
return nil, ErrRuntimeClosed
}
}
// Metrics returns real-time metrics for this box.
func (b *Box) Metrics(ctx context.Context) (*BoxMetrics, error) {
b.runtime.ensureDrainRunning()
ch := make(chan boxMetricsResult, 1)
h := registerHandleForDispatch(cgo.NewHandle(ch))
var cerr C.CBoxliteError
code := C.boxlite_box_metrics(b.handle, C.cbBoxMetrics(), handleToPtr(h), &cerr)
if code != C.Ok {
deleteHandleForDispatch(h)
return nil, freeError(&cerr)
}
select {
case res := <-ch:
if res.err != nil {
return nil, res.err
}
return res.value, nil
case <-ctx.Done():
drainAndDelete(ch, h, b.runtime.closing)
return nil, ctx.Err()
case <-b.runtime.closing:
drainAndDelete(ch, h, b.runtime.closing)
return nil, ErrRuntimeClosed
}
}
// cBoxMetricsToGo converts the C struct to its Go counterpart.
func cBoxMetricsToGo(cm *C.CBoxMetrics) BoxMetrics {
return BoxMetrics{
CPUPercent: float64(cm.cpu_percent),
MemoryBytes: int64(cm.memory_bytes),
CommandsExecuted: int(cm.commands_executed),
ExecErrors: int(cm.exec_errors),
BytesSent: int64(cm.bytes_sent),
BytesReceived: int64(cm.bytes_received),
CreateDurationMs: int64(cm.create_duration_ms),
BootDurationMs: int64(cm.boot_duration_ms),
NetworkBytesSent: int64(cm.network_bytes_sent),
NetworkBytesReceived: int64(cm.network_bytes_received),
NetworkTCPConns: int(cm.network_tcp_connections),
NetworkTCPErrors: int(cm.network_tcp_errors),
}
}
// cRuntimeMetricsToGo converts the C struct to its Go counterpart.
func cRuntimeMetricsToGo(cm *C.CRuntimeMetrics) RuntimeMetrics {
return RuntimeMetrics{
BoxesCreatedTotal: int(cm.boxes_created_total),
BoxesFailedTotal: int(cm.boxes_failed_total),
RunningBoxes: int(cm.num_running_boxes),
TotalCommandsExecuted: int(cm.total_commands_executed),
TotalExecErrors: int(cm.total_exec_errors),
}
}