Skip to content

Commit 08a891a

Browse files
feat: Make logger a configurable struct member for stub
Previously, the stub package used a global logger instance, making it difficult for applications using the stub as a library to customize logging behavior. Co-authored-by: Krisztian Litkey <krisztian.litkey@intel.com> Signed-off-by: Gaurav Ghildiyal <gauravkg@google.com> Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent 9b8befa commit 08a891a

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

pkg/stub/stub.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ type Stub interface {
178178
// This is the default timeout if the plugin has not been started or
179179
// the timeout received in the Configure request otherwise.
180180
RequestTimeout() time.Duration
181+
182+
// Logger returns the logger used by the stub.
183+
Logger() nrilog.Logger
181184
}
182185

183186
const (
@@ -188,9 +191,6 @@ const (
188191
)
189192

190193
var (
191-
// Logger for messages generated internally by the stub itself.
192-
log = nrilog.Get()
193-
194194
// Used instead of a nil Context in logging.
195195
noCtx = context.TODO()
196196

@@ -268,6 +268,14 @@ func WithTTRPCOptions(clientOpts []ttrpc.ClientOpts, serverOpts []ttrpc.ServerOp
268268
}
269269
}
270270

271+
// WithLogger sets the logger to be used by the stub.
272+
func WithLogger(logger nrilog.Logger) Option {
273+
return func(s *stub) error {
274+
s.logger = logger
275+
return nil
276+
}
277+
}
278+
271279
// stub implements Stub.
272280
type stub struct {
273281
sync.Mutex
@@ -295,6 +303,7 @@ type stub struct {
295303

296304
registrationTimeout time.Duration
297305
requestTimeout time.Duration
306+
logger nrilog.Logger
298307
}
299308

300309
// Handlers for NRI plugin event and request.
@@ -329,6 +338,7 @@ func New(p interface{}, opts ...Option) (Stub, error) {
329338

330339
registrationTimeout: DefaultRegistrationTimeout,
331340
requestTimeout: DefaultRequestTimeout,
341+
logger: nrilog.Get(),
332342
}
333343

334344
for _, o := range opts {
@@ -345,7 +355,7 @@ func New(p interface{}, opts ...Option) (Stub, error) {
345355
return nil, err
346356
}
347357

348-
log.Infof(noCtx, "Created plugin %s (%s, handles %s)", stub.Name(),
358+
stub.logger.Infof(noCtx, "Created plugin %s (%s, handles %s)", stub.Name(),
349359
filepath.Base(os.Args[0]), stub.events.PrettyString())
350360

351361
return stub, nil
@@ -440,15 +450,15 @@ func (stub *stub) Start(ctx context.Context) (retErr error) {
440450
return err
441451
}
442452

443-
log.Infof(ctx, "Started plugin %s...", stub.Name())
453+
stub.logger.Infof(ctx, "Started plugin %s...", stub.Name())
444454

445455
stub.started = true
446456
return nil
447457
}
448458

449459
// Stop the plugin.
450460
func (stub *stub) Stop() {
451-
log.Infof(noCtx, "Stopping plugin %s...", stub.Name())
461+
stub.logger.Infof(noCtx, "Stopping plugin %s...", stub.Name())
452462

453463
stub.Lock()
454464
defer stub.Unlock()
@@ -504,7 +514,7 @@ func (stub *stub) Run(ctx context.Context) error {
504514

505515
err = <-stub.srvErrC
506516
if err == ttrpc.ErrServerClosed {
507-
log.Infof(noCtx, "ttrpc server closed %s : %v", stub.Name(), err)
517+
stub.logger.Infof(noCtx, "ttrpc server closed %s : %v", stub.Name(), err)
508518
}
509519

510520
return err
@@ -522,6 +532,10 @@ func (stub *stub) Name() string {
522532
return stub.idx + "-" + stub.name
523533
}
524534

535+
func (stub *stub) Logger() nrilog.Logger {
536+
return stub.logger
537+
}
538+
525539
func (stub *stub) RegistrationTimeout() time.Duration {
526540
return stub.registrationTimeout
527541
}
@@ -533,12 +547,12 @@ func (stub *stub) RequestTimeout() time.Duration {
533547
// Connect the plugin to NRI.
534548
func (stub *stub) connect() error {
535549
if stub.conn != nil {
536-
log.Infof(noCtx, "Using given plugin connection...")
550+
stub.logger.Infof(noCtx, "Using given plugin connection...")
537551
return nil
538552
}
539553

540554
if env := os.Getenv(api.PluginSocketEnvVar); env != "" {
541-
log.Infof(noCtx, "Using connection %q from environment...", env)
555+
stub.logger.Infof(noCtx, "Using connection %q from environment...", env)
542556

543557
fd, err := strconv.Atoi(env)
544558
if err != nil {
@@ -566,7 +580,7 @@ func (stub *stub) connect() error {
566580

567581
// Register the plugin with NRI.
568582
func (stub *stub) register(ctx context.Context) error {
569-
log.Infof(ctx, "Registering plugin %s...", stub.Name())
583+
stub.logger.Infof(ctx, "Registering plugin %s...", stub.Name())
570584

571585
ctx, cancel := context.WithTimeout(ctx, stub.registrationTimeout)
572586
defer cancel()
@@ -621,7 +635,7 @@ func (stub *stub) Configure(ctx context.Context, req *api.ConfigureRequest) (rpl
621635
err error
622636
)
623637

624-
log.Infof(ctx, "Configuring plugin %s for runtime %s/%s...", stub.Name(),
638+
stub.logger.Infof(ctx, "Configuring plugin %s for runtime %s/%s...", stub.Name(),
625639
req.RuntimeName, req.RuntimeVersion)
626640

627641
stub.registrationTimeout = time.Duration(req.RegistrationTimeout * int64(time.Millisecond))
@@ -636,7 +650,7 @@ func (stub *stub) Configure(ctx context.Context, req *api.ConfigureRequest) (rpl
636650
} else {
637651
events, err = handler(ctx, req.Config, req.RuntimeName, req.RuntimeVersion)
638652
if err != nil {
639-
log.Errorf(ctx, "Plugin configuration failed: %v", err)
653+
stub.logger.Errorf(ctx, "Plugin configuration failed: %v", err)
640654
return nil, err
641655
}
642656

@@ -646,13 +660,13 @@ func (stub *stub) Configure(ctx context.Context, req *api.ConfigureRequest) (rpl
646660

647661
// Only allow plugins to subscribe to events they can handle.
648662
if extra := events & ^stub.events; extra != 0 {
649-
log.Errorf(ctx, "Plugin subscribed for unhandled events %s (0x%x)",
663+
stub.logger.Errorf(ctx, "Plugin subscribed for unhandled events %s (0x%x)",
650664
extra.PrettyString(), extra)
651665
return nil, fmt.Errorf("internal error: unhandled events %s (0x%x)",
652666
extra.PrettyString(), extra)
653667
}
654668

655-
log.Infof(ctx, "Subscribing plugin %s (%s) for events %s", stub.Name(),
669+
stub.logger.Infof(ctx, "Subscribing plugin %s (%s) for events %s", stub.Name(),
656670
filepath.Base(os.Args[0]), events.PrettyString())
657671
}
658672

@@ -679,7 +693,7 @@ func (stub *stub) collectSync(req *api.SynchronizeRequest) (*api.SynchronizeResp
679693
stub.Lock()
680694
defer stub.Unlock()
681695

682-
log.Debugf(noCtx, "collecting sync req with %d pods, %d containers...",
696+
stub.logger.Debugf(noCtx, "collecting sync req with %d pods, %d containers...",
683697
len(req.Pods), len(req.Containers))
684698

685699
if stub.syncReq == nil {

0 commit comments

Comments
 (0)