You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: This is PBS-Go-specific. Proof-of-concept PR: #4085
Motivation
Be able to swap dependencies to test and customize behaviors.
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: #3748 with this PR: #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: #4062
Mechanism
Currently this is only done for logger object under logger package. logger/interface.go defines the interface. The providers (implementers of the interface) are logger/default.go and logger/alternative.go - one of them can be compile-time enabled (using build tags). The provider is instantiated in logger/logger.go. Alternative implementation of logger can be built with a tag custom_logger likego build -tags custom_logger.
The call site will simply replace glog.X call with logger.Log.X.
justadreamer
changed the title
PBS-Go: Lightweight compile-time dependency injection mechanism
PBS-Go: Use global objects through shims to be able to swap implementations
Jan 13, 2025
Note: This is PBS-Go-specific. Proof-of-concept PR: #4085
Motivation
Be able to swap dependencies to test and customize behaviors.
Rationale
The primary use case that can benefit from this is logging.
prebid-server
code makes direct calls toglog.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. slogThe example of how it can benefit testing is given as part of solving this issue: #3748 with this PR: #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: #4062
Mechanism
Currently this is only done for
logger
object underlogger
package.logger/interface.go
defines the interface. The providers (implementers of the interface) arelogger/default.go
andlogger/alternative.go
- one of them can be compile-time enabled (using build tags). The provider is instantiated inlogger/logger.go
. Alternative implementation of logger can be built with a tagcustom_logger
likego build -tags custom_logger
.The call site will simply replace
glog.X
call withlogger.Log.X
.Proof-of-concept
PR: #4085
The text was updated successfully, but these errors were encountered: