forked from elastic/beats
/
config.go
150 lines (127 loc) · 3.83 KB
/
config.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// Config is put into a different package to prevent cyclic imports in case
// it is needed in several locations
package config
import (
"fmt"
"regexp"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/common"
)
var (
functionPattern = "^[A-Za-z][A-Za-z0-9\\-]{0,139}$"
functionRE = regexp.MustCompile(functionPattern)
configOverrides = common.MustNewConfigFrom(map[string]interface{}{
"path.data": "/tmp",
"path.logs": "/tmp/logs",
"setup.template.enabled": true,
"queue.mem": map[string]interface{}{
"flush.min_events": 10,
"flush.timeout": "0.01s",
},
})
functionLoggingOverrides = common.MustNewConfigFrom(map[string]interface{}{
"logging.to_stderr": true,
"logging.to_files": false,
})
esOverrides = common.MustNewConfigFrom(map[string]interface{}{
"queue.mem.events": "${output.elasticsearch.bulk_max_size}",
"output.elasticsearch.bulk_max_size": 50,
})
logstashOverrides = common.MustNewConfigFrom(map[string]interface{}{
"queue.mem.events": "${output.logstash.bulk_max_size}",
"output.logstash": map[string]interface{}{
"bulk_max_size": 50,
"pipelining": 0,
},
})
// Overrides overrides the default configuration provided by libbeat.
Overrides = []cfgfile.ConditionalOverride{
cfgfile.ConditionalOverride{
Check: always,
Config: configOverrides,
},
cfgfile.ConditionalOverride{
Check: isLogstash,
Config: logstashOverrides,
},
cfgfile.ConditionalOverride{
Check: isElasticsearch,
Config: esOverrides,
},
}
functionOverride = cfgfile.ConditionalOverride{
Check: always,
Config: functionLoggingOverrides,
}
// FunctionOverrides contain logging settings
FunctionOverrides = append(Overrides, functionOverride)
)
// Config default configuration for Functionbeat.
type Config struct {
Provider *common.Config `config:"provider" validate:"required"`
}
// ProviderConfig is a generic configured used by providers.
type ProviderConfig struct {
Functions []*common.Config `config:"functions"`
}
// FunctionConfig minimal configuration from each function.
type FunctionConfig struct {
Type string `config:"type"`
Name functionName `config:"name"`
Enabled bool `config:"enabled"`
}
// DefaultConfig is the default configuration for Functionbeat.
var DefaultConfig = Config{}
// DefaultFunctionConfig is the default configuration for new function.
var DefaultFunctionConfig = FunctionConfig{
Enabled: true,
}
var always = func(_ *common.Config) bool {
return true
}
var isLogstash = func(cfg *common.Config) bool {
return isOutput(cfg, "logstash")
}
var isElasticsearch = func(cfg *common.Config) bool {
return isOutput(cfg, "elasticsearch")
}
func isOutput(cfg *common.Config, name string) bool {
outputCfg, err := cfg.Child("output", -1)
if err != nil {
return false
}
return outputCfg.HasField(name)
}
type functionName string
func (f *functionName) Unpack(s string) error {
if !functionRE.MatchString(s) {
return fmt.Errorf(
"invalid name: '%s', name must match [a-zA-Z0-9-] and be at most 140 characters",
s,
)
}
*f = functionName(s)
return nil
}
func (f *functionName) String() string {
return string(*f)
}
// Validate enforces that function names are unique.
func (p *ProviderConfig) Validate() error {
names := make(map[functionName]bool)
for _, rawfn := range p.Functions {
fc := FunctionConfig{}
rawfn.Unpack(&fc)
if !fc.Enabled {
return nil
}
if _, found := names[fc.Name]; found {
return fmt.Errorf("function name '%s' already exist, name must be unique", fc.Name)
}
names[fc.Name] = true
}
return nil
}