forked from nathanielc/morgoth
/
mapper.go
216 lines (186 loc) · 4.54 KB
/
mapper.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
package morgoth
import (
"hash/fnv"
"regexp"
"sort"
)
type Mapper struct {
detectorMappers map[uint64][]*DetectorMapper
detectorMatchers []*DetectorMatcher
Stats MapperStats
}
type MapperStats struct {
WindowCount uint64
MapperStats []*DetectorMapperStats
MatcherStats []*DetectorMatcherStats
}
type DetectorMatcher struct {
NamePattern *regexp.Regexp
TagPatterns map[string]*regexp.Regexp
DetectorBuilder func() *Detector
Stats DetectorMatcherStats
}
type DetectorMatcherStats struct {
NamePattern string
TagPatterns map[string]string
MatchCount uint64
}
type DetectorMapper struct {
Name string
Tags map[string]string
detector *Detector
Stats DetectorMapperStats
}
type DetectorMapperStats struct {
Name string
Tags map[string]string
MapCount uint64
DetectorStats *DetectorStats
}
func NewMapper(detectorMappers []*DetectorMapper, detectorMatchers []*DetectorMatcher) *Mapper {
mapper := &Mapper{
detectorMappers: make(
map[uint64][]*DetectorMapper,
len(detectorMappers),
),
detectorMatchers: make(
[]*DetectorMatcher,
0,
len(detectorMatchers),
),
}
for _, m := range detectorMappers {
mapper.addDetectorMapper(m)
}
for _, m := range detectorMatchers {
mapper.addDetectorMatcher(m)
}
return mapper
}
func (self *Mapper) addDetectorMapper(mapper *DetectorMapper) {
hash := calcHash(mapper.Name, mapper.Tags)
self.detectorMappers[hash] = append(
self.detectorMappers[hash],
mapper,
)
self.Stats.MapperStats = append(
self.Stats.MapperStats,
&mapper.Stats,
)
}
func (self *Mapper) addDetectorMatcher(matcher *DetectorMatcher) {
self.detectorMatchers = append(
self.detectorMatchers,
matcher,
)
self.Stats.MatcherStats = append(
self.Stats.MatcherStats,
&matcher.Stats,
)
}
func (self *Mapper) Map(w *Window) *Detector {
self.Stats.WindowCount++
// First check for match in detector mappers
hash := calcHash(w.Name, w.Tags)
for _, mapper := range self.detectorMappers[hash] {
detector := mapper.Map(w)
if detector != nil {
return detector
}
}
// Last check for match in Detector matchers
for _, matcher := range self.detectorMatchers {
if matcher.IsMatch(w) {
mapper := matcher.NewDetectorMapper(w)
self.addDetectorMapper(mapper)
return mapper.detector
}
}
// No mapping found
return nil
}
func (self *Mapper) GetDetectorMappings() []*DetectorMapper {
mappings := make([]*DetectorMapper, 0, len(self.detectorMappers))
for _, value := range self.detectorMappers {
mappings = append(mappings, value...)
}
return mappings
}
func calcHash(name string, tags map[string]string) uint64 {
hash := fnv.New64a()
hash.Write([]byte(name))
// Sort all tag keys
keys := make([]string, len(tags))
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
//Hash key/value pairs
for _, k := range keys {
hash.Write([]byte(k))
hash.Write([]byte(tags[k]))
}
return hash.Sum64()
}
func NewDetectorMapper(w *Window, detector *Detector) *DetectorMapper {
m := &DetectorMapper{
Name: w.Name,
Tags: w.Tags,
detector: detector,
Stats: DetectorMapperStats{
Name: w.Name,
Tags: w.Tags,
DetectorStats: &detector.Stats,
},
}
return m
}
func (self *DetectorMapper) Map(w *Window) *Detector {
if self.Name != w.Name {
return nil
}
// Check that window doesn't have extra tags
if len(self.Tags) != len(w.Tags) {
return nil
}
// Check that tag sets match
for k, mapTag := range self.Tags {
if tag, ok := w.Tags[k]; !ok || tag != mapTag {
return nil
}
}
self.Stats.MapCount++
return self.detector
}
func NewDetectorMatcher(namePattern *regexp.Regexp, tagPatterns map[string]*regexp.Regexp, detectorBuilder DetectorBuilder) *DetectorMatcher {
tags := make(map[string]string, len(tagPatterns))
for tag, pattern := range tagPatterns {
tags[tag] = pattern.String()
}
return &DetectorMatcher{
NamePattern: namePattern,
TagPatterns: tagPatterns,
DetectorBuilder: detectorBuilder,
Stats: DetectorMatcherStats{
NamePattern: namePattern.String(),
TagPatterns: tags,
},
}
}
func (self *DetectorMatcher) IsMatch(w *Window) bool {
if !self.NamePattern.MatchString(w.Name) {
return false
}
//Check only defined tags match patterns
for k, pattern := range self.TagPatterns {
if tag, ok := w.Tags[k]; !ok || !pattern.MatchString(tag) {
return false
}
}
self.Stats.MatchCount++
return true
}
func (self *DetectorMatcher) NewDetectorMapper(w *Window) *DetectorMapper {
detector := self.DetectorBuilder()
return NewDetectorMapper(w, detector)
}