-
Notifications
You must be signed in to change notification settings - Fork 3
/
multiexporter.go
115 lines (93 loc) · 2.17 KB
/
multiexporter.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
package eventexporter
import (
"fmt"
"strings"
)
type MultiExporter []Exporter
// NewMultiExporter inits the exporter services like; segment, datadog etc..
// and implements Exporter interface with Send and Close functions
func NewMultiExporter(e ...Exporter) MultiExporter {
return MultiExporter(e)
}
// Send publishes the events to multiple upstreams, returns error on first
// occurence
func (m MultiExporter) Send(event *Event) error {
for _, e := range m {
if !isWhitelisted(e.Name(), event.WhitelistedUpstreams) {
continue
}
if err := e.Send(event); err != nil {
return err
}
}
return nil
}
// Name returns the name of the exporter.
func (MultiExporter) Name() string { return "all" }
// Close closes the upstreams.
func (m MultiExporter) Close() error {
for _, e := range m {
if err := e.Close(); err != nil {
return err
}
}
return nil
}
// eventSeperator uses Event struct and seperates eventname and tags.
// while appending tags into array, if tags dont exist in whitelist wont be added to array
// Also empty properties are not added into tags array
func eventSeperator(m *Event) (string, []string) {
eventName := strings.Replace(m.Name, " ", "_", -1)
tags := make([]string, 0)
if m.User.Email != "" {
tags = append(tags, fmt.Sprintf("email:%s", m.User.Email))
}
if m.User.Username != "" {
tags = append(tags, fmt.Sprintf("username:%s", m.User.Username))
}
for key, val := range m.Properties {
value, ok := val.(string)
if !ok {
continue
}
if isAllowed(key) && value != "" {
tags = append(tags, fmt.Sprintf("%s:%s", clean(key), clean(val.(string))))
}
}
return eventName, tags
}
func clean(s string) string {
return strings.Replace(s, " ", "_", -1)
}
func isAllowed(ß string) bool {
whitelist := []string{
"host",
"groupName",
"email",
"inviter",
"subject",
"env",
"category",
"label",
"firstName",
"invitesCount",
"group",
}
for _, val := range whitelist {
if ß == val {
return true
}
}
return false
}
func isWhitelisted(name string, whitelist []string) bool {
if len(whitelist) == 0 {
return true
}
for _, w := range whitelist {
if w == name {
return true
}
}
return false
}