-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: TCP Input #6700
Feature: TCP Input #6700
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
:type: tcp | ||
|
||
[id="{beatname_lc}-input-{type}"] | ||
=== TCP input | ||
|
||
++++ | ||
<titleabbrev>TCP</titleabbrev> | ||
++++ | ||
|
||
Use the `TCP` input to read events over TCP. | ||
|
||
Example configuration: | ||
|
||
["source","yaml",subs="attributes"] | ||
---- | ||
{beatname_lc}.inputs: | ||
- type: tcp | ||
max_message_size: 10240 | ||
host: "localhost:9000" | ||
---- | ||
|
||
|
||
==== Configuration options | ||
|
||
The `tcp` input supports the following configuration options plus the | ||
<<{beatname_lc}-input-{type}-common-options>> described later. | ||
|
||
[float] | ||
[id="{beatname_lc}-input-{type}-max-message-size"] | ||
==== `max_message_size` | ||
|
||
The maximum size of the message received over TCP. The default is `20MiB`. | ||
|
||
[float] | ||
[id="{beatname_lc}-input-{type}-host"] | ||
==== `host` | ||
|
||
The host and TCP port to listen on for event streams. | ||
|
||
[float] | ||
[id="{beatname_lc}-input-{type}-line-delimiter"] | ||
==== `line_delimiter` | ||
|
||
Specify the characters used to split the incoming events. The default is '\n'. | ||
|
||
[float] | ||
[id="{beatname_lc}-input-{type}-timeout"] | ||
==== `timeout` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't see this config in the reference config file. Also make sure to use |
||
|
||
The number of seconds of inactivity before a remote connection is closed. The default is `300s`. | ||
|
||
[id="{beatname_lc}-input-{type}-common-options"] | ||
include::../inputs/input-common-options.asciidoc[] | ||
|
||
:type!: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tcp | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/dustin/go-humanize" | ||
|
||
"github.com/elastic/beats/filebeat/harvester" | ||
"github.com/elastic/beats/filebeat/inputsource/tcp" | ||
) | ||
|
||
type config struct { | ||
tcp.Config `config:",inline"` | ||
harvester.ForwarderConfig `config:",inline"` | ||
} | ||
|
||
var defaultConfig = config{ | ||
ForwarderConfig: harvester.ForwarderConfig{ | ||
Type: "tcp", | ||
}, | ||
Config: tcp.Config{ | ||
LineDelimiter: "\n", | ||
Timeout: time.Minute * 5, | ||
MaxMessageSize: 20 * humanize.MiByte, | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package tcp | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/elastic/beats/filebeat/channel" | ||
"github.com/elastic/beats/filebeat/harvester" | ||
"github.com/elastic/beats/filebeat/input" | ||
"github.com/elastic/beats/filebeat/inputsource/tcp" | ||
"github.com/elastic/beats/filebeat/util" | ||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/common/cfgwarn" | ||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
func init() { | ||
err := input.Register("tcp", NewInput) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Input for TCP connection | ||
type Input struct { | ||
sync.Mutex | ||
server *tcp.Server | ||
started bool | ||
outlet channel.Outleter | ||
config *config | ||
log *logp.Logger | ||
} | ||
|
||
// NewInput creates a new TCP input | ||
func NewInput( | ||
cfg *common.Config, | ||
outlet channel.Factory, | ||
context input.Context, | ||
) (input.Input, error) { | ||
cfgwarn.Experimental("TCP input type is used") | ||
|
||
out, err := outlet(cfg, context.DynamicFields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
forwarder := harvester.NewForwarder(out) | ||
|
||
config := defaultConfig | ||
err = cfg.Unpack(&config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cb := func(data []byte, metadata tcp.Metadata) { | ||
event := createEvent(data, metadata) | ||
forwarder.Send(event) | ||
} | ||
|
||
server, err := tcp.New(cb, &config.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Input{ | ||
server: server, | ||
started: false, | ||
outlet: out, | ||
config: &config, | ||
log: logp.NewLogger("tcp input").With(config.Config.Host), | ||
}, nil | ||
} | ||
|
||
// Run start a TCP input | ||
func (p *Input) Run() { | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
if !p.started { | ||
p.log.Info("Starting TCP input") | ||
err := p.server.Start() | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this return the error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would require changes in the upstream interface, I can create a followup issue on that? // Input is the interface common to all input
type Input interface {
Run()
Stop()
Wait()
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general the way we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agree on that point but I think changing that interface is out of scope for this PR, so do you agree the plan of action is to do a followup issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if we follow up on this ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. created #6771 |
||
p.log.Errorw("Error starting the TCP server", "error", err) | ||
} | ||
p.started = true | ||
} | ||
} | ||
|
||
// Stop stops TCP server | ||
func (p *Input) Stop() { | ||
p.log.Info("Stopping TCP input") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logging is not congruent with the UDP input. In UDP the message is logged after acquiring the lock. IDK which order, but I would like the behavior to be the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, done in the next commit. |
||
defer p.outlet.Close() | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
p.server.Stop() | ||
p.started = false | ||
} | ||
|
||
// Wait stop the current server | ||
func (p *Input) Wait() { | ||
p.Stop() | ||
} | ||
|
||
func createEvent(raw []byte, metadata tcp.Metadata) *util.Data { | ||
data := util.NewData() | ||
data.Event = beat.Event{ | ||
Timestamp: time.Now(), | ||
Fields: common.MapStr{ | ||
"message": string(raw), | ||
"source": metadata.RemoteAddr.String(), | ||
}, | ||
} | ||
return data | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tcp | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/filebeat/inputsource/tcp" | ||
) | ||
|
||
func TestCreateEvent(t *testing.T) { | ||
hello := "hello world" | ||
ip := "127.0.0.1" | ||
parsedIP := net.ParseIP(ip) | ||
addr := &net.IPAddr{IP: parsedIP, Zone: ""} | ||
|
||
message := []byte(hello) | ||
mt := tcp.Metadata{RemoteAddr: addr} | ||
|
||
data := createEvent(message, mt) | ||
event := data.GetEvent() | ||
|
||
m, err := event.GetValue("message") | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(message), m) | ||
|
||
from, _ := event.GetValue("source") | ||
assert.Equal(t, ip, from) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package tcp | ||
|
||
import ( | ||
"bufio" | ||
"net" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
// Client is a remote client. | ||
type client struct { | ||
conn net.Conn | ||
log *logp.Logger | ||
callback CallbackFunc | ||
done chan struct{} | ||
metadata Metadata | ||
splitFunc bufio.SplitFunc | ||
maxReadMessage size | ||
timeout time.Duration | ||
} | ||
|
||
func newClient( | ||
conn net.Conn, | ||
log *logp.Logger, | ||
callback CallbackFunc, | ||
splitFunc bufio.SplitFunc, | ||
maxReadMessage size, | ||
timeout time.Duration, | ||
) *client { | ||
client := &client{ | ||
conn: conn, | ||
log: log.With("address", conn.RemoteAddr()), | ||
callback: callback, | ||
done: make(chan struct{}), | ||
splitFunc: splitFunc, | ||
maxReadMessage: maxReadMessage, | ||
timeout: timeout, | ||
metadata: Metadata{ | ||
RemoteAddr: conn.RemoteAddr(), | ||
}, | ||
} | ||
return client | ||
} | ||
|
||
func (c *client) handle() error { | ||
r := NewResetableLimitedReader(NewDeadlineReader(c.conn, c.timeout), uint64(c.maxReadMessage)) | ||
buf := bufio.NewReader(r) | ||
scanner := bufio.NewScanner(buf) | ||
scanner.Split(c.splitFunc) | ||
|
||
for scanner.Scan() { | ||
err := scanner.Err() | ||
if err != nil { | ||
// we are forcing a close on the socket, lets ignore any error that could happen. | ||
select { | ||
case <-c.done: | ||
break | ||
default: | ||
} | ||
// This is a user defined limit and we should notify the user. | ||
if IsMaxReadBufferErr(err) { | ||
c.log.Errorw("client errors", "error", err) | ||
} | ||
return errors.Wrap(err, "tcp client error") | ||
} | ||
r.Reset() | ||
c.callback(scanner.Bytes(), c.metadata) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *client) close() { | ||
close(c.done) | ||
c.conn.Close() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember @andrewkroh suggested to not have a default and require the use to specify it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently don't have any default on this value, I've provided an example hosts/port, but It's not a hard default.
Following your comment, I did a small change and added a more explicit error for the host.