Skip to content
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

Add UDS support #2

Closed
21 changes: 21 additions & 0 deletions .chloggen/receiver_statsdreceiver_add-uds-support-part-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# 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: receiver/statsd

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add UDS support

# 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:
5 changes: 3 additions & 2 deletions receiver/statsdreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestValidate(t *testing.T) {
noObjectNameErr = "must specify object id for all TimerHistogramMappings"
statsdTypeNotSupportErr = "statsd_type is not a supported mapping for histogram and timing metrics: %s"
observerTypeNotSupportErr = "observer_type is not supported for histogram and timing metrics: %s"
invalidHistogramErr = "histogram configuration requires observer_type: histogram"
)

tests := []test{
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestValidate(t *testing.T) {
},
},
},
expectedErr: "histogram configuration requires observer_type: histogram",
expectedErr: invalidHistogramErr,
},
{
name: "negativeAggregationInterval",
Expand All @@ -163,7 +164,7 @@ func TestValidate(t *testing.T) {
{StatsdType: "timing", ObserverType: "gauge"},
},
},
expectedErr: "aggregation_interval must be a positive duration",
expectedErr: negativeAggregationIntervalErr,
},
}

Expand Down
81 changes: 81 additions & 0 deletions receiver/statsdreceiver/internal/testutil/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package testutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/testutil"

import (
"fmt"
"io"
"net"
"strings"
)

// StatsDTestClient defines the properties of a StatsD connection.
type StatsDTestClient struct {
transport string
address string
conn io.Writer
}

// NewStatsDTestClient creates a new StatsDTestClient instance to support the need for testing
// the statsdreceiver package and is not intended/tested to be used in production.
func NewStatsDTestClient(transport string, address string) (*StatsDTestClient, error) {
statsd := &StatsDTestClient{
transport: transport,
address: address,
}

err := statsd.connect()
if err != nil {
return nil, err
}

return statsd, nil
}

// connect populates the StatsDTestClient.conn
func (s *StatsDTestClient) connect() error {
switch s.transport {
case "udp":
udpAddr, err := net.ResolveUDPAddr(s.transport, s.address)
if err != nil {
return err
}
s.conn, err = net.DialUDP(s.transport, nil, udpAddr)
if err != nil {
return err
}
case "unixgram":
unixAddr, err := net.ResolveUnixAddr(s.transport, s.address)
if err != nil {
return err
}
s.conn, err = net.DialUnix(s.transport, nil, unixAddr)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown/unsupported transport: %s", s.transport)
}

return nil
}

// Disconnect closes the StatsDTestClient.conn.
func (s *StatsDTestClient) Disconnect() error {
var err error
if cl, ok := s.conn.(io.Closer); ok {
err = cl.Close()
}
s.conn = nil
return err
}

// SendMetric sends the input metric to the StatsDTestClient connection.
func (s *StatsDTestClient) SendMetric(metric Metric) error {
_, err := io.Copy(s.conn, strings.NewReader(metric.String()))
if err != nil {
return fmt.Errorf("send metric on test client: %w", err)
}
return nil
}
20 changes: 20 additions & 0 deletions receiver/statsdreceiver/internal/testutil/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package testutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/testutil"

import (
"fmt"
)

// Metric contains the metric fields for a StatsDTestClient message.
type Metric struct {
Name string
Value string
Type string
}

// String formats a Metric into a StatsDTestClient message.
func (m Metric) String() string {
return fmt.Sprintf("%s:%s|%s", m.Name, m.Value, m.Type)
}
16 changes: 16 additions & 0 deletions receiver/statsdreceiver/internal/testutil/temporary_socket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package testutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/internal/testutil"

import (
"crypto/rand"
"fmt"
"testing"
)

func CreateTemporarySocket(t testing.TB, _ string) string {
b := make([]byte, 10)
rand.Read(b)
return fmt.Sprintf("%s/%s", t.TempDir(), fmt.Sprintf("%x", b))
}
103 changes: 0 additions & 103 deletions receiver/statsdreceiver/internal/transport/client/client.go

This file was deleted.

Loading