Skip to content

Commit

Permalink
[receiver/tcpcheck] New receiver
Browse files Browse the repository at this point in the history
Signed-off-by: huangyanfeng <huangyanfeng1992@gmail.com>
  • Loading branch information
yanfeng1992 committed Aug 8, 2024
1 parent f4442aa commit 5d5cdf3
Show file tree
Hide file tree
Showing 30 changed files with 1,819 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ body:
- receiver/sshcheck
- receiver/statsd
- receiver/syslog
- receiver/tcpcheck
- receiver/tcplog
- receiver/udplog
- receiver/vcenter
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ body:
- receiver/sshcheck
- receiver/statsd
- receiver/syslog
- receiver/tcpcheck
- receiver/tcplog
- receiver/udplog
- receiver/vcenter
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ body:
- receiver/sshcheck
- receiver/statsd
- receiver/syslog
- receiver/tcpcheck
- receiver/tcplog
- receiver/udplog
- receiver/vcenter
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/unmaintained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ body:
- receiver/sshcheck
- receiver/statsd
- receiver/syslog
- receiver/tcpcheck
- receiver/tcplog
- receiver/udplog
- receiver/vcenter
Expand Down
1 change: 1 addition & 0 deletions receiver/tcpcheckreceiver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
44 changes: 44 additions & 0 deletions receiver/tcpcheckreceiver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# TCP Check Receiver

<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [alpha]: metrics |
| Distributions | [contrib] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Areceiver%2Ftcpcheck%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Areceiver%2Ftcpcheck) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Areceiver%2Ftcpcheck%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Areceiver%2Ftcpcheck) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@yanfeng1992](https://www.github.com/yanfeng1992) |

[alpha]: https://github.com/open-telemetry/opentelemetry-collector#alpha
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
<!-- end autogenerated section -->

This receiver creates stats by connecting to an TCP server.


## Configuration

The following settings are required:
- `endpoint`



The following settings are optional:

- `collection_interval` (default = `60s`): This receiver collects metrics on an interval. Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.


### Example Configuration

```yaml
receivers:
sshcheck:
endpoint: localhost:80
collection_interval: 60s
```
The full list of settings exposed for this receiver are documented [here](./config.go) with detailed sample configurations [here](./testdata/config.yaml).
## Metrics
Details about the metrics produced by this receiver can be found in [metadata.yaml](./metadata.yaml)
40 changes: 40 additions & 0 deletions receiver/tcpcheckreceiver/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"errors"
"net"
"strings"

"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/configtcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/metadata"
)

// Predefined error responses for configuration validation failures
var (
errMissingEndpoint = errors.New(`"endpoint" not specified in config`)
errInvalidEndpoint = errors.New(`"endpoint" is invalid`)
errConfigNotTCPCheck = errors.New("config was not a TCP check receiver config")
)

type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
configtcp.TCPClientSettings `mapstructure:",squash"`
MetricsBuilderConfig metadata.MetricsBuilderConfig `mapstructure:",squash"`
}

func (c Config) Validate() (err error) {
if c.TCPClientSettings.Endpoint == "" {
err = multierr.Append(err, errMissingEndpoint)
} else if strings.Contains(c.TCPClientSettings.Endpoint, " ") {
err = multierr.Append(err, errInvalidEndpoint)
} else if _, _, splitErr := net.SplitHostPort(c.TCPClientSettings.Endpoint); splitErr != nil {
err = multierr.Append(splitErr, errInvalidEndpoint)
}
return
}
105 changes: 105 additions & 0 deletions receiver/tcpcheckreceiver/config_test.go
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 tcpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver"

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"

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

// check that OTel Collector patterns are implemented
func TestCheckConfig(t *testing.T) {
t.Parallel()
if err := componenttest.CheckConfigStruct(&Config{}); err != nil {
t.Fatal(err)
}
}

