Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db.DB/redis.Client: Details in Address for GetAddr #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 20 additions & 7 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/lib/pq"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
"net"
Expand All @@ -36,7 +37,7 @@ type DB struct {

Options *Options

addr string
addrDescription string
columnMap ColumnMap
logger *logging.Logger
tableSemaphores map[string]*semaphore.Weighted
Expand Down Expand Up @@ -94,8 +95,8 @@ func (o *Options) Validate() error {

// NewDbFromConfig returns a new DB from Config.
func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks RetryConnectorCallbacks) (*DB, error) {
var addr string
var db *sqlx.DB
var typeAddr string

switch c.Type {
case "mysql":
Expand Down Expand Up @@ -144,8 +145,8 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry
return setGaleraOpts(ctx, conn, int64(c.Options.WsrepSyncWait))
}

addr = config.Addr
db = sqlx.NewDb(sql.OpenDB(NewConnector(connector, logger, connectorCallbacks)), MySQL)
typeAddr = config.Addr
case "pgsql":
uri := &url.URL{
Scheme: "postgres",
Expand Down Expand Up @@ -202,12 +203,18 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry
return nil, errors.Wrap(err, "can't open pgsql database")
}

addr = utils.JoinHostPort(c.Host, port)
db = sqlx.NewDb(sql.OpenDB(NewConnector(connector, logger, connectorCallbacks)), PostgreSQL)
typeAddr = utils.JoinHostPort(c.Host, port)
default:
return nil, unknownDbType(c.Type)
}

addrDescription := c.Type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a benefit renaming addr to addrDescription and introducing typeAddr. I would revert that and do the following here:

if c.TlsOptions.Enable {
    addr = fmt.Sprintf("%s+tls://%s@%s/%s", c.Type, c.User, addr, c.Database)
} else {
    addr = fmt.Sprintf("%s://%s@%s/%s", c.Type, c.User, addr, c.Database)
}

if c.TlsOptions.Enable {
addrDescription += "+tls"
}
addrDescription += fmt.Sprintf("://%s@%s/%s", c.User, typeAddr, c.Database)

db.SetMaxIdleConns(c.Options.MaxConnections / 3)
db.SetMaxOpenConns(c.Options.MaxConnections)

Expand All @@ -217,15 +224,21 @@ func NewDbFromConfig(c *Config, logger *logging.Logger, connectorCallbacks Retry
DB: db,
Options: &c.Options,
columnMap: NewColumnMap(db.Mapper),
addr: addr,
addrDescription: addrDescription,
logger: logger,
tableSemaphores: make(map[string]*semaphore.Weighted),
}, nil
}

// GetAddr returns the database host:port or Unix socket address.
// GetAddr returns the Redis connection address in a technical form.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

database not Redis but I would extend it anyway:

GetAddr returns the URI-like database connection string with the following syntax type[+tls]://user@host[:port]/database.

func (db *DB) GetAddr() string {
return db.addr
return db.addrDescription
}

// MarshalLogObject implements zapcore.ObjectMarshaler, adding the database address to each log message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would link to GetAttr here so that it's clear which additional information is logged.

func (db *DB) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("database_address", db.addrDescription)
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, please add a newline before return as it improves readability.

I know that there is no specific convention or rule in Go regarding the use of newlines before return statements. Even the official Go style guide does not explicitly mention anything in this regard. But we do this in PHP as well and the majority of the code in Icinga DB and Icinga Go library already does this, so I would make this a convention for us.

}

// BuildColumns returns all columns of the given struct.
Expand Down
103 changes: 103 additions & 0 deletions database/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package database

import (
"github.com/icinga/icinga-go-library/config"
"github.com/icinga/icinga-go-library/logging"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"testing"
)

