Skip to content

Commit

Permalink
fix: add flag to skip fail if the endpoint is not available
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <bence.csati@axoflow.com>
  • Loading branch information
csatib02 committed Dec 4, 2024
1 parent df7ad28 commit 9dbb6dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type TCPClientSettings struct {
type Config struct {
TCPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.

// SkipFailOnInvalidTCPEndpoint controls whether to fail if the endpoint is invalid.
// This is useful for cases where the collector is started before the endpoint becomes available.
SkipFailOnInvalidTCPEndpoint bool `mapstructure:"skip_fail_on_invalid_tcp_endpoint"`

// RequireAck enables the acknowledgement feature.
RequireAck bool `mapstructure:"require_ack"`

Expand Down Expand Up @@ -87,8 +91,11 @@ func (config *Config) Validate() error {

// Resolve TCP address just to ensure that it is a valid one. It is better
// to fail here than at when the exporter is started.
if _, err := net.ResolveTCPAddr("tcp", config.Endpoint); err != nil {
return fmt.Errorf("exporter has an invalid TCP endpoint: %w", err)
if !config.SkipFailOnInvalidTCPEndpoint {
if _, err := net.ResolveTCPAddr("tcp", config.Endpoint); err != nil {
return fmt.Errorf("exporter has an invalid TCP endpoint: %w", err)
}
}

return nil
}
11 changes: 11 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ func TestConfigValidate(t *testing.T) {
},
err: fmt.Errorf("exporter has an invalid TCP endpoint: address http://localhost:24224: too many colons in address"),
},
{
desc: "Endpoint is invalid but SkipFailOnInvalidTCPEndpoint is false",
cfg: &Config{
TCPClientSettings: TCPClientSettings{
Endpoint: "http://localhost:24224",
ConnectionTimeout: time.Second * 30,
},
SkipFailOnInvalidTCPEndpoint: true,
},
err: nil,
},
{
desc: "Config is valid",
cfg: &Config{
Expand Down

0 comments on commit 9dbb6dd

Please sign in to comment.