Skip to content

Commit

Permalink
[receiver/tcpcheck] Rebase Latest Version open-telemetry#34458
Browse files Browse the repository at this point in the history
  • Loading branch information
chengchuanpeng committed Jan 26, 2025
1 parent 8d6a795 commit ac801de
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .chloggen/tcp-checker.yaml
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: new_component

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: tcpcheckreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Introducing new component tcpcheck receiver

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34414]

# (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: []
44 changes: 44 additions & 0 deletions receiver/tcpcheckreceiver/internal/configtcp/configtcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package configtcp // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/configtcp"

import (
"context"
"net"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confignet"
)

type TCPClientSettings struct {
// Endpoint is always required
Endpoint string `mapstructure:"endpoint"`
Timeout time.Duration `mapstructure:"timeout"`
}

type Client struct {
net.Conn
TCPAddrConfig confignet.TCPAddrConfig
}

// Dial starts a TCP session.
func (c *Client) Dial() (err error) {
c.Conn, err = c.TCPAddrConfig.Dial(context.Background())
if err != nil {
return err
}
return nil
}

func (tcs *TCPClientSettings) ToClient(_ component.Host, _ component.TelemetrySettings) (*Client, error) {
return &Client{
TCPAddrConfig: confignet.TCPAddrConfig{
Endpoint: tcs.Endpoint,
DialerConfig: confignet.DialerConfig{
Timeout: tcs.Timeout,
},
},
}, nil
}
62 changes: 62 additions & 0 deletions receiver/tcpcheckreceiver/internal/configtcp/configtcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package configtcp // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/configtcp"

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/extension/auth/authtest"
)

type mockHost struct {
component.Host
ext map[component.ID]extension.Extension
}

func TestAllTCPClientSettings(t *testing.T) {
host := &mockHost{
ext: map[component.ID]extension.Extension{
component.MustNewID("testauth"): &authtest.MockClient{},
},
}

endpoint := "localhost:8080"
timeout := time.Second * 5

tests := []struct {
name string
settings TCPClientSettings
shouldError bool
}{
{
name: "valid_settings_endpoint",
settings: TCPClientSettings{
Endpoint: endpoint,
Timeout: timeout,
},
shouldError: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tt := componenttest.NewNopTelemetrySettings()
tt.TracerProvider = nil

client, err := test.settings.ToClient(host, tt)
if test.shouldError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

assert.EqualValues(t, client.TCPAddrConfig.Endpoint, test.settings.Endpoint)
assert.EqualValues(t, client.TCPAddrConfig.DialerConfig.Timeout, test.settings.Timeout)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resourceMetrics:
- resource: {}
scopeMetrics:
- metrics:
- description: Measures the duration of TCP connection.
gauge:
dataPoints:
- asInt: "36"
attributes:
- key: tcp.endpoint
value:
stringValue: 127.0.0.1:8080
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
name: tcpcheck.duration
unit: ns
- description: 1 if the TCP client successfully connected, otherwise 0.
name: tcpcheck.status
gauge:
dataPoints:
- asInt: "1"
attributes:
- key: tcp.endpoint
value:
stringValue: 127.0.0.1:8080
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
unit: "1"
scope:
name: otelcol/tcpcheckreceiver
version: latest
1 change: 1 addition & 0 deletions versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ module-sets:
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/systemdreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcplogreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tlscheckreceiver
- github.com/open-telemetry/opentelemetry-collector-contrib/receiver/udplogreceiver
Expand Down

0 comments on commit ac801de

Please sign in to comment.