-
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.
Signed-off-by: huangyanfeng <huangyanfeng1992@gmail.com>
- Loading branch information
1 parent
f4442aa
commit 5d5cdf3
Showing
30 changed files
with
1,819 additions
and
0 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
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
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 @@ | ||
include ../../Makefile.Common |
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,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) | ||
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,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 | ||
} |
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 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) | ||
} |
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,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 |
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,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 | |
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,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)) | ||
} |
Oops, something went wrong.