-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/statsd] add TCP support to statsdreceiver (#26700)
**Description:** Adds TCP support to statsdreceiver **Link to tracking Issue:** Fixes #23327 **Testing:** Unit tests **Documentation:** No changes. --------- Co-authored-by: Joshua MacDonald <[email protected]>
- Loading branch information
Showing
5 changed files
with
170 additions
and
18 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'enhancement' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: statsdreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add TCP support to statsdreceiver | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [23327] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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
105 changes: 105 additions & 0 deletions
105
receiver/statsdreceiver/internal/transport/tcp_server.go
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,105 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package transport // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/transport" | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"net" | ||
"strings" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/consumer" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/protocol" | ||
) | ||
|
||
var errTCPServerDone = errors.New("server stopped") | ||
|
||
type tcpServer struct { | ||
listener net.Listener | ||
reporter Reporter | ||
wg sync.WaitGroup | ||
stopChan chan struct{} | ||
} | ||
|
||
var _ Server = (*tcpServer)(nil) | ||
|
||
// NewTCPServer creates a transport.Server using TCP as its transport. | ||
func NewTCPServer(addr string) (Server, error) { | ||
l, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
t := tcpServer{ | ||
listener: l, | ||
stopChan: make(chan struct{}), | ||
} | ||
return &t, nil | ||
} | ||
|
||
func (t *tcpServer) ListenAndServe(parser protocol.Parser, nextConsumer consumer.Metrics, reporter Reporter, transferChan chan<- Metric) error { | ||
if parser == nil || nextConsumer == nil || reporter == nil { | ||
return errNilListenAndServeParameters | ||
} | ||
|
||
t.reporter = reporter | ||
LOOP: | ||
for { | ||
connChan := make(chan net.Conn, 1) | ||
go func() { | ||
c, err := t.listener.Accept() | ||
if err != nil { | ||
t.reporter.OnDebugf("TCP Transport - Accept error: %v", | ||
err) | ||
} else { | ||
connChan <- c | ||
} | ||
}() | ||
|
||
select { | ||
case conn := <-connChan: | ||
t.wg.Add(1) | ||
go t.handleConn(conn, transferChan) | ||
case <-t.stopChan: | ||
break LOOP | ||
} | ||
} | ||
return errTCPServerDone | ||
} | ||
|
||
func (t *tcpServer) handleConn(c net.Conn, transferChan chan<- Metric) { | ||
payload := make([]byte, 4096) | ||
var remainder []byte | ||
for { | ||
n, err := c.Read(payload) | ||
if err != nil { | ||
t.reporter.OnDebugf("TCP transport (%s) Error reading payload: %v", c.LocalAddr(), err) | ||
t.wg.Done() | ||
return | ||
} | ||
buf := bytes.NewBuffer(append(remainder, payload[0:n]...)) | ||
for { | ||
bytes, err := buf.ReadBytes((byte)('\n')) | ||
if errors.Is(err, io.EOF) { | ||
if len(bytes) != 0 { | ||
remainder = bytes | ||
} | ||
break | ||
} | ||
line := strings.TrimSpace(string(bytes)) | ||
if line != "" { | ||
transferChan <- Metric{line, c.LocalAddr()} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (t *tcpServer) Close() error { | ||
close(t.stopChan) | ||
t.wg.Wait() | ||
return t.listener.Close() | ||
} |
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