Skip to content

Commit 3bd18a8

Browse files
committed
fix: add flag to skip fail if the endpoint is not available
Signed-off-by: Bence Csati <bence.csati@axoflow.com>
1 parent df7ad28 commit 3bd18a8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

config.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type TCPClientSettings struct {
3333
type Config struct {
3434
TCPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
3535

36+
// SkipFailOnInvalidTCPEndpoint controls whether to fail if the endpoint is invalid.
37+
// This is useful for cases where the collector is started before the endpoint becomes available.
38+
SkipFailOnInvalidTCPEndpoint bool `mapstructure:"skip_fail_on_invalid_tcp_endpoint"`
39+
3640
// RequireAck enables the acknowledgement feature.
3741
RequireAck bool `mapstructure:"require_ack"`
3842

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

8892
// Resolve TCP address just to ensure that it is a valid one. It is better
8993
// to fail here than at when the exporter is started.
90-
if _, err := net.ResolveTCPAddr("tcp", config.Endpoint); err != nil {
91-
return fmt.Errorf("exporter has an invalid TCP endpoint: %w", err)
94+
if !config.SkipFailOnInvalidTCPEndpoint {
95+
if _, err := net.ResolveTCPAddr("tcp", config.Endpoint); err != nil {
96+
return fmt.Errorf("exporter has an invalid TCP endpoint: %w", err)
97+
}
9298
}
99+
93100
return nil
94101
}

config_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ func TestConfigValidate(t *testing.T) {
110110
},
111111
err: fmt.Errorf("exporter has an invalid TCP endpoint: address http://localhost:24224: too many colons in address"),
112112
},
113+
{
114+
desc: "Endpoint is invalid but SkipFailOnInvalidTCPEndpoint is false",
115+
cfg: &Config{
116+
TCPClientSettings: TCPClientSettings{
117+
Endpoint: "http://localhost:24224",
118+
ConnectionTimeout: time.Second * 30,
119+
},
120+
SkipFailOnInvalidTCPEndpoint: true,
121+
},
122+
err: nil,
123+
},
113124
{
114125
desc: "Config is valid",
115126
cfg: &Config{

0 commit comments

Comments
 (0)