-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
123 lines (106 loc) · 2.43 KB
/
read.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
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
123
package main
import (
"sort"
"time"
// "github.com/coreos/go-systemd/v22/journal"
"github.com/coreos/go-systemd/v22/sdjournal"
"github.com/google/uuid"
)
// A small wrapper around sdjournal so we can define our own functions on it.
type JournalEntry struct {
E *sdjournal.JournalEntry
}
func aggregateIdenticalMessages(msg string, hours int) LogEntry {
j, err := sdjournal.NewJournal()
if err != nil {
panic(err)
}
defer j.Close()
err = j.AddMatch("SENTRY_MESSAGE_KEY=" + msg)
if err != nil {
panic(err)
}
logStart := time.Now().Add(-time.Duration(hours) * time.Hour)
// dur := time.Duration(hours) * time.Hour
// timeNow := time.Now().Sub(dur)
err = j.SeekRealtimeUsec(uint64(logStart.UnixMicro() / 1000))
if err != nil {
panic(err)
}
entries := LogEntry{}
for {
x, err := j.Next()
if x == 0 {
return entries
}
if err != nil {
panic(err)
}
entry, err := j.GetEntry()
entries.Entries = append(entries.Entries, &JournalEntry{E: entry})
}
return entries
}
func discoverAllMessages(hours int) []*JournalEntry {
j, err := sdjournal.NewJournal()
if err != nil {
panic(err)
}
defer j.Close()
err = j.AddMatch("SYSLOG_IDENTIFIER=sentry")
if err != nil {
panic(err)
}
logStart := time.Now().Add(-time.Duration(hours) * time.Hour)
// dur := time.Duration(hours) * time.Hour
// timeNow := time.Now().Sub(dur)
err = j.SeekRealtimeUsec(uint64(logStart.UnixMicro() / 1000))
if err != nil {
panic(err)
}
entries := []*JournalEntry{}
for {
x, err := j.Next()
if x == 0 {
return entries
}
if err != nil {
panic(err)
}
entry, err := j.GetEntry()
entries = append(entries, &JournalEntry{E: entry})
}
return entries
}
func GroupMain() []LogEntry {
// func main(){
//
entries := discoverAllMessages(1)
grouped := make(map[string]LogEntry)
for _, entry := range entries {
k := entry.E.Fields["SENTRY_MESSAGE_KEY"]
ref, found := grouped[k]
if !found {
grouped[k] = LogEntry{
LastSeen: time.UnixMicro(int64(entry.E.RealtimeTimestamp)),
Entries: []*JournalEntry{},
Id: uuid.New().String(),
}
}
ref = grouped[k]
ref.Entries = append(ref.Entries, entry)
ut := time.UnixMicro(int64(entry.E.RealtimeTimestamp))
if ut.After(ref.LastSeen) {
ref.LastSeen = ut
}
grouped[k] = ref
}
// get the keys, sorted
keys := make([]LogEntry, 0, len(grouped))
for _, v := range grouped {
keys = append(keys, v)
}
// Sort
sort.Sort(ByAge(keys))
return keys
}