-
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
Merged
Merged
Feature: TCP Input #6700
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
p.log.Errorw("Error starting the TCP server", "error", err) | ||
} | ||
p.started = true | ||
} | ||
} | ||
|
||
// Stop stops TCP server | ||
func (p *Input) Stop() { | ||
defer p.outlet.Close() | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
p.log.Info("Stopping TCP input") | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.