diff --git a/internal/static/integrations/squid_exporter/squid_exporter.go b/internal/static/integrations/squid_exporter/squid_exporter.go index 96e4153e5bf..5d36f959670 100644 --- a/internal/static/integrations/squid_exporter/squid_exporter.go +++ b/internal/static/integrations/squid_exporter/squid_exporter.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net" + "net/netip" "strconv" se "github.com/boynux/squid-exporter/collector" @@ -44,7 +45,13 @@ func (c *Config) validate() error { if host == "" { return ErrNoHostname } - c.Host = host + + if ip, err := netip.ParseAddr(host); err == nil && ip.IsValid() && ip.Is6() { + // Restore the explicit brackets for IPv6 addresses + c.Host = fmt.Sprintf("[%s]", host) + } else { + c.Host = host + } if port == "" { return ErrNoPort @@ -95,7 +102,7 @@ func New(c *Config) (integrations.Integration, error) { } seExporter := se.New(&se.CollectorConfig{ - Hostname: c.Host, + Hostname: fmt.Sprintf("[%s]", c.Host), Port: c.Port, Login: c.Username, Password: string(c.Password), diff --git a/internal/static/integrations/squid_exporter/squid_exporter_test.go b/internal/static/integrations/squid_exporter/squid_exporter_test.go index f36d14480fd..63d41ac1a5c 100644 --- a/internal/static/integrations/squid_exporter/squid_exporter_test.go +++ b/internal/static/integrations/squid_exporter/squid_exporter_test.go @@ -12,9 +12,10 @@ import ( func TestConfigValidate(t *testing.T) { cases := []struct { - name string - getConfig func() Config - expectedErr error + name string + getConfig func() Config + expectedErr error + expectedHost string }{ { name: "valid", @@ -64,17 +65,30 @@ func TestConfigValidate(t *testing.T) { }, expectedErr: errors.New("address a@#$%:asdf::12312: too many colons in address"), }, + { + name: "valid ipv6", + getConfig: func() Config { + cfg := Config{} + cfg.Address = "[::1]:51001" + return cfg + }, + expectedHost: "[::1]", + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { cfg := tc.getConfig() err := cfg.validate() - if tc.expectedErr == nil { - require.NoError(t, err) + if tc.expectedErr != nil { + require.ErrorContains(t, err, tc.expectedErr.Error()) return } - require.ErrorContains(t, err, tc.expectedErr.Error()) + + require.NoError(t, err) + if tc.expectedHost != "" { + require.Equal(t, tc.expectedHost, cfg.Host) + } }) } }