Skip to content

Commit

Permalink
client.go: allow transaction logs to use a specific logger
Browse files Browse the repository at this point in the history
Provide an option to logger to explictly direct transactions
logs. When not specified, client.logger remains as the used
logger.

Reported-at: https://issues.redhat.com/browse/SDN-3507
Signed-off-by: Flavio Fernandes <flaviof@redhat.com>
  • Loading branch information
flavio-fernandes committed Aug 7, 2023
1 parent 6785b52 commit 36b11f9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
28 changes: 24 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ type ovsdbClient struct {

trafficSeen chan struct{}

logger *logr.Logger
logger *logr.Logger
txnLogger *logr.Logger
}

// database is everything needed to map between go types and an ovsdb Database
Expand Down Expand Up @@ -181,6 +182,25 @@ func newOVSDBClient(clientDBModel model.ClientDBModel, opts ...Option) (*ovsdbCl
)
ovs.logger = &l
}

// Specific logger for transactions. Set verbosity explicitly.
if ovs.options.txnLogger == nil {
if ovs.options.logger == nil {
// create a new logger to log to stdout
l := stdr.NewWithOptions(log.New(os.Stderr, "", log.LstdFlags), stdr.Options{LogCaller: stdr.All}).WithName("libovsdb").WithValues(
"database", ovs.primaryDBName,
).V(4)
ovs.txnLogger = &l
} else {
l := ovs.options.logger.WithValues(
"database", ovs.primaryDBName,
).V(4)
ovs.txnLogger = &l
}
} else {
l := ovs.options.txnLogger.V(4)
ovs.txnLogger = &l
}
ovs.metrics.init(clientDBModel.Name(), ovs.options.metricNamespace, ovs.options.metricSubsystem)
ovs.registerMetrics()

Expand Down Expand Up @@ -822,9 +842,9 @@ func (o *ovsdbClient) transact(ctx context.Context, dbName string, skipChWrite b
if o.rpcClient == nil {
return nil, ErrNotConnected
}
dbgLogger := o.logger.WithValues("database", dbName).V(4)
if dbgLogger.Enabled() {
dbgLogger.Info("transacting operations", "operations", fmt.Sprintf("%+v", operation))
// Use dedicated logger for providing transactions.
if o.txnLogger.Enabled() {
o.txnLogger.Info(fmt.Sprintf("%+v", operation))
}
err := o.rpcClient.CallWithContext(ctx, "transact", args, &reply)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type options struct {
timeout time.Duration
backoff backoff.BackOff
logger *logr.Logger
txnLogger *logr.Logger
registry prometheus.Registerer
shouldRegisterMetrics bool // in case metrics are changed after-the-fact
metricNamespace string // prometheus metric namespace
Expand Down Expand Up @@ -137,6 +138,15 @@ func WithLogger(l *logr.Logger) Option {
}
}

// WithTransactionLogger allows setting a specific log sink for transactions.
// Otherwise, the default go log package is used.
func WithTransactionLogger(l *logr.Logger) Option {
return func(o *options) error {
o.txnLogger = l
return nil
}
}

// WithMetricsRegistry allows the user to specify a Prometheus metrics registry.
// If supplied, the metrics as defined in metrics.go will be registered.
func WithMetricsRegistry(r prometheus.Registerer) Option {
Expand Down

0 comments on commit 36b11f9

Please sign in to comment.