-
Notifications
You must be signed in to change notification settings - Fork 7
/
drain.go
349 lines (308 loc) · 8.92 KB
/
drain.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package drain
import (
"fmt"
"math"
"strconv"
"strings"
"unicode"
"github.com/hashicorp/golang-lru/simplelru"
)
type Config struct {
maxNodeDepth int
LogClusterDepth int
SimTh float64
MaxChildren int
ExtraDelimiters []string
MaxClusters int
ParamString string
}
type LogCluster struct {
logTemplateTokens []string
id int
size int
}
func (c *LogCluster) getTemplate() string {
return strings.Join(c.logTemplateTokens, " ")
}
func (c *LogCluster) String() string {
return fmt.Sprintf("id={%d} : size={%d} : %s", c.id, c.size, c.getTemplate())
}
func createLogClusterCache(maxSize int) *LogClusterCache {
if maxSize == 0 {
maxSize = math.MaxInt
}
cache, _ := simplelru.NewLRU(maxSize, nil)
return &LogClusterCache{
cache: cache,
}
}
type LogClusterCache struct {
cache simplelru.LRUCache
}
func (c *LogClusterCache) Values() []*LogCluster {
values := make([]*LogCluster, 0)
for _, key := range c.cache.Keys() {
if value, ok := c.cache.Peek(key); ok {
values = append(values, value.(*LogCluster))
}
}
return values
}
func (c *LogClusterCache) Set(key int, cluster *LogCluster) {
c.cache.Add(key, cluster)
}
func (c *LogClusterCache) Get(key int) *LogCluster {
cluster, ok := c.cache.Get(key)
if !ok {
return nil
}
return cluster.(*LogCluster)
}
func createNode() *Node {
return &Node{
keyToChildNode: make(map[string]*Node),
clusterIDs: make([]int, 0),
}
}
type Node struct {
keyToChildNode map[string]*Node
clusterIDs []int
}
func DefaultConfig() *Config {
return &Config{
LogClusterDepth: 4,
SimTh: 0.4,
MaxChildren: 100,
ParamString: "<*>",
}
}
func New(config *Config) *Drain {
if config.LogClusterDepth < 3 {
panic("depth argument must be at least 3")
}
config.maxNodeDepth = config.LogClusterDepth - 2
d := &Drain{
config: config,
rootNode: createNode(),
idToCluster: createLogClusterCache(config.MaxClusters),
}
return d
}
type Drain struct {
config *Config
rootNode *Node
idToCluster *LogClusterCache
clustersCounter int
}
func (d *Drain) Clusters() []*LogCluster {
return d.idToCluster.Values()
}
func (d *Drain) Train(content string) *LogCluster {
contentTokens := d.getContentAsTokens(content)
matchCluster := d.treeSearch(d.rootNode, contentTokens, d.config.SimTh, false)
// Match no existing log cluster
if matchCluster == nil {
d.clustersCounter++
clusterID := d.clustersCounter
matchCluster = &LogCluster{
logTemplateTokens: contentTokens,
id: clusterID,
size: 1,
}
d.idToCluster.Set(clusterID, matchCluster)
d.addSeqToPrefixTree(d.rootNode, matchCluster)
} else {
newTemplateTokens := d.createTemplate(contentTokens, matchCluster.logTemplateTokens)
matchCluster.logTemplateTokens = newTemplateTokens
matchCluster.size++
// Touch cluster to update its state in the cache.
d.idToCluster.Get(matchCluster.id)
}
return matchCluster
}
// Match against an already existing cluster. Match shall be perfect (sim_th=1.0). New cluster will not be created as a result of this call, nor any cluster modifications.
func (d *Drain) Match(content string) *LogCluster {
contentTokens := d.getContentAsTokens(content)
matchCluster := d.treeSearch(d.rootNode, contentTokens, 1.0, true)
return matchCluster
}
func (d *Drain) getContentAsTokens(content string) []string {
content = strings.TrimSpace(content)
for _, extraDelimiter := range d.config.ExtraDelimiters {
content = strings.Replace(content, extraDelimiter, " ", -1)
}
return strings.Split(content, " ")
}
func (d *Drain) treeSearch(rootNode *Node, tokens []string, simTh float64, includeParams bool) *LogCluster {
tokenCount := len(tokens)
// at first level, children are grouped by token (word) count
curNode, ok := rootNode.keyToChildNode[strconv.Itoa(tokenCount)]
// no template with same token count yet
if !ok {
return nil
}
// handle case of empty log string - return the single cluster in that group
if tokenCount == 0 {
return d.idToCluster.Get(curNode.clusterIDs[0])
}
// find the leaf node for this log - a path of nodes matching the first N tokens (N=tree depth)
curNodeDepth := 1
for _, token := range tokens {
// at max depth
if curNodeDepth >= d.config.maxNodeDepth {
break
}
// this is last token
if curNodeDepth == tokenCount {
break
}
keyToChildNode := curNode.keyToChildNode
curNode, ok = keyToChildNode[token]
if !ok { // no exact next token exist, try wildcard node
curNode, ok = keyToChildNode[d.config.ParamString]
}
if !ok { // no wildcard node exist
return nil
}
curNodeDepth++
}
// get best match among all clusters with same prefix, or None if no match is above sim_th
cluster := d.fastMatch(curNode.clusterIDs, tokens, simTh, includeParams)
return cluster
}
// fastMatch Find the best match for a log message (represented as tokens) versus a list of clusters
func (d *Drain) fastMatch(clusterIDs []int, tokens []string, simTh float64, includeParams bool) *LogCluster {
var matchCluster, maxCluster *LogCluster
maxSim := -1.0
maxParamCount := -1
for _, clusterID := range clusterIDs {
// Try to retrieve cluster from cache with bypassing eviction
// algorithm as we are only testing candidates for a match.
cluster := d.idToCluster.Get(clusterID)
if cluster == nil {
continue
}
curSim, paramCount := d.getSeqDistance(cluster.logTemplateTokens, tokens, includeParams)
if curSim > maxSim || (curSim == maxSim && paramCount > maxParamCount) {
maxSim = curSim
maxParamCount = paramCount
maxCluster = cluster
}
}
if maxSim >= simTh {
matchCluster = maxCluster
}
return matchCluster
}
func (d *Drain) getSeqDistance(seq1, seq2 []string, includeParams bool) (float64, int) {
if len(seq1) != len(seq2) {
panic("seq1 seq2 be of same length")
}
simTokens := 0
paramCount := 0
for i := range seq1 {
token1 := seq1[i]
token2 := seq2[i]
if token1 == d.config.ParamString {
paramCount++
} else if token1 == token2 {
simTokens++
}
}
if includeParams {
simTokens += paramCount
}
retVal := float64(simTokens) / float64(len(seq1))
return retVal, paramCount
}
func (d *Drain) addSeqToPrefixTree(rootNode *Node, cluster *LogCluster) {
tokenCount := len(cluster.logTemplateTokens)
tokenCountStr := strconv.Itoa(tokenCount)
firstLayerNode, ok := rootNode.keyToChildNode[tokenCountStr]
if !ok {
firstLayerNode = createNode()
rootNode.keyToChildNode[tokenCountStr] = firstLayerNode
}
curNode := firstLayerNode
// handle case of empty log string
if tokenCount == 0 {
curNode.clusterIDs = append(curNode.clusterIDs, cluster.id)
return
}
currentDepth := 1
for _, token := range cluster.logTemplateTokens {
// if at max depth or this is last token in template - add current log cluster to the leaf node
if (currentDepth >= d.config.maxNodeDepth) || currentDepth >= tokenCount {
// clean up stale clusters before adding a new one.
newClusterIDs := make([]int, 0, len(curNode.clusterIDs))
for _, clusterID := range curNode.clusterIDs {
if d.idToCluster.Get(clusterID) != nil {
newClusterIDs = append(newClusterIDs, clusterID)
}
}
newClusterIDs = append(newClusterIDs, cluster.id)
curNode.clusterIDs = newClusterIDs
break
}
// if token not matched in this layer of existing tree.
if _, ok = curNode.keyToChildNode[token]; !ok {
// if token not matched in this layer of existing tree.
if !d.hasNumbers(token) {
if _, ok = curNode.keyToChildNode[d.config.ParamString]; ok {
if len(curNode.keyToChildNode) < d.config.MaxChildren {
newNode := createNode()
curNode.keyToChildNode[token] = newNode
curNode = newNode
} else {
curNode = curNode.keyToChildNode[d.config.ParamString]
}
} else {
if len(curNode.keyToChildNode)+1 < d.config.MaxChildren {
newNode := createNode()
curNode.keyToChildNode[token] = newNode
curNode = newNode
} else if len(curNode.keyToChildNode)+1 == d.config.MaxChildren {
newNode := createNode()
curNode.keyToChildNode[d.config.ParamString] = newNode
curNode = newNode
} else {
curNode = curNode.keyToChildNode[d.config.ParamString]
}
}
} else {
if _, ok = curNode.keyToChildNode[d.config.ParamString]; !ok {
newNode := createNode()
curNode.keyToChildNode[d.config.ParamString] = newNode
curNode = newNode
} else {
curNode = curNode.keyToChildNode[d.config.ParamString]
}
}
} else {
// if the token is matched
curNode = curNode.keyToChildNode[token]
}
currentDepth++
}
}
func (d *Drain) hasNumbers(s string) bool {
for _, c := range s {
if unicode.IsNumber(c) {
return true
}
}
return false
}
func (d *Drain) createTemplate(seq1, seq2 []string) []string {
if len(seq1) != len(seq2) {
panic("seq1 seq2 be of same length")
}
retVal := make([]string, len(seq2))
copy(retVal, seq2)
for i := range seq1 {
if seq1[i] != seq2[i] {
retVal[i] = d.config.ParamString
}
}
return retVal
}