This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
/
dyno_msg.go
96 lines (88 loc) · 2.75 KB
/
dyno_msg.go
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
package main
import (
"bytes"
"strconv"
"strings"
)
var (
keySource = []byte("source")
keyDyno = []byte("dyno")
keyMemoryTotal = []byte("memory_total")
keyMemoryRSS = []byte("memory_rss")
keyMemoryCache = []byte("memory_cache")
keyMemorySwap = []byte("memory_swap")
keyMemoryPgpgin = []byte("memory_pgpgin")
keyMemoryPgpgout = []byte("memory_pgpgout")
keyLoadAvg1Min = []byte("load_avg_1m")
keyLoadAvg5Min = []byte("load_avg_5m")
keyLoadAvg15Min = []byte("load_avg_15m")
dynoMemMsgSentinel = []byte("sample#memory_total")
dynoLoadMsgSentinel = []byte("sample#load_avg_1m")
dynoErrorSentinel = []byte("Error R")
)
type dynoError struct {
Code int
}
func parseBytesToDynoError(msg []byte) (dynoError, error) {
de := dynoError{}
byteCode := msg[len(dynoErrorSentinel) : len(dynoErrorSentinel)+2]
code, err := strconv.Atoi(string(byteCode))
if err != nil {
return de, err
}
de.Code = code
return de, nil
}
type dynoMemMsg struct {
Source string
Dyno string
MemoryTotal float64
MemoryRSS float64
MemoryCache float64
MemorySwap float64
MemoryPgpgin int
MemoryPgpgout int
}
func (dm *dynoMemMsg) HandleLogfmt(key, val []byte) error {
switch {
case bytes.Equal(key, keySource):
dm.Source = string(val)
case bytes.Equal(key, keyDyno):
dm.Dyno = string(val)
case bytes.HasSuffix(key, keyMemoryTotal):
dm.MemoryTotal, _ = strconv.ParseFloat(strings.TrimSuffix(string(val), "MB"), 64)
case bytes.HasSuffix(key, keyMemoryRSS):
dm.MemoryRSS, _ = strconv.ParseFloat(strings.TrimSuffix(string(val), "MB"), 64)
case bytes.HasSuffix(key, keyMemoryCache):
dm.MemoryCache, _ = strconv.ParseFloat(strings.TrimSuffix(string(val), "MB"), 64)
case bytes.HasSuffix(key, keyMemorySwap):
dm.MemorySwap, _ = strconv.ParseFloat(strings.TrimSuffix(string(val), "MB"), 64)
case bytes.HasSuffix(key, keyMemoryPgpgin):
dm.MemoryPgpgin, _ = strconv.Atoi(strings.TrimSuffix(string(val), "pages"))
case bytes.HasSuffix(key, keyMemoryPgpgout):
dm.MemoryPgpgout, _ = strconv.Atoi(strings.TrimSuffix(string(val), "pages"))
}
return nil
}
type dynoLoadMsg struct {
Source string
Dyno string
LoadAvg1Min float64
LoadAvg5Min float64
LoadAvg15Min float64
}
func (dm *dynoLoadMsg) HandleLogfmt(key, val []byte) error {
switch {
case bytes.Equal(key, keySource):
dm.Source = string(val)
case bytes.Equal(key, keyDyno):
dm.Dyno = string(val)
case bytes.HasSuffix(key, keyLoadAvg1Min):
dm.LoadAvg1Min, _ = strconv.ParseFloat(string(val), 64)
case bytes.HasSuffix(key, keyLoadAvg5Min):
dm.LoadAvg5Min, _ = strconv.ParseFloat(string(val), 64)
case bytes.HasSuffix(key, keyLoadAvg15Min):
dm.LoadAvg15Min, _ = strconv.ParseFloat(string(val), 64)
}
return nil
}