func TestNewDbFromConfig_GetAddr(t *testing.T) {
tests := []struct {
name string
conf *Config
addr string
}{
{
name: "mysql-simple",
conf: &Config{
Type: "mysql",
Host: "example.com",
Database: "db",
User: "user",
},
addr: "mysql://[email protected]:3306/db",
},
{
name: "mysql-custom-port",
conf: &Config{
Type: "mysql",
Host: "example.com",
Port: 1234,
Database: "db",
User: "user",
},
addr: "mysql://[email protected]:1234/db",
},
{
name: "mysql-tls",
conf: &Config{
Type: "mysql",
Host: "example.com",
Database: "db",
User: "user",
TlsOptions: config.TLS{Enable: true},
},
addr: "mysql+tls://[email protected]:3306/db",
},
{
name: "mysql-unix-domain-socket",
conf: &Config{
Type: "mysql",
Host: "/var/empty/mysql.sock",
Database: "db",
User: "user",
},
addr: "mysql://user@/var/empty/mysql.sock/db",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This demonstrates one limitation of the approach: / can appear both in the URL syntax and in a path. The somewhat proper but ugly solution to this would be to just use net/url where this would result in mysql://user@%2Fvar%2Fempty%2Fmysql.sock/db.

A nicer looking alternative might be to use %q instead of %s for unix domain sockets, but that raises the question why make it look like an URL if it actually isn't one.

And a somewhat strange-looking alternative might be to take some inspiration from lib/pq:

// Host and port can alternatively be specified in the query string. lib/pq can't parse the connection URI
// if a Unix domain socket path is specified in the host part of the URI, therefore always use the query
// string. See also https://github.com/lib/pq/issues/796
"host": {c.Host},

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that raises the question why make it look like an URL if it actually isn't one.

This was inspired by the comment above, #53 (review).

I don't know what exactly would be seen as a beautiful solution. Maybe make it more strange by prefixing a filesystem path with file://. I just checked against the MySQL documentation and found other beauties like root@localhost?socket=%2Ftmp%2Fmysql.sock or root@localhost?socket=(/tmp/mysql.sock).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that raises the question why make it look like an URL if it actually isn't one.

This was inspired by the comment above, #53 (review).

With my comment I didn't mean to imply that it is absolutely necessary to create URI-like strings, but
since both MySQL and PostgreSQL use URI-like connection strings, I think it's a valid approach to create a relaxed version for logging. For sockets, I would use (/path/to/socket.sock) or just leave it as it is.

},
{
name: "pgsql-simple",
conf: &Config{
Type: "pgsql",
Host: "example.com",
Database: "db",
User: "user",
},
addr: "pgsql://[email protected]:5432/db",
},
{
name: "pgsql-custom-port",
conf: &Config{
Type: "pgsql",
Host: "example.com",
Port: 1234,
Database: "db",
User: "user",
},
addr: "pgsql://[email protected]:1234/db",
},
{
name: "pgsql-tls",
conf: &Config{
Type: "pgsql",
Host: "example.com",
Database: "db",
User: "user",
TlsOptions: config.TLS{Enable: true},
},
addr: "pgsql+tls://[email protected]:5432/db",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
db, err := NewDbFromConfig(
test.conf,
logging.NewLogger(zaptest.NewLogger(t).Sugar(), 0),
RetryConnectorCallbacks{})
require.NoError(t, err)
require.Equal(t, test.addr, db.GetAddr())
})
}
}
23 changes: 21 additions & 2 deletions redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
"net"
Expand Down Expand Up @@ -79,9 +80,27 @@ func NewClientFromConfig(c *Config, logger *logging.Logger) (*Client, error) {
return NewClient(redis.NewClient(options), logger, &c.Options), nil
}

// GetAddr returns the Redis host:port or Unix socket address.
// GetAddr returns the Redis connection address in a technical form.
func (c *Client) GetAddr() string {
return c.Client.Options().Addr
description := "redis"
if c.Client.Options().TLSConfig != nil {
description += "+tls"
}
description += "://"
if username := c.Client.Options().Username; username != "" {
description += username + "@"
}
description += c.Client.Options().Addr
if db := c.Client.Options().DB; db != 0 {
description += fmt.Sprintf("/%d", db)
}
return description
}

// MarshalLogObject implements zapcore.ObjectMarshaler, adding the redis connection string to each log message.
func (c *Client) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("redis_address", c.GetAddr())
return nil
}

// HPair defines Redis hashes field-value pairs.
Expand Down
87 changes: 87 additions & 0 deletions redis/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package redis

import (
"github.com/icinga/icinga-go-library/config"
"github.com/icinga/icinga-go-library/logging"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"testing"
)

func TestNewClientFromConfig_GetAddr(t *testing.T) {
tests := []struct {
name string
conf *Config
addr string
}{
{
name: "redis-simple",
conf: &Config{
Host: "example.com",
},
addr: "redis://example.com:6379",
},
{
name: "redis-custom-port",
conf: &Config{
Host: "example.com",
Port: 6380,
},
addr: "redis://example.com:6380",
},
{
name: "redis-acl",
conf: &Config{
Host: "example.com",
Username: "user",
Password: "pass",
},
addr: "redis://[email protected]:6379",
},
{
name: "redis-custom-database",
conf: &Config{
Host: "example.com",
Database: 23,
},
addr: "redis://example.com:6379/23",
},
{
name: "redis-tls",
conf: &Config{
Host: "example.com",
TlsOptions: config.TLS{Enable: true},
},
addr: "redis+tls://example.com:6379",
},
{
name: "redis-with-everything",
conf: &Config{
Host: "example.com",
Port: 6380,
Username: "user",
Password: "pass",
Database: 23,
TlsOptions: config.TLS{Enable: true},
},
addr: "redis+tls://[email protected]:6380/23",
},
{
name: "redis-unix-domain-socket",
conf: &Config{
Host: "/var/empty/redis.sock",
},
addr: "redis:///var/empty/redis.sock",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
redis, err := NewClientFromConfig(
test.conf,
logging.NewLogger(zaptest.NewLogger(t).Sugar(), 0))
require.NoError(t, err)
require.Equal(t, test.addr, redis.GetAddr())
})
}
}