// test the validate function for config
func TestValidate(t *testing.T) {
t.Parallel()
testCases := []struct {
desc string
cfg *Config
expectedErr error
}{
{
desc: "missing endpoint",
cfg: &Config{
TCPClientSettings: configtcp.TCPClientSettings{
Endpoint: "",
},
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(errMissingEndpoint),
},
{
desc: "invalid endpoint",
cfg: &Config{
TCPClientSettings: configtcp.TCPClientSettings{
Endpoint: "badendpoint . cuz spaces:443",
},
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(errInvalidEndpoint),
},
{
desc: "no error",
cfg: &Config{
TCPClientSettings: configtcp.TCPClientSettings{
Endpoint: "localhost:8080",
},
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: error(nil),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
actualErr := tc.cfg.Validate()
if tc.expectedErr != nil {
require.EqualError(t, actualErr, tc.expectedErr.Error())
} else {
require.NoError(t, actualErr)
}
})
}
}

func TestLoadConfig(t *testing.T) {
// load test config
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
rcvrs, err := cm.Sub("receivers")
require.NoError(t, err)
tcpconf, err := rcvrs.Sub("tcpcheck")
require.NoError(t, err)
// unmarshal to receiver config
actualConfig, ok := NewFactory().CreateDefaultConfig().(*Config)
require.True(t, ok)
require.NoError(t, tcpconf.Unmarshal(actualConfig))

// set expected config
expectedConfig, ok := NewFactory().CreateDefaultConfig().(*Config)
require.True(t, ok)

expectedConfig.ControllerConfig = scraperhelper.ControllerConfig{
InitialDelay: time.Second,
CollectionInterval: 60 * time.Second,
}
expectedConfig.TCPClientSettings = configtcp.TCPClientSettings{
Endpoint: "localhost:80",
Timeout: 10 * time.Second,
}
require.Equal(t, expectedConfig, actualConfig)
}
6 changes: 6 additions & 0 deletions receiver/tcpcheckreceiver/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

//go:generate mdatagen metadata.yaml
55 changes: 55 additions & 0 deletions receiver/tcpcheckreceiver/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# tcpcheck

## Default Metrics

The following metrics are emitted by default. Each of them can be disabled by applying the following configuration:

```yaml
metrics:
<metric_name>:
enabled: false
```
### tcpcheck.duration
Measures the duration of TCP connection.
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| ms | Gauge | Int |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| tcp.endpoint | Full TCO endpoint | Any Str |
### tcpcheck.error
Records errors occurring during TCP check.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| {error} | Sum | Int | Cumulative | false |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| error.message | Error message recorded during check | Any Str |
### tcpcheck.status
1 if the TCP client successfully connected, otherwise 0.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| 1 | Sum | Int | Cumulative | false |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| tcp.endpoint | Full TCO endpoint | Any Str |
53 changes: 53 additions & 0 deletions receiver/tcpcheckreceiver/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"context"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/configtcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/metadata"
)

// NewFactory creates a factory for tcpcheckreceiver receiver.
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
}

func createDefaultConfig() component.Config {
cfg := scraperhelper.NewDefaultControllerConfig()
cfg.CollectionInterval = 10 * time.Second

return &Config{
ControllerConfig: cfg,
TCPClientSettings: configtcp.TCPClientSettings{
Timeout: 10 * time.Second,
},
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}

func createMetricsReceiver(_ context.Context, params receiver.Settings, rConf component.Config, consumer consumer.Metrics) (receiver.Metrics, error) {
cfg, ok := rConf.(*Config)
if !ok {
return nil, errConfigNotTCPCheck
}

tcpCheckScraper := newScraper(cfg, params)
scraper, err := scraperhelper.NewScraper(metadata.Type.String(), tcpCheckScraper.scrape, scraperhelper.WithStart(tcpCheckScraper.start))
if err != nil {
return nil, err
}

return scraperhelper.NewScraperControllerReceiver(&cfg.ControllerConfig, params, consumer, scraperhelper.AddScraper(scraper))
}
Loading

0 comments on commit 5d5cdf3

Please sign in to comment.