Skip to content

Commit

Permalink
resolve issue #79
Browse files Browse the repository at this point in the history
* implement go-tarantool Logger interface to use the same logger as router uses
* AddInstance bugfix: pass r.cfg.PoolOpts to new instance
  • Loading branch information
nurzhan-saktaganov committed Sep 24, 2024
1 parent 2ee4088 commit a96a063
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## Unreleased

BUG FIXES:

* AddInstance bugfix: pass r.cfg.PoolOpts to new instance

FEATURES:

* Support StdoutLoggerf that allows control log level (resolve issue #84)
* Implement go-tarantool Logger interface to use the same logger as router uses (resolve issue #79)

## v1.0.1

Expand Down
59 changes: 59 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package vshard_router

import (
"context"

"github.com/tarantool/go-tarantool/v2"
)

// go-tarantool writes logs by default to stderr. Stderr might be not available, or user might use syslog or logging into file.
// So we should implement logging interface and redirect go-tarantool logs to the user's logger.
type tarantoolOptsLogger struct {
loggerf LogfProvider
ctx context.Context
}

// Does almost the same thing as defaultLogger in go-tarantool, but uses user provided logger instead of stdout logger.
// https://github.com/tarantool/go-tarantool/blob/592db69eed8649b82ce432b930c27daeee98c52f/connection.go#L90
func (l tarantoolOptsLogger) Report(event tarantool.ConnLogKind, conn *tarantool.Connection, v ...interface{}) {
switch event {
case tarantool.LogReconnectFailed:
reconnects, ok1 := v[0].(uint)
err, ok2 := v[1].(error)
if ok1 && ok2 {
l.loggerf.Errorf(l.ctx, "tarantool: reconnect (%d) to %s failed: %s", reconnects, conn.Addr(), err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: reconnect to %s failed (unexpected v... format): %+v", conn.Addr(), v)
}
case tarantool.LogLastReconnectFailed:
if err, ok := v[0].(error); ok {
l.loggerf.Errorf(l.ctx, "tarantool: last reconnect to %s failed: %s, giving it up", conn.Addr(), err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: last reconnect to %s failed (unexpected v... format): %v+", conn.Addr(), v)
}
case tarantool.LogUnexpectedResultId:
if header, ok := v[0].(tarantool.Header); ok {
l.loggerf.Errorf(l.ctx, "tarantool: connection %s got unexpected resultId (%d) in response"+
"(probably cancelled request)",
conn.Addr(), header.RequestId)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: connection %s got unexpected resultId in response"+
"(probably cancelled request) (unexpected v... format): %+v",
conn.Addr(), v)
}
case tarantool.LogWatchEventReadFailed:
if err, ok := v[0].(error); ok {
l.loggerf.Errorf(l.ctx, "tarantool: unable to parse watch event: %s", err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: unable to parse watch event (unexpected v... format): %+v", v)
}
case tarantool.LogAppendPushFailed:
if err, ok := v[0].(error); ok {
l.loggerf.Errorf(l.ctx, "tarantool: unable to append a push response: %s", err)
} else {
l.loggerf.Errorf(l.ctx, "tarantool: unable to append a push response (unexpected v... format): %+v", v)
}
default:
l.loggerf.Errorf(l.ctx, "tarantool: unexpected event %d on conn %s, v...: %+v", event, conn, v)
}
}
1 change: 1 addition & 0 deletions topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (r *Router) AddInstance(ctx context.Context, rsID uuid.UUID, info InstanceI
User: r.cfg.User,
Password: r.cfg.Password,
},
Opts: r.cfg.PoolOpts,
}

idToReplicasetRef := r.getIDToReplicaset()
Expand Down
6 changes: 6 additions & 0 deletions vshard.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ func prepareCfg(cfg Config) (Config, error) {
}
}

// Log tarantool internal events using the same logger as router uses.
cfg.PoolOpts.Logger = tarantoolOptsLogger{
loggerf: cfg.Loggerf,
ctx: context.Background(),
}

if cfg.Metrics == nil {
cfg.Metrics = emptyMetricsProvider
}
Expand Down

0 comments on commit a96a063

Please sign in to comment.