forked from flashcatcloud/categraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logs_endpoints.go
210 lines (183 loc) · 7.26 KB
/
logs_endpoints.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
//go:build !no_logs
package agent
import (
"fmt"
"net"
"strconv"
"strings"
"time"
coreconfig "flashcat.cloud/categraf/config"
logsconfig "flashcat.cloud/categraf/config/logs"
)
var logsEndpoints = map[string]int{
"agent-http.logs.flashcat.cloud": 443,
"agent-tcp.logs.flashcat.cloud": 8848,
}
// logs-intake endpoint prefix.
const (
tcpEndpointPrefix = "agent-tcp.logs"
httpEndpointPrefix = "agent-http.logs."
)
// BuildEndpoints returns the endpoints to send logs.
func BuildEndpoints(intakeTrackType logsconfig.IntakeTrackType, intakeProtocol logsconfig.IntakeProtocol, intakeOrigin logsconfig.IntakeOrigin) (*logsconfig.Endpoints, error) {
return BuildEndpointsWithConfig(httpEndpointPrefix, intakeTrackType, intakeProtocol, intakeOrigin)
}
// BuildEndpointsWithConfig returns the endpoints to send logs.
func BuildEndpointsWithConfig(endpointPrefix string, intakeTrackType logsconfig.IntakeTrackType, intakeProtocol logsconfig.IntakeProtocol, intakeOrigin logsconfig.IntakeOrigin) (*logsconfig.Endpoints, error) {
logsConfig := coreconfig.Config.Logs
switch logsConfig.SendType {
case "http":
return BuildHTTPEndpointsWithConfig(endpointPrefix, intakeTrackType, intakeProtocol, intakeOrigin)
case "tcp":
return buildTCPEndpoints(logsConfig)
case "kafka":
return buildKafkaEndpoints(logsConfig)
}
return buildTCPEndpoints(logsConfig)
}
func buildKafkaEndpoints(logsConfig coreconfig.Logs) (*logsconfig.Endpoints, error) {
// return nil, nil
// Provide default values for legacy settings when the configuration key does not exist
defaultTLS := coreconfig.Config.Logs.SendWithTLS
main := logsconfig.Endpoint{
APIKey: strings.TrimSpace(logsConfig.APIKey),
UseCompression: logsConfig.UseCompression,
CompressionLevel: logsConfig.CompressionLevel,
ConnectionResetInterval: 0,
BackoffBase: 1.0,
BackoffMax: 120.0,
BackoffFactor: 2.0,
RecoveryInterval: 2,
RecoveryReset: false,
Addr: logsConfig.SendTo,
Topic: logsConfig.Topic,
}
if intakeTrackType != "" {
main.Version = logsconfig.EPIntakeVersion2
main.TrackType = intakeTrackType
} else {
main.Version = logsconfig.EPIntakeVersion1
}
if len(logsConfig.SendTo) != 0 {
brokers := strings.Split(logsConfig.SendTo, ",")
if len(brokers) == 0 {
return nil, fmt.Errorf("wrong send_to content %s", logsConfig.SendTo)
}
host, port, err := parseAddress(brokers[0])
if err != nil {
return nil, fmt.Errorf("could not parse %s: %v", logsConfig.SendTo, err)
}
main.Host = host
main.Port = port
main.UseSSL = defaultTLS
} else {
return nil, fmt.Errorf("empty send_to is not allowed when send_type is kafka")
}
return NewEndpoints(main, false, "kafka"), nil
}
func buildTCPEndpoints(logsConfig coreconfig.Logs) (*logsconfig.Endpoints, error) {
main := logsconfig.Endpoint{
APIKey: logsConfig.APIKey,
ProxyAddress: "",
ConnectionResetInterval: 0,
}
if len(logsConfig.SendTo) != 0 {
host, port, err := parseAddress(logsConfig.SendTo)
if err != nil {
return nil, fmt.Errorf("could not parse %s: %v", logsConfig.SendTo, err)
}
main.Host = host
main.Port = port
main.UseSSL = logsConfig.SendWithTLS
} else {
main.Host = "agent-tcp.logs.flashcat.cloud"
main.Port = logsEndpoints[main.Host]
main.UseSSL = logsConfig.SendWithTLS
}
return NewEndpoints(main, false, "tcp"), nil
}
// BuildHTTPEndpoints returns the HTTP endpoints to send logs to.
func BuildHTTPEndpoints(intakeTrackType logsconfig.IntakeTrackType, intakeProtocol logsconfig.IntakeProtocol, intakeOrigin logsconfig.IntakeOrigin) (*logsconfig.Endpoints, error) {
return BuildHTTPEndpointsWithConfig(httpEndpointPrefix, intakeTrackType, intakeProtocol, intakeOrigin)
}
// BuildHTTPEndpointsWithConfig uses two arguments that instructs it how to access configuration parameters, then returns the HTTP endpoints to send logs to. This function is able to default to the 'classic' BuildHTTPEndpoints() w ldHTTPEndpointsWithConfigdefault variables logsConfigDefaultKeys and httpEndpointPrefix
func BuildHTTPEndpointsWithConfig(endpointPrefix string, intakeTrackType logsconfig.IntakeTrackType, intakeProtocol logsconfig.IntakeProtocol, intakeOrigin logsconfig.IntakeOrigin) (*logsconfig.Endpoints, error) {
// Provide default values for legacy settings when the configuration key does not exist
logsConfig := coreconfig.Config.Logs
defaultTLS := coreconfig.Config.Logs.SendWithTLS
main := logsconfig.Endpoint{
APIKey: strings.TrimSpace(logsConfig.APIKey),
UseCompression: logsConfig.UseCompression,
CompressionLevel: logsConfig.CompressionLevel,
ConnectionResetInterval: 0,
BackoffBase: 1.0,
BackoffMax: 120.0,
BackoffFactor: 2.0,
RecoveryInterval: 2,
RecoveryReset: false,
}
if intakeTrackType != "" {
main.Version = logsconfig.EPIntakeVersion2
main.TrackType = intakeTrackType
main.Protocol = intakeProtocol
main.Origin = intakeOrigin
} else {
main.Version = logsconfig.EPIntakeVersion1
}
if len(logsConfig.SendTo) != 0 {
host, port, err := parseAddress(logsConfig.SendTo)
if err != nil {
return nil, fmt.Errorf("could not parse %s: %v", logsConfig.SendTo, err)
}
main.Host = host
main.Port = port
main.UseSSL = defaultTLS
} else {
main.Host = logsConfig.SendTo
main.UseSSL = defaultTLS
}
batchWait := time.Duration(logsConfig.BatchWait) * time.Second
batchMaxConcurrentSend := coreconfig.BatchConcurrence()
batchMaxSize := coreconfig.BatchMaxSize()
batchMaxContentSize := coreconfig.BatchMaxContentSize()
return NewEndpointsWithBatchSettings(main, false, "http", batchWait, batchMaxConcurrentSend, batchMaxSize, batchMaxContentSize), nil
}
// parseAddress returns the host and the port of the address.
func parseAddress(address string) (string, int, error) {
host, portString, err := net.SplitHostPort(address)
if err != nil {
return "", 0, err
}
port, err := strconv.Atoi(portString)
if err != nil {
return "", 0, err
}
return host, port, nil
}
// NewEndpoints returns a new endpoints composite with default batching settings
func NewEndpoints(main logsconfig.Endpoint, useProto bool, typ string) *logsconfig.Endpoints {
logsConfig := coreconfig.Config.Logs
return &logsconfig.Endpoints{
Main: main,
Additionals: nil,
UseProto: useProto,
Type: typ,
BatchWait: time.Duration(logsConfig.BatchWait) * time.Second,
BatchMaxConcurrentSend: coreconfig.BatchConcurrence(),
BatchMaxSize: coreconfig.BatchMaxSize(),
BatchMaxContentSize: coreconfig.BatchMaxContentSize(),
}
}
// NewEndpointsWithBatchSettings returns a new endpoints composite with non-default batching settings specified
func NewEndpointsWithBatchSettings(main logsconfig.Endpoint, useProto bool, typ string, batchWait time.Duration, batchMaxConcurrentSend int, batchMaxSize int, batchMaxContentSize int) *logsconfig.Endpoints {
return &logsconfig.Endpoints{
Main: main,
Additionals: nil,
UseProto: useProto,
Type: typ,
BatchWait: batchWait,
BatchMaxConcurrentSend: batchMaxConcurrentSend,
BatchMaxSize: batchMaxSize,
BatchMaxContentSize: batchMaxContentSize,
}
}