-
Notifications
You must be signed in to change notification settings - Fork 28
/
groupingkey.go
130 lines (118 loc) · 3.58 KB
/
groupingkey.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package modelprocessor
import (
"context"
"crypto/md5"
"encoding/hex"
"hash"
"io"
"github.com/elastic/apm-data/model/modelpb"
)
// SetGroupingKey is a modelpb.BatchProcessor that sets the grouping key for errors
// by hashing their stack frames.
type SetGroupingKey struct{}
// ProcessBatch sets the grouping key for errors.
func (s SetGroupingKey) ProcessBatch(ctx context.Context, b *modelpb.Batch) error {
for _, event := range *b {
if event.Error != nil {
s.processError(ctx, event.Error)
}
}
return nil
}
func (s SetGroupingKey) processError(ctx context.Context, event *modelpb.Error) {
hash := md5.New()
var updated bool
if event.Exception != nil {
if s.hashExceptionTree(event.Exception, hash, s.hashExceptionType) {
updated = true
}
}
if event.Log != nil {
if s.maybeWriteString(event.Log.ParamMessage, hash) {
updated = true
}
}
var haveExceptionStacktrace bool
if event.Exception != nil {
haveExceptionStacktrace = s.hashExceptionTree(event.Exception, hash, s.hashExceptionStacktrace)
updated = updated || haveExceptionStacktrace
}
if !haveExceptionStacktrace && event.Log != nil {
if s.hashStacktrace(event.Log.Stacktrace, hash) {
updated = true
}
}
if !updated {
if event.Exception != nil {
updated = s.hashExceptionTree(event.Exception, hash, s.hashExceptionMessage)
}
if !updated && event.Log != nil {
s.maybeWriteString(event.Log.Message, hash)
}
}
event.GroupingKey = hex.EncodeToString(hash.Sum(nil))
}
func (s SetGroupingKey) hashExceptionTree(e *modelpb.Exception, out hash.Hash, f func(*modelpb.Exception, hash.Hash) bool) bool {
updated := f(e, out)
for _, cause := range e.Cause {
if s.hashExceptionTree(cause, out, f) {
updated = true
}
}
return updated
}
func (s SetGroupingKey) hashExceptionType(e *modelpb.Exception, out hash.Hash) bool {
return s.maybeWriteString(e.Type, out)
}
func (s SetGroupingKey) hashExceptionMessage(e *modelpb.Exception, out hash.Hash) bool {
return s.maybeWriteString(e.Message, out)
}
func (s SetGroupingKey) hashExceptionStacktrace(e *modelpb.Exception, out hash.Hash) bool {
return s.hashStacktrace(e.Stacktrace, out)
}
func (s SetGroupingKey) hashStacktrace(stacktrace []*modelpb.StacktraceFrame, out hash.Hash) bool {
var updated bool
for _, frame := range stacktrace {
if frame.ExcludeFromGrouping {
continue
}
switch {
case frame.Module != "":
io.WriteString(out, frame.Module)
updated = true
case frame.Filename != "":
io.WriteString(out, frame.Filename)
updated = true
case frame.Classname != "":
io.WriteString(out, frame.Classname)
updated = true
}
if s.maybeWriteString(frame.Function, out) {
updated = true
}
}
return updated
}
func (SetGroupingKey) maybeWriteString(s string, out hash.Hash) bool {
if s == "" {
return false
}
io.WriteString(out, s)
return true
}