-
Notifications
You must be signed in to change notification settings - Fork 3
/
endpoints.go
678 lines (623 loc) · 23.5 KB
/
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package bitflow
import (
"errors"
"flag"
"fmt"
"net"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/antongulenko/golib"
log "github.com/sirupsen/logrus"
)
type MarshallingFormat string
type EndpointType string
const (
UndefinedEndpoint = EndpointType("")
TcpEndpoint = EndpointType("tcp")
TcpListenEndpoint = EndpointType("listen")
FileEndpoint = EndpointType("file")
StdEndpoint = EndpointType("std")
HttpEndpoint = EndpointType("http")
EmptyEndpoint = EndpointType("empty")
UndefinedFormat = MarshallingFormat("")
TextFormat = MarshallingFormat("text")
CsvFormat = MarshallingFormat("csv")
BinaryFormat = MarshallingFormat("bin")
PrometheusFormat = MarshallingFormat("prometheus")
tcpDownloadRetryInterval = 1000 * time.Millisecond
tcpDialTimeout = 2000 * time.Millisecond
)
var (
stdTransportTarget = "-"
binaryFileSuffix = ".bin"
)
var DefaultEndpointFactory = EndpointFactory{
FlagOutputFilesClean: false,
FlagIoBuffer: 4096,
FlagTcpConnectionLimit: 0,
FlagParallelHandler: ParallelSampleHandler{
ParallelParsers: runtime.NumCPU(),
BufferedSamples: 10000,
},
FlagFilesKeepAlive: false,
FlagInputFilesRobust: false,
FlagInputTcpAcceptLimit: 0,
FlagTcpSourceDropErrors: false,
FlagOutputTcpListenBuffer: 0,
FlagFilesAppend: false,
FlagFileVanishedCheck: 0,
}
func init() {
DefaultEndpointFactory.Clear()
RegisterDefaults(&DefaultEndpointFactory)
}
// EndpointFactory creates SampleSink and SampleSource instances for a SamplePipeline.
// It defines command line flags for configuring the objects it creates.
// All fields named Flag* are set by the according command line flags and evaluated in CreateInput() and CreateOutput().
// FlagInputs is not set by command line flags automatically.
// After flag.Parse(), those fields can be modified to override the command line flags defined by the user.
type EndpointFactory struct {
FlagSourceTag string
// File input/output flags
FlagInputFilesRobust bool
FlagOutputFilesClean bool
FlagIoBuffer int
FlagFilesKeepAlive bool
FlagFilesAppend bool
FlagFileVanishedCheck time.Duration
// TCP input/output flags
FlagOutputTcpListenBuffer uint
FlagTcpConnectionLimit uint
FlagInputTcpAcceptLimit uint
FlagTcpSourceDropErrors bool
FlagTcpLogReceivedData bool
// Parallel marshalling/unmarshalling flags
FlagParallelHandler ParallelSampleHandler
// CustomDataSources can be filled by client code before EndpointFactory.CreateInput or similar
// methods to allow creation of custom data sources. The map key is a short name of the data source
// that can be used in URL endpoint descriptions. The parameter for the function will be
// the URL of the endpoint, with the scheme part stripped.
// Example: When registering a function with the key "http", the following URL endpoint:
// http://localhost:5555/abc
// will invoke the factory function with the parameter "localhost:5555/abc"
CustomDataSources map[EndpointType]func(string) (SampleSource, error)
// CustomDataSinks can be filled by client code before EndpointFactory.CreateOutput or similar
// methods to allow creation of custom data sinks. See CustomDataSources for the meaning of the
// map keys and values.
CustomDataSinks map[EndpointType]func(string) (SampleProcessor, error)
// Marshallers can be filled by client code before EndpointFactory.CreateOutput or similar
// methods to allow custom marshalling formats in output files, network connections and so on.
Marshallers map[MarshallingFormat]func() Marshaller
// CustomGeneralFlags, CustomInputFlags and CustomOutputFlags lets client code
// register custom command line flags that configure aspects of endpoints created
// through CustomDataSources and CustomDataSinks.
CustomGeneralFlags []func(f *flag.FlagSet)
CustomInputFlags []func(f *flag.FlagSet)
CustomOutputFlags []func(f *flag.FlagSet)
}
func NewEndpointFactory() *EndpointFactory {
factory := DefaultEndpointFactory
factory.Clear()
RegisterDefaults(&factory)
return &factory
}
func (f *EndpointFactory) Clear() {
f.CustomDataSources = make(map[EndpointType]func(string) (SampleSource, error))
f.CustomDataSinks = make(map[EndpointType]func(string) (SampleProcessor, error))
f.Marshallers = make(map[MarshallingFormat]func() Marshaller)
f.CustomGeneralFlags = nil
f.CustomInputFlags = nil
f.CustomOutputFlags = nil
}
func RegisterDefaults(factory *EndpointFactory) {
RegisterBuiltinMarshallers(factory)
RegisterConsoleBoxOutput(factory)
RegisterEmptyInputOutput(factory)
}
func RegisterEmptyInputOutput(factory *EndpointFactory) {
factory.CustomDataSinks[EmptyEndpoint] = func(string) (SampleProcessor, error) {
return new(DroppingSampleProcessor), nil
}
factory.CustomDataSources[EmptyEndpoint] = func(string) (SampleSource, error) {
return new(EmptySampleSource), nil
}
}
func RegisterBuiltinMarshallers(factory *EndpointFactory) {
factory.Marshallers[TextFormat] = func() Marshaller {
return TextMarshaller{}
}
factory.Marshallers[CsvFormat] = func() Marshaller {
return CsvMarshaller{}
}
factory.Marshallers[BinaryFormat] = func() Marshaller {
return BinaryMarshaller{}
}
factory.Marshallers[PrometheusFormat] = func() Marshaller {
return PrometheusMarshaller{}
}
}
func (f *EndpointFactory) ParseParameters(params map[string]string) (err error) {
get := func(name string) string {
if err != nil {
return ""
}
res := params[name]
delete(params, name)
return res
}
strParam := func(target *string, name string) {
*target = get(name)
}
boolParam := func(target *bool, name string) {
if strVal := get(name); strVal != "" {
*target, err = strconv.ParseBool(strVal)
}
}
intParam := func(target *int, name string) {
if strVal := get(name); strVal != "" {
*target, err = strconv.Atoi(strVal)
}
}
uintParam := func(target *uint, name string) {
if strVal := get(name); strVal != "" {
val, parseErr := strconv.ParseUint(strVal, 10, 64)
err = parseErr
*target = uint(val)
}
}
durationParam := func(target *time.Duration, name string) {
if strVal := get(name); strVal != "" {
*target, err = time.ParseDuration(strVal)
}
}
strParam(&f.FlagSourceTag, "source-tag")
boolParam(&f.FlagOutputFilesClean, "files-clean")
intParam(&f.FlagIoBuffer, "files-buf")
uintParam(&f.FlagTcpConnectionLimit, "tcp-limit")
boolParam(&f.FlagTcpLogReceivedData, "tcp-log-received")
intParam(&f.FlagParallelHandler.ParallelParsers, "par")
intParam(&f.FlagParallelHandler.BufferedSamples, "buf")
boolParam(&f.FlagFilesKeepAlive, "files-keep-alive")
boolParam(&f.FlagInputFilesRobust, "files-robust")
uintParam(&f.FlagInputTcpAcceptLimit, "listen-limit")
boolParam(&f.FlagTcpSourceDropErrors, "tcp-drop-err")
uintParam(&f.FlagOutputTcpListenBuffer, "listen-buffer")
boolParam(&f.FlagFilesAppend, "files-append")
durationParam(&f.FlagFileVanishedCheck, "files-check-output")
if err == nil && len(params) > 0 {
err = fmt.Errorf("Unexpected parameters for EndpointFactory: %v", params)
}
return
}
// RegisterConfigFlags registers all flags to the global CommandLine object.
func (f *EndpointFactory) RegisterFlags() {
f.RegisterGeneralFlagsTo(flag.CommandLine)
f.RegisterInputFlagsTo(flag.CommandLine)
f.RegisterOutputFlagsTo(flag.CommandLine)
}
// RegisterGeneralFlagsTo registers flags that configure different aspects of both
// data input and data output. These flags affect to both performance and functionality of
// TCP, file and std I/O.
func (f *EndpointFactory) RegisterGeneralFlagsTo(fs *flag.FlagSet) {
// Files
fs.BoolVar(&f.FlagOutputFilesClean, "files-clean", f.FlagOutputFilesClean, "Delete all potential output files before writing.")
fs.IntVar(&f.FlagIoBuffer, "files-buf", f.FlagIoBuffer, "Size (byte) of buffered IO when reading/writing files.")
// TCP
fs.UintVar(&f.FlagTcpConnectionLimit, "tcp-limit", f.FlagTcpConnectionLimit, "Limit number of TCP connections to accept/establish. Exit afterwards")
// Parallel marshalling/unmarshalling
fs.IntVar(&f.FlagParallelHandler.ParallelParsers, "par", f.FlagParallelHandler.ParallelParsers, "Parallel goroutines used for (un)marshalling samples")
fs.IntVar(&f.FlagParallelHandler.BufferedSamples, "buf", f.FlagParallelHandler.BufferedSamples, "Number of samples buffered when (un)marshalling.")
// Custom
for _, factoryFunc := range f.CustomGeneralFlags {
factoryFunc(fs)
}
}
// RegisterInputFlagsTo registers flags that configure aspects of data input.
func (f *EndpointFactory) RegisterInputFlagsTo(fs *flag.FlagSet) {
fs.StringVar(&f.FlagSourceTag, "source-tag", f.FlagSourceTag, "Add the data source (e.g. input file, TCP endpoint, ...) as the given tag to each read sample.")
fs.BoolVar(&f.FlagFilesKeepAlive, "files-keep-alive", f.FlagFilesKeepAlive, "Do not shut down after all files have been read. Useful in combination with -listen-buffer.")
fs.BoolVar(&f.FlagInputFilesRobust, "files-robust", f.FlagInputFilesRobust, "When encountering errors while reading files, print warnings instead of failing.")
fs.UintVar(&f.FlagInputTcpAcceptLimit, "listen-limit", f.FlagInputTcpAcceptLimit, "Limit number of simultaneous TCP connections accepted for incoming data.")
fs.BoolVar(&f.FlagTcpSourceDropErrors, "tcp-drop-err", f.FlagTcpSourceDropErrors, "Don't print errors when establishing active TCP input connection fails")
for _, factoryFunc := range f.CustomInputFlags {
factoryFunc(fs)
}
}
// RegisterOutputConfigFlagsTo registers flags that configure data outputs.
func (f *EndpointFactory) RegisterOutputFlagsTo(fs *flag.FlagSet) {
fs.UintVar(&f.FlagOutputTcpListenBuffer, "listen-buffer", f.FlagOutputTcpListenBuffer, "When listening for outgoing connections, store a number of samples in a ring buffer that will be delivered first to all established connections.")
fs.BoolVar(&f.FlagFilesAppend, "files-append", f.FlagFilesAppend, "For file output, do no create new files by incrementing the suffix and append to existing files.")
fs.DurationVar(&f.FlagFileVanishedCheck, "files-check-output", f.FlagFileVanishedCheck, "For file output, check if the output file vanished or changed in regular intervals. Reopen the file in that case.")
fs.BoolVar(&f.FlagTcpLogReceivedData, "tcp-log-received", f.FlagTcpLogReceivedData, "For all TCP output connections, log received data, which is usually not expected.")
for _, factoryFunc := range f.CustomOutputFlags {
factoryFunc(fs)
}
}
// Writer returns an instance of SampleReader, configured by the values stored in the EndpointFactory.
func (f *EndpointFactory) Reader(um Unmarshaller) SampleReader {
return SampleReader{
ParallelSampleHandler: f.FlagParallelHandler,
Unmarshaller: um,
}
}
// CreateInput creates a SampleSource object based on the given input endpoint descriptions
// and the configuration flags in the EndpointFactory.
func (f *EndpointFactory) CreateInput(inputs ...string) (SampleSource, error) {
var result SampleSource
inputType := UndefinedEndpoint
for _, input := range inputs {
endpoint, err := f.ParseEndpointDescription(input, false)
if err != nil {
return nil, err
}
if endpoint.Format != UndefinedFormat {
return nil, fmt.Errorf("Format cannot be specified for data input: %v", input)
}
if result == nil {
inputType = endpoint.Type
result, err = f.createInput(endpoint)
if err != nil {
return nil, err
}
} else {
if inputType != endpoint.Type {
return nil, fmt.Errorf("Please provide only one data source (Provided %v and %v)", inputType, endpoint.Type)
}
if endpoint.IsCustomType {
return nil, fmt.Errorf("Cannot define multiple sources for custom input type '%v'", inputType)
}
switch endpoint.Type {
case StdEndpoint:
return nil, errors.New("Cannot read from stdin multiple times")
case TcpListenEndpoint:
return nil, errors.New("Cannot listen for input on multiple TCP ports")
case TcpEndpoint, HttpEndpoint:
source := result.(*TCPSource)
source.RemoteAddrs = append(source.RemoteAddrs, endpoint.Target)
case FileEndpoint:
source := result.(*FileSource)
source.FileNames = append(source.FileNames, endpoint.Target)
default:
return nil, errors.New("Unknown endpoint type: " + string(endpoint.Type))
}
}
}
return result, nil
}
func (f *EndpointFactory) createInput(endpoint EndpointDescription) (SampleSource, error) {
reader := f.Reader(nil) // nil as Unmarshaller makes the SampleSource auto-detect the format
if f.FlagSourceTag != "" {
reader.Handler = sourceTagger(f.FlagSourceTag)
}
switch endpoint.Type {
case StdEndpoint:
source := NewConsoleSource()
source.Reader = reader
return source, nil
case TcpEndpoint, HttpEndpoint:
source := &TCPSource{
RemoteAddrs: []string{endpoint.Target},
PrintErrors: !f.FlagTcpSourceDropErrors,
RetryInterval: tcpDownloadRetryInterval,
DialTimeout: tcpDialTimeout,
UseHTTP: endpoint.Type == HttpEndpoint,
}
source.TcpConnLimit = f.FlagTcpConnectionLimit
source.Reader = reader
return source, nil
case TcpListenEndpoint:
source := NewTcpListenerSource(endpoint.Target)
source.SimultaneousConnections = f.FlagInputTcpAcceptLimit
source.TcpConnLimit = f.FlagTcpConnectionLimit
source.Reader = reader
return source, nil
case FileEndpoint:
source := &FileSource{
FileNames: []string{endpoint.Target},
IoBuffer: f.FlagIoBuffer,
Robust: f.FlagInputFilesRobust,
KeepAlive: f.FlagFilesKeepAlive,
}
source.Reader = reader
return source, nil
default:
if factory, ok := f.CustomDataSources[endpoint.Type]; ok && endpoint.IsCustomType {
result, err := factory(endpoint.Target)
if err != nil {
err = fmt.Errorf("Error creating '%v' input: %v", endpoint.Type, err)
}
return result, err
} else {
return nil, errors.New("Unknown input endpoint type: " + string(endpoint.Type))
}
}
}
// Writer returns an instance of SampleWriter, configured by the values stored in the EndpointFactory.
func (f *EndpointFactory) Writer() SampleWriter {
return SampleWriter{f.FlagParallelHandler}
}
// CreateInput creates a SampleSink object based on the given output endpoint description
// and the configuration flags in the EndpointFactory.
func (f *EndpointFactory) CreateOutput(output string) (SampleProcessor, error) {
endpoint, err := f.ParseEndpointDescription(output, true)
if err != nil {
return nil, err
}
var marshaller Marshaller
if format := endpoint.OutputFormat(); format != UndefinedFormat {
marshaller, err = f.CreateMarshaller(format)
if err != nil {
return nil, err
}
}
resultSink, marshallingSink, err := f.createOutput(endpoint, &marshaller)
if err != nil {
return nil, err
}
if marshallingSink != nil {
marshallingSink.SetMarshaller(marshaller)
marshallingSink.Writer = f.Writer()
}
return resultSink, nil
}
func (f *EndpointFactory) createOutput(endpoint EndpointDescription, marshaller *Marshaller) (SampleProcessor, *AbstractMarshallingSampleOutput, error) {
switch endpoint.Type {
case StdEndpoint:
sink := NewConsoleSink()
if txt, ok := (*marshaller).(TextMarshaller); ok {
txt.AssumeStdout = true
*marshaller = txt
} else if txt2, ok2 := (*marshaller).(*TextMarshaller); ok2 {
txt2.AssumeStdout = true
}
return sink, &sink.AbstractMarshallingSampleOutput, nil
case FileEndpoint:
sink := &FileSink{
Filename: endpoint.Target,
IoBuffer: f.FlagIoBuffer,
CleanFiles: f.FlagOutputFilesClean,
Append: f.FlagFilesAppend,
VanishedFileCheck: f.FlagFileVanishedCheck,
}
return sink, &sink.AbstractMarshallingSampleOutput, nil
case TcpEndpoint:
sink := &TCPSink{
Endpoint: endpoint.Target,
DialTimeout: tcpDialTimeout,
}
sink.TcpConnLimit = f.FlagTcpConnectionLimit
if f.FlagTcpLogReceivedData {
sink.LogReceivedTraffic = log.ErrorLevel
}
return sink, &sink.AbstractMarshallingSampleOutput, nil
case TcpListenEndpoint:
sink := &TCPListenerSink{
Endpoint: endpoint.Target,
BufferedSamples: f.FlagOutputTcpListenBuffer,
}
sink.TcpConnLimit = f.FlagTcpConnectionLimit
if f.FlagTcpLogReceivedData {
sink.LogReceivedTraffic = log.ErrorLevel
}
return sink, &sink.AbstractMarshallingSampleOutput, nil
case HttpEndpoint:
theUrl, err := url.Parse("http://" + endpoint.Target)
if err != nil {
return nil, nil, err
}
sink := &HttpServerSink{
Endpoint: theUrl.Host,
RootPathPrefix: theUrl.Path,
SubPathTag: strings.Join(theUrl.Query()["tag"], ""),
BufferedSamples: f.FlagOutputTcpListenBuffer,
}
sink.TcpConnLimit = f.FlagTcpConnectionLimit
if f.FlagTcpLogReceivedData {
sink.LogReceivedTraffic = log.ErrorLevel
}
return sink, &sink.AbstractMarshallingSampleOutput, nil
default:
if factory, ok := f.CustomDataSinks[endpoint.Type]; ok && endpoint.IsCustomType {
sink, err := factory(endpoint.Target)
if err != nil {
err = fmt.Errorf("Error creating '%v' output: %v", endpoint.Type, err)
}
return sink, nil, err
} else {
return nil, nil, errors.New("Unknown output endpoint type: " + string(endpoint.Type))
}
}
}
func (f *EndpointFactory) CreateMarshaller(format MarshallingFormat) (Marshaller, error) {
factory, ok := f.Marshallers[format]
if !ok {
return nil, fmt.Errorf("Unknown marshaller format: %v", format)
}
return factory(), nil
}
// IsConsoleOutput returns true if the given processor will output to the standard output when started.
func IsConsoleOutput(sink SampleProcessor) bool {
writer, ok1 := sink.(*WriterSink)
_, ok2 := sink.(*ConsoleBoxSink)
return (ok1 && writer.Output == os.Stdout) || ok2
}
// EndpointDescription describes a data endpoint, regardless of the data direction
// (input or output).
type EndpointDescription struct {
Format MarshallingFormat
Type EndpointType
IsCustomType bool
Target string
Params map[string]string
}
// OutputFormat returns the MarshallingFormat that should be used when sending
// data to the described endpoint.
func (e EndpointDescription) OutputFormat() MarshallingFormat {
format := e.Format
if format == UndefinedFormat {
format = e.DefaultOutputFormat()
}
return format
}
// DefaultOutputFormat returns the default MarshallingFormat that should be used when sending
// data to the described endpoint, if no format is specified by the user.
func (e EndpointDescription) DefaultOutputFormat() MarshallingFormat {
switch e.Type {
case TcpEndpoint, TcpListenEndpoint:
return BinaryFormat
case FileEndpoint:
if strings.HasSuffix(e.Target, binaryFileSuffix) {
return BinaryFormat
}
return CsvFormat
case HttpEndpoint:
return CsvFormat
case StdEndpoint:
return TextFormat
default:
if e.IsCustomType {
return UndefinedFormat
} else {
panic(fmt.Sprintf("Unknown endpoint type: %v", e.Type))
}
}
}
// ParseEndpointDescription parses the given string to an EndpointDescription object.
// The string can be one of two forms: the URL-style description will be parsed by
// ParseUrlEndpointDescription, other descriptions will be parsed by GuessEndpointDescription.
func (f *EndpointFactory) ParseEndpointDescription(endpoint string, isOutput bool) (EndpointDescription, error) {
if strings.Contains(endpoint, "://") {
return f.ParseUrlEndpointDescription(endpoint)
} else {
guessed, err := GuessEndpointDescription(endpoint)
// Special case: Correct the default output transport type for standard output to ConsoleBoxEndpoint
if err == nil && isOutput {
if guessed.Target == stdTransportTarget && guessed.Format == UndefinedFormat {
guessed.Type = ConsoleBoxEndpoint
guessed.IsCustomType = true
}
}
return guessed, err
}
}
// ParseUrlEndpointDescription parses the endpoint string as a URL endpoint description.
// It has the form:
// format+transport://target
//
// One of the format and transport parts must be specified, optionally both.
// If one of format or transport is missing, it will be guessed.
// The order does not matter. The 'target' part must not be empty.
func (f *EndpointFactory) ParseUrlEndpointDescription(endpoint string) (res EndpointDescription, err error) {
urlParts := strings.SplitN(endpoint, "://", 2)
if len(urlParts) != 2 || urlParts[0] == "" || urlParts[1] == "" {
err = fmt.Errorf("Invalid URL endpoint: %v", endpoint)
return
}
target := urlParts[1]
res.Target = target
for _, part := range strings.Split(urlParts[0], "+") {
if err = f.parseEndpointUrlPart(part, &res, endpoint, target); err != nil {
return
}
}
if res.Type == UndefinedEndpoint {
var guessErr error
res.Type, guessErr = GuessEndpointType(target)
if guessErr != nil {
err = guessErr
}
}
if res.IsCustomType && res.Format != UndefinedFormat {
err = fmt.Errorf("Cannot define the data format for transport '%v'", res.Type)
}
return
}
func (f *EndpointFactory) parseEndpointUrlPart(part string, res *EndpointDescription, endpoint, target string) error {
// TODO unclean: this parsing method is used for both marshalling/unmarshalling endpoints
if f.isMarshallingFormat(part) {
if res.Format != "" {
return fmt.Errorf("Multiple formats defined in: %v", endpoint)
}
res.Format = MarshallingFormat(part)
} else {
if res.Type != UndefinedEndpoint {
return fmt.Errorf("Multiple transport types defined: %v", endpoint)
}
switch EndpointType(part) {
case TcpEndpoint, TcpListenEndpoint, FileEndpoint, HttpEndpoint:
res.Type = EndpointType(part)
case StdEndpoint:
if target != stdTransportTarget {
return fmt.Errorf("Transport '%v' can only be defined with target '%v'", part, stdTransportTarget)
}
res.Type = StdEndpoint
default:
res.IsCustomType = true
res.Type = EndpointType(part)
}
}
return nil
}
func (f *EndpointFactory) isMarshallingFormat(formatName string) bool {
_, ok := f.Marshallers[MarshallingFormat(formatName)]
return ok
}
// GuessEndpointDescription guesses the transport type and format of the given endpoint target.
// See GuessEndpointType for details.
func GuessEndpointDescription(endpoint string) (res EndpointDescription, err error) {
res.Target = endpoint
res.Type, err = GuessEndpointType(endpoint)
return
}
// GuessEndpointType guesses the EndpointType for the given target.
// Three forms of are recognized for the target:
// - A host:port pair indicates an active TCP endpoint
// - A :port pair (without the host part, but with the colon) indicates a passive TCP endpoint listening on the given port.
// - The hyphen '-' is interpreted as standard input/output.
// - All other targets are treated as file names.
func GuessEndpointType(target string) (EndpointType, error) {
var typ EndpointType
if target == "" {
return UndefinedEndpoint, errors.New("Empty endpoint/file is not valid")
} else if target == stdTransportTarget {
typ = StdEndpoint
} else {
host, port, err1 := net.SplitHostPort(target)
_, err2 := net.LookupPort("tcp", port)
if err1 == nil && err2 == nil {
if host == "" {
typ = TcpListenEndpoint
} else {
typ = TcpEndpoint
}
} else {
if strings.Contains(target, ":") || !IsValidFilename(target) {
var err golib.MultiError
err.Add(err1)
err.Add(err2)
return UndefinedEndpoint, fmt.Errorf("Not a filename and not a valid TCP endpoint: %v (%v)", target, err.NilOrError())
}
typ = FileEndpoint
}
}
log.Debugf("Guessed transport type of %v: %v", target, typ)
return typ, nil
}
// IsValidFilename tries to infer in a system-independent way, if the given path is a valid file name.
func IsValidFilename(path string) bool {
_, err := os.Stat(path)
switch err := err.(type) {
case *os.PathError:
return err == nil || err.Err == nil || err.Err.Error() != "invalid argument"
default:
return true
}
}
type sourceTagger string
func (t sourceTagger) HandleSample(sample *Sample, source string) {
sample.SetTag(string(t), source)
}