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

PBS-Go: Lightweight compile-time dependency injection mechanism #1

Open
justadreamer opened this issue Dec 3, 2024 · 0 comments
Open

Comments

@justadreamer
Copy link

justadreamer commented Dec 3, 2024

Note: This is PBS-Go-specific.

Motivation

Be able to swap dependencies to be able to test and customize behavior in production.

Rationale

The primary use case that can benefit from this is logging. prebid-server code makes direct calls to glog.Warningf, glog.Errorf. This is a side-effectful behavior that depends on glog package. It would be useful to encapsulate it behind some common logging interface facade to be able to replace glog with another logger in compile time to a) be able to test b) f.e. use other logger s.a. slog

The example of how it can benefit testing is given as part of solving this issue: prebid#3748 with this PR: prebid#3829. The issue specifically complains that there is undesirable logging happening and it would be nice to remove it. To actually test that it has been removed - we need to inject a different logger during testing - this is done using an interface and instance provided in di/logger.go and tests instantiating a different instance in runtime.

Another example is that if logger is encapsulated - we will be able to easily swap the glog with slog f.e. for the use case described here: prebid#4062

Mechanism

The di/interfaces package will contain interfaces for the dependencies s.a. logger, f.e. di/interfaces/log.go. The di/providers will contain compile-time enabled (using build tags) dependency providers that are instantiated in di/di.go. It is super simple to drop in another implementation of logger and build with a tag custom_logger likego build -tags custom_logger.

The call site will simply replace glog.X call with di.Log.X.

Proof-of-concept

PR:

@justadreamer justadreamer assigned divilla and unassigned divilla Dec 3, 2024
@justadreamer justadreamer changed the title PBS-Go: Lightweight dependency injection PBS-Go: Lightweight compile-time dependency injection mechanism Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants