From 9dbb6dd8790cca18a86e9031e4d576ba7ab1da39 Mon Sep 17 00:00:00 2001 From: Bence Csati Date: Wed, 4 Dec 2024 12:41:36 +0100 Subject: [PATCH] fix: add flag to skip fail if the endpoint is not available Signed-off-by: Bence Csati --- config.go | 11 +++++++++-- config_test.go | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 723854b..6298a0f 100644 --- a/config.go +++ b/config.go @@ -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"` @@ -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 } diff --git a/config_test.go b/config_test.go index 8b47641..91360c8 100644 --- a/config_test.go +++ b/config_test.go @@ -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{