Skip to content

Commit ab14413

Browse files
committed
Log UNIX socket address w/o port number
E.g. not Connecting to database at '/var/lib/mysql/mysql.sock:0' Connecting to Redis at '/run/icingadb-redis/icingadb-redis-server.sock:6380'
1 parent 833c935 commit ab14413

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

cmd/icingadb/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/pkg/errors"
1919
"go.uber.org/zap"
2020
"golang.org/x/sync/errgroup"
21-
"net"
2221
"os"
2322
"os/signal"
2423
"sync"
@@ -64,7 +63,7 @@ func run() int {
6463
}
6564
defer db.Close()
6665
{
67-
logger.Infof("Connecting to database at '%s'", net.JoinHostPort(cmd.Config.Database.Host, fmt.Sprint(cmd.Config.Database.Port)))
66+
logger.Infof("Connecting to database at '%s'", utils.JoinHostPort(cmd.Config.Database.Host, cmd.Config.Database.Port))
6867
err := db.Ping()
6968
if err != nil {
7069
logger.Fatalf("%+v", errors.Wrap(err, "can't connect to database"))
@@ -80,7 +79,7 @@ func run() int {
8079
logger.Fatalf("%+v", errors.Wrap(err, "can't create Redis client from config"))
8180
}
8281
{
83-
logger.Infof("Connecting to Redis at '%s'", net.JoinHostPort(cmd.Config.Redis.Host, fmt.Sprint(cmd.Config.Redis.Port)))
82+
logger.Infof("Connecting to Redis at '%s'", utils.JoinHostPort(cmd.Config.Redis.Host, cmd.Config.Redis.Port))
8483
_, err := rc.Ping(context.Background()).Result()
8584
if err != nil {
8685
logger.Fatalf("%+v", errors.Wrap(err, "can't connect to Redis"))

pkg/utils/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/pkg/errors"
1111
"golang.org/x/exp/utf8string"
1212
"math"
13+
"net"
1314
"os"
1415
"path/filepath"
1516
"strings"
@@ -206,3 +207,12 @@ func MaxInt(x, y int) int {
206207

207208
return y
208209
}
210+
211+
// JoinHostPort is like its equivalent in net., but handles UNIX sockets as well.
212+
func JoinHostPort(host string, port int) string {
213+
if strings.HasPrefix(host, "/") {
214+
return host
215+
}
216+
217+
return net.JoinHostPort(host, fmt.Sprint(port))
218+
}

0 commit comments

Comments
 (0)