-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add statsdreceiver Unixgram Support (#36608)
#### Description Adds `unixgram` transport for the `statsdreceiver`. Additionally, creates a new `packetServer` base class for both `UDS` and `UDP*` transport types #### Link to tracking issue #21385 #### Testing Added a unit test --------- Co-authored-by: Christos Markou <chrismarkou92@gmail.com>
- Loading branch information
1 parent
23306ea
commit c208ea2
Showing
9 changed files
with
204 additions
and
64 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 UDS 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: [21385] | ||
|
||
# (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
84 changes: 84 additions & 0 deletions
84
receiver/statsdreceiver/internal/transport/packet_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,84 @@ | ||
// 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 ( | ||
"errors" | ||
"net" | ||
|
||
"go.opentelemetry.io/collector/consumer" | ||
) | ||
|
||
type packetServer struct { | ||
packetConn net.PacketConn | ||
transport Transport | ||
} | ||
|
||
// ListenAndServe starts the server ready to receive metrics. | ||
func (u *packetServer) ListenAndServe( | ||
nextConsumer consumer.Metrics, | ||
reporter Reporter, | ||
transferChan chan<- Metric, | ||
) error { | ||
if nextConsumer == nil || reporter == nil { | ||
return errNilListenAndServeParameters | ||
} | ||
|
||
buf := make([]byte, 65527) // max size for udp packet body (assuming ipv6) | ||
for { | ||
n, addr, err := u.packetConn.ReadFrom(buf) | ||
if addr == nil && u.transport == UDS { | ||
addr = &udsAddr{ | ||
network: u.transport.String(), | ||
address: u.packetConn.LocalAddr().String(), | ||
} | ||
} | ||
|
||
if n > 0 { | ||
u.handlePacket(n, buf, addr, transferChan) | ||
} | ||
if err != nil { | ||
reporter.OnDebugf("%s Transport (%s) - ReadFrom error: %v", | ||
u.transport, | ||
u.packetConn.LocalAddr(), | ||
err) | ||
var netErr net.Error | ||
if errors.As(err, &netErr) { | ||
if netErr.Timeout() { | ||
continue | ||
} | ||
} | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// handlePacket is helper that parses the buffer and split it line by line to be parsed upstream. | ||
func (u *packetServer) handlePacket( | ||
numBytes int, | ||
data []byte, | ||
addr net.Addr, | ||
transferChan chan<- Metric, | ||
) { | ||
splitPacket := NewSplitBytes(data[:numBytes], '\n') | ||
for splitPacket.Next() { | ||
chunk := splitPacket.Chunk() | ||
if len(chunk) > 0 { | ||
transferChan <- Metric{string(chunk), addr} | ||
} | ||
} | ||
} | ||
|
||
type udsAddr struct { | ||
network string | ||
address string | ||
} | ||
|
||
func (u *udsAddr) Network() string { | ||
return u.network | ||
} | ||
|
||
func (u *udsAddr) String() string { | ||
return u.address | ||
} |
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,42 @@ | ||
// 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 ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
type udsServer struct { | ||
packetServer | ||
} | ||
|
||
// Ensure that Server is implemented on UDS Server. | ||
var _ (Server) = (*udsServer)(nil) | ||
|
||
// NewUDSServer creates a transport.Server using Unixgram as its transport. | ||
func NewUDSServer(transport Transport, socketPath string) (Server, error) { | ||
if !transport.IsPacketTransport() { | ||
return nil, fmt.Errorf("NewUDSServer with %s: %w", transport.String(), ErrUnsupportedPacketTransport) | ||
} | ||
|
||
conn, err := net.ListenPacket(transport.String(), socketPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("starting to listen %s socket: %w", transport.String(), err) | ||
} | ||
|
||
return &udsServer{ | ||
packetServer: packetServer{ | ||
packetConn: conn, | ||
transport: transport, | ||
}, | ||
}, nil | ||
} | ||
|
||
// Close closes the server. | ||
func (u *udsServer) Close() error { | ||
os.Remove(u.packetConn.LocalAddr().String()) | ||
return u.packetConn.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
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