This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
input_attrs.go
172 lines (157 loc) · 4.78 KB
/
input_attrs.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
package graylog
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/suzuki-shunsuke/go-set/v6"
)
var (
// InputAttrsIntFieldSet is the set of int fields of all type of input attributes.
InputAttrsIntFieldSet = set.NewStrSet()
// InputAttrsBoolFieldSet is the set of bool fields of all type of input attributes.
InputAttrsBoolFieldSet = set.NewStrSet()
// InputAttrsStrFieldSet is the set of string fields of all type of input attributes.
InputAttrsStrFieldSet = set.NewStrSet()
inputAttrsList = []NewInputAttrs{
NewInputAWSFlowLogsAttrs,
NewInputAWSCloudWatchLogsAttrs,
NewInputAWSCloudTrailAttrs,
NewInputBeatsAttrs,
NewInputCEFAMQPAttrs,
NewInputCEFKafkaAttrs,
NewInputCEFTCPAttrs,
NewInputCEFUDPAttrs,
NewInputFakeHTTPMessageAttrs,
NewInputGELFAMQPAttrs,
NewInputGELFHTTPAttrs,
NewInputGELFKafkaAttrs,
NewInputGELFTCPAttrs,
NewInputGELFUDPAttrs,
NewInputJSONPathAttrs,
NewInputNetFlowUDPAttrs,
NewInputRawAMQPAttrs,
NewInputRawKafkaAttrs,
NewInputRawTCPAttrs,
NewInputSyslogAMQPAttrs,
NewInputSyslogKafkaAttrs,
NewInputSyslogTCPAttrs,
NewInputSyslogUDPAttrs,
}
// initialize at init function for preventing initialization loop
attrsSet = &inputAttrsSet{}
)
type (
// GetUnknownTypeInputAttrsIntf returns an unknown type InputAttrs.
GetUnknownTypeInputAttrsIntf func(map[string]NewInputAttrs, string) InputAttrs
// GetInputAttrsByTypeIntf returns a given type InputAttrs.
GetInputAttrsByTypeIntf func(map[string]NewInputAttrs, string) InputAttrs
// NewInputAttrs is the constructor of InputAttrs.
NewInputAttrs func() InputAttrs
// InputAttrs represents Input Attributes.
// A receiver must be a pointer.
InputAttrs interface {
InputType() string
}
inputAttrsSet struct {
data map[string]NewInputAttrs
GetUnknownType GetUnknownTypeInputAttrsIntf
GetByType GetInputAttrsByTypeIntf
}
)
func init() {
attrsSet = &inputAttrsSet{
data: map[string]NewInputAttrs{},
GetUnknownType: func(data map[string]NewInputAttrs, t string) InputAttrs {
return &InputUnknownAttrs{inputType: t}
},
GetByType: func(data map[string]NewInputAttrs, t string) InputAttrs {
if data == nil {
return attrsSet.GetUnknownType(data, t)
}
a, ok := data[t]
if !ok {
return attrsSet.GetUnknownType(data, t)
}
return a()
},
}
if err := SetInputAttrs(inputAttrsList...); err != nil {
panic(err)
}
for _, attrs := range inputAttrsList {
a := attrs()
ts := reflect.ValueOf(a).Elem().Type()
n := ts.NumField()
for i := 0; i < n; i++ {
f := ts.Field(i)
tag := strings.Split(f.Tag.Get("json"), ",")[0]
switch f.Type.Kind() {
case reflect.String:
InputAttrsStrFieldSet.Add(tag)
case reflect.Int:
InputAttrsIntFieldSet.Add(tag)
case reflect.Bool:
InputAttrsBoolFieldSet.Add(tag)
default:
panic(fmt.Sprintf("invalid type: %v", f.Type.Kind()))
}
}
}
}
// SetFuncGetUnknownTypeInputAttrs customizes NewInputAttrsByType's behavior.
func SetFuncGetUnknownTypeInputAttrs(f GetUnknownTypeInputAttrsIntf) {
attrsSet.GetUnknownType = f
}
// GetFuncGetUnknownTypeInputAttrs returns the global GetUnknownTypeInputAttrs function.
// Mainly this is used to prevent global pollution at test.
//
// f := graylog.GetFuncGetUnknownTypeInputAttrs()
// // change the global function temporary
// defer graylog.SetFuncGetUnknownTypeInputAttrs(f)
// graylog.SetFuncGetUnknownTypeInputAttrs(customFunc)
func GetFuncGetUnknownTypeInputAttrs() GetUnknownTypeInputAttrsIntf {
if attrsSet == nil {
return nil
}
return attrsSet.GetUnknownType
}
// SetFuncGetInputAttrsByType customizes NewInputAttrsByType's behavior.
func SetFuncGetInputAttrsByType(f GetInputAttrsByTypeIntf) {
attrsSet.GetByType = f
}
// GetFuncGetInputAttrsByType returns the global GetInputAttrsByType function.
// Mainly this is used to prevent global pollution at test.
//
// f := graylog.GetFuncGetInputAttrsByType()
// // change the global function temporary
// defer graylog.SetFuncGetInputAttrsByType(f)
// graylog.SetFuncGetInputAttrsByType(customFunc)
func GetFuncGetInputAttrsByType() GetInputAttrsByTypeIntf {
if attrsSet == nil {
return nil
}
return attrsSet.GetByType
}
// NewInputAttrsByType returns a new InputAttrs.
func NewInputAttrsByType(t string) InputAttrs {
return attrsSet.GetByType(attrsSet.data, t)
}
// SetInputAttrs sets InputAttrs.
// You can add the custom InputAttrs and override existing InputAttrs.
func SetInputAttrs(args ...NewInputAttrs) error {
if attrsSet == nil {
attrsSet = &inputAttrsSet{data: map[string]NewInputAttrs{}}
}
if attrsSet.data == nil {
attrsSet.data = map[string]NewInputAttrs{}
}
for _, f := range args {
a := f()
if reflect.TypeOf(a).Kind() != reflect.Ptr {
return errors.New("NewInputAttrs must return pointer")
}
attrsSet.data[a.InputType()] = f
}
return nil
}