Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/static/integrations/squid_exporter/squid_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"net/netip"
"strconv"

se "github.com/boynux/squid-exporter/collector"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
})
}
}
Expand Down
Loading