Skip to content

Commit

Permalink
Refactor 2nd Version
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenoCC-Peng committed Dec 13, 2024
1 parent 31f1d8c commit bd296ce
Show file tree
Hide file tree
Showing 6 changed files with 536 additions and 160 deletions.
15 changes: 8 additions & 7 deletions receiver/tcpcheckreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ package tcpcheckreceiver // import "github.com/open-telemetry/opentelemetry-coll
import (
"errors"
"fmt"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/metadata"
"go.opentelemetry.io/collector/config/confignet"
"net"
"strconv"
"strings"

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

"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
)
Expand All @@ -28,7 +27,7 @@ var (
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
metadata.MetricsBuilderConfig `mapstructure:",squash"`
Targets []*confignet.TCPAddrConfig `mapstructure:"targets"`
tcpConfigs []*confignet.TCPAddrConfig `mapstructure:",squash"`
}

func validatePort(port string) error {
Expand Down Expand Up @@ -69,13 +68,15 @@ func validateTarget(cfg *confignet.TCPAddrConfig) error {
func (cfg *Config) Validate() error {
var err error

if len(cfg.Targets) == 0 {
if len(cfg.tcpConfigs) == 0 {
err = multierr.Append(err, errMissingTargets)
}

for _, target := range cfg.Targets {
err = multierr.Append(err, validateTarget(target))
for _, tcpConfig := range cfg.tcpConfigs {
err = multierr.Append(err, validateTarget(tcpConfig))
}

return err
}

// createcient
63 changes: 39 additions & 24 deletions receiver/tcpcheckreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,35 @@ package tcpcheckreceiver // import "github.com/open-telemetry/opentelemetry-coll

import (
"context"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcpcheckreceiver/internal/metadata"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confignet"
"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/metadata"
)

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

//func createDefaultConfig() component.Config {
// cfg := scraperhelper.NewDefaultControllerConfig()
// // ??
// cfg.CollectionInterval = 10 * time.Second
//
// return &Config{
// ControllerConfig: cfg,
// // do we need to add timeout?
// TCPClientSettings: configtcp.TCPClientSettings{
// Timeout: 10 * time.Second,
// },
// MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
// }
//}

func createDefaultConfig() component.Config {
// to do where to set timeout
func newDefaultConfig() component.Config {
cfg := scraperhelper.NewDefaultControllerConfig()

return &Config{
ControllerConfig: cfg,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Targets: []*confignet.TCPAddrConfig{},
tcpConfigs: []*confignet.TCPAddrConfig{},
}
}

func createMetricsReceiver(
// todo: match new version
func newReceiver(
_ context.Context,
settings receiver.Settings,
cfg component.Config,
Expand All @@ -59,7 +44,8 @@ func createMetricsReceiver(
return nil, errConfigTCPCheck
}

mp := newScraper(tlsCheckConfig, settings, getConnectionState)
//mp := newScraper(tlsCheckConfig, settings, getConnectionState)
mp := newScraper(tlsCheckConfig, settings)
s, err := scraperhelper.NewScraperWithoutType(mp.scrape)
if err != nil {
return nil, err
Expand All @@ -73,3 +59,32 @@ func createMetricsReceiver(
opt,
)
}

// timeout ??
//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, tcpCheckScraper.scrape, scraperhelper.WithStart(tcpCheckScraper.start))
// if err != nil {
// return nil, err
// }
//
// return scraperhelper.NewScraperControllerReceiver(&cfg.ControllerConfig, params, consumer, scraperhelper.AddScraper(scraper))
//}
137 changes: 93 additions & 44 deletions receiver/tcpcheckreceiver/internal/configtcp/configtcp.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,93 @@
// 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
}
//// 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 "C"
//import (
// "context"
// "errors"
// "fmt"
// "go.opentelemetry.io/collector/config/confignet"
// "net"
// "strconv"
// "strings"
//)
//
//// Predefined error responses for configuration validation failures
//var (
// errInvalidEndpoint = errors.New(`"Endpoint" must be in the form of <hostname>:<port>`)
// errMissingTargets = errors.New(`No targets specified`)
// errConfigTCPCheck = errors.New(`Invalid Config`)
//)
//
//// X this
//func (tcs *TCPClientSettings) ToClient() (*Client, error) {
// TCPAddrConfig: confignet.TCPAddrConfig{
// Endpoint: tcs.Endpoint,
// DialerConfig: confignet.DialerConfig{
// Timeout: tcs.Timeout,
// },
// },
// }
//}
//
//func validatePort(port string) error {
// portNum, err := strconv.Atoi(port)
// if err != nil {
// return fmt.Errorf("provided port is not a number: %s", port)
// }
// if portNum < 1 || portNum > 65535 {
// return fmt.Errorf("provided port is out of valid range (1-65535): %d", portNum)
// }
// return nil
//}
//
//func (tcs *TCPClientSettings) ValidateTarget() error {
// var err error
//
// if tcs.Endpoint == "" {
// return errMissingTargets
// }
//
// if strings.Contains(tcs.Endpoint, "://") {
// return fmt.Errorf("endpoint contains a scheme, which is not allowed: %s", tcs.Endpoint)
// }
//
// _, port, parseErr := net.SplitHostPort(tcs.Endpoint)
// if parseErr != nil {
// return fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr)
// }
//
// portParseErr := validatePort(port)
// if portParseErr != nil {
// return fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), portParseErr)
// }
//
// return err
//}
//
//// Client
//type Client struct {
// Connection net.Conn
// Listener net.Listener
// TCPAddrConfig confignet.TCPAddrConfig
//}
//
//// Dial starts a TCP session.
//func (c *Client) Dial() (err error) {
// c.Connection, err = c.TCPAddrConfig.Dial(context.Background())
// if err != nil {
// return err
// }
// return nil
//}
//
//// mock
//func (c *Client) Listen() (err error) {
// c.Listener, err = c.TCPAddrConfig.Listen(context.Background())
// if err != nil {
// return err
// }
// return nil
//}
Loading

0 comments on commit bd296ce

Please sign in to comment.