Skip to content

Commit b9c7e2f

Browse files
authored
Merge pull request #116 from chronosphereio/service-ports
fix: add cloudflare, prometheus_remote_write and splunk to service ports [sc-115821]
2 parents 0f81e09 + 4ca91d1 commit b9c7e2f

File tree

3 files changed

+293
-160
lines changed

3 files changed

+293
-160
lines changed

service_port.go

+175-99
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,154 @@
11
package fluentbitconfig
22

33
import (
4-
"errors"
4+
"net"
5+
"strconv"
56
"strings"
67

78
"github.com/calyptia/go-fluentbit-config/v2/networking"
89
)
910

10-
var servicePortDefaultsByPlugin = map[string]servicePortDefaults{
11-
// Inputs.
12-
"collectd": {Port: 25826, Protocol: networking.ProtocolUDP},
13-
"elasticsearch": {Port: 9200, Protocol: networking.ProtocolTCP},
14-
"forward": {Port: 24224, Protocol: networking.ProtocolTCP}, // only if `unix_path` is not set.
15-
"http": {Port: 9880, Protocol: networking.ProtocolTCP},
16-
"mqtt": {Port: 1883, Protocol: networking.ProtocolTCP},
17-
"opentelemetry": {Port: 4318, Protocol: networking.ProtocolTCP},
18-
"statsd": {Port: 8125, Protocol: networking.ProtocolUDP},
19-
"syslog": {Port: 5140, Protocol: networking.ProtocolUDP}, // only if `mode` is not `unix_udp` (default) or `unix_tcp`
20-
"tcp": {Port: 5170, Protocol: networking.ProtocolTCP},
21-
"udp": {Port: 5170, Protocol: networking.ProtocolUDP},
22-
"cloudflare": {Port: 9880, Protocol: networking.ProtocolTCP},
23-
24-
// Outputs.
25-
"prometheus_exporter": {Port: 2021, Protocol: networking.ProtocolTCP},
11+
type ServicePortGetter struct {
12+
EnabledFunc func(p Plugin) bool
13+
PortFunc func(p Plugin) (int32, bool)
14+
ProtocolFunc func(p Plugin) (networking.Protocol, bool)
15+
DefaultPort int32
16+
DefaultProtocol networking.Protocol
2617
}
2718

28-
type servicePortDefaults struct {
29-
Port int
30-
Protocol networking.Protocol
19+
func fromPort(p Plugin) (int32, bool) {
20+
portVal, ok := p.Properties.Get("port")
21+
if !ok {
22+
return 0, false
23+
}
24+
25+
i, ok := int32FromAny(portVal)
26+
return i, ok
27+
}
28+
29+
var inputServicePorts = map[string]ServicePortGetter{
30+
"cloudflare": {
31+
PortFunc: func(p Plugin) (int32, bool) {
32+
addrVal, ok := p.Properties.Get("addr")
33+
if !ok {
34+
return 0, false
35+
}
36+
37+
_, port, err := net.SplitHostPort(stringFromAny(addrVal))
38+
if err != nil {
39+
return 0, false
40+
}
41+
42+
i, err := strconv.ParseInt(port, 10, 32)
43+
if err != nil {
44+
return 0, false
45+
}
46+
47+
return int32(i), true
48+
},
49+
DefaultPort: 9880,
50+
DefaultProtocol: networking.ProtocolTCP,
51+
},
52+
"collectd": {
53+
PortFunc: fromPort,
54+
DefaultPort: 25826,
55+
DefaultProtocol: networking.ProtocolUDP,
56+
},
57+
"elasticsearch": {
58+
PortFunc: fromPort,
59+
DefaultPort: 9200,
60+
DefaultProtocol: networking.ProtocolTCP,
61+
},
62+
"forward": {
63+
EnabledFunc: func(p Plugin) bool {
64+
return !p.Properties.Has("unix_path")
65+
},
66+
PortFunc: fromPort,
67+
DefaultPort: 24224,
68+
DefaultProtocol: networking.ProtocolTCP,
69+
},
70+
"http": {
71+
PortFunc: fromPort,
72+
DefaultPort: 9880,
73+
DefaultProtocol: networking.ProtocolTCP,
74+
},
75+
"mqtt": {
76+
PortFunc: fromPort,
77+
DefaultPort: 1883,
78+
DefaultProtocol: networking.ProtocolTCP,
79+
},
80+
"opentelemetry": {
81+
PortFunc: fromPort,
82+
DefaultPort: 4318,
83+
DefaultProtocol: networking.ProtocolTCP,
84+
},
85+
"prometheus_remote_write": {
86+
PortFunc: fromPort,
87+
DefaultPort: 8080,
88+
DefaultProtocol: networking.ProtocolTCP,
89+
},
90+
"splunk": {
91+
PortFunc: fromPort,
92+
DefaultPort: 8088,
93+
DefaultProtocol: networking.ProtocolTCP,
94+
},
95+
"statsd": {
96+
PortFunc: fromPort,
97+
DefaultPort: 8125,
98+
DefaultProtocol: networking.ProtocolUDP,
99+
},
100+
"syslog": {
101+
EnabledFunc: func(p Plugin) bool {
102+
modeVal, ok := p.Properties.Get("mode")
103+
if !ok {
104+
return false // default mode is unix_udp
105+
}
106+
107+
mode := strings.ToLower(stringFromAny(modeVal))
108+
return mode == "tcp" || mode == "udp"
109+
},
110+
PortFunc: fromPort,
111+
ProtocolFunc: func(p Plugin) (networking.Protocol, bool) {
112+
modeVal, ok := p.Properties.Get("mode")
113+
if !ok {
114+
return networking.Protocol("unknown"), false
115+
}
116+
117+
mode := strings.ToLower(stringFromAny(modeVal))
118+
switch mode {
119+
case "tcp":
120+
return networking.ProtocolTCP, true
121+
case "udp":
122+
return networking.ProtocolUDP, true
123+
}
124+
125+
return networking.Protocol("unknown"), false
126+
},
127+
DefaultPort: 5140,
128+
DefaultProtocol: networking.ProtocolTCP,
129+
},
130+
"tcp": {
131+
PortFunc: fromPort,
132+
DefaultPort: 5170,
133+
DefaultProtocol: networking.ProtocolTCP,
134+
},
135+
"udp": {
136+
PortFunc: fromPort,
137+
DefaultPort: 5170,
138+
DefaultProtocol: networking.ProtocolUDP,
139+
},
140+
}
141+
142+
var outputServicePorts = map[string]ServicePortGetter{
143+
"prometheus_exporter": {
144+
PortFunc: fromPort,
145+
DefaultPort: 2021,
146+
DefaultProtocol: networking.ProtocolTCP,
147+
},
31148
}
32149

33150
type ServicePort struct {
34-
Port int
151+
Port int32
35152
Protocol networking.Protocol
36153
Kind SectionKind
37154
// Plugin is not available for `service`` section kind.
@@ -43,16 +160,16 @@ type ServicePorts []ServicePort
43160
func (c *Config) ServicePorts() ServicePorts {
44161
var out ServicePorts
45162

46-
enabledVal, ok := c.Service.Get("http_server")
47-
if ok && (enabledVal == true || strings.ToLower(stringFromAny(enabledVal)) == "on") {
163+
httpServerEnabled, ok := c.Service.Get("http_server")
164+
if ok && (httpServerEnabled == true || strings.ToLower(stringFromAny(httpServerEnabled)) == "on") {
48165
portVal, ok := c.Service.Get("http_port")
49166
if !ok {
50167
out = append(out, ServicePort{
51168
Port: 2020,
52169
Protocol: networking.ProtocolTCP,
53170
Kind: SectionKindService,
54171
})
55-
} else if i, ok := intFromAny(portVal); ok {
172+
} else if i, ok := int32FromAny(portVal); ok {
56173
out = append(out, ServicePort{
57174
Port: i,
58175
Protocol: networking.ProtocolTCP,
@@ -61,90 +178,49 @@ func (c *Config) ServicePorts() ServicePorts {
61178
}
62179
}
63180

64-
lookup := func(kind SectionKind, plugins Plugins) {
65-
for _, plugin := range plugins {
66-
err := ValidateSection(kind, plugin.Properties)
67-
if errors.Is(err, ErrMissingName) {
68-
continue
69-
}
70-
71-
var e *UnknownPluginError
72-
if errors.As(err, &e) {
73-
continue
74-
}
75-
76-
if plugin.Name == "forward" && plugin.Properties.Has("unix_path") {
77-
continue
78-
}
181+
process := func(getters map[string]ServicePortGetter, kind SectionKind, plugin Plugin) {
182+
getter, ok := getters[plugin.Name]
183+
if !ok {
184+
return
185+
}
79186

80-
if plugin.Name == "syslog" {
81-
modeVal, ok := plugin.Properties.Get("mode")
82-
if !ok {
83-
continue
84-
}
85-
86-
if ok {
87-
mode := strings.ToLower(stringFromAny(modeVal))
88-
if mode == "unix_udp" || mode == "unix_tcp" {
89-
continue
90-
}
91-
}
92-
}
187+
if getter.EnabledFunc != nil && !getter.EnabledFunc(plugin) {
188+
return
189+
}
93190

94-
portVal, ok := plugin.Properties.Get("port")
191+
port := getter.DefaultPort
192+
if getter.PortFunc != nil {
193+
var ok bool
194+
port, ok = getter.PortFunc(plugin)
95195
if !ok {
96-
defaults, ok := servicePortDefaultsByPlugin[plugin.Name]
97-
if ok {
98-
plugin := plugin
99-
plugin.Properties = nil
100-
out = append(out, ServicePort{
101-
Port: defaults.Port,
102-
Protocol: defaults.Protocol,
103-
Kind: kind,
104-
Plugin: &plugin,
105-
})
106-
}
196+
port = getter.DefaultPort
107197
}
198+
}
108199

109-
if ok {
110-
port, ok := intFromAny(portVal)
111-
if ok {
112-
var protocol networking.Protocol
113-
if plugin.Name == "syslog" {
114-
modeVal, ok := plugin.Properties.Get("mode")
115-
if ok {
116-
if v := networking.Protocol(strings.ToUpper(stringFromAny(modeVal))); v.OK() {
117-
protocol = v
118-
}
119-
}
120-
}
121-
122-
if protocol == "" {
123-
defaultPort, ok := servicePortDefaultsByPlugin[plugin.Name]
124-
if ok {
125-
protocol = defaultPort.Protocol
126-
}
127-
}
128-
129-
if protocol == "" {
130-
protocol = networking.ProtocolTCP
131-
}
132-
133-
plugin := plugin
134-
plugin.Properties = nil
135-
out = append(out, ServicePort{
136-
Port: port,
137-
Protocol: protocol,
138-
Kind: kind,
139-
Plugin: &plugin,
140-
})
141-
}
200+
protocol := getter.DefaultProtocol
201+
if getter.ProtocolFunc != nil {
202+
var ok bool
203+
protocol, ok = getter.ProtocolFunc(plugin)
204+
if !ok {
205+
protocol = getter.DefaultProtocol
142206
}
143207
}
208+
209+
out = append(out, ServicePort{
210+
Port: port,
211+
Protocol: protocol,
212+
Kind: kind,
213+
Plugin: &plugin,
214+
})
215+
}
216+
217+
for _, input := range c.Pipeline.Inputs {
218+
process(inputServicePorts, SectionKindInput, input)
144219
}
145220

146-
lookup(SectionKindInput, c.Pipeline.Inputs)
147-
lookup(SectionKindOutput, c.Pipeline.Outputs)
221+
for _, output := range c.Pipeline.Outputs {
222+
process(outputServicePorts, SectionKindOutput, output)
223+
}
148224

149225
return out
150226
}

0 commit comments

Comments
 (0)