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

PoC: light-weight compile time dependency injection for logger #4085

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .semgrep/adapter/package-import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
// ruleid: package-import-check
"github.com/mitchellh/copystructure"
// ruleid: package-import-check
"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/di"
)

import (
Expand All @@ -17,7 +17,7 @@ import (
// ok: package-import-check
"os"
// ruleid: package-import-check
log "github.com/golang/glog"
log "github.com/prebid/prebid-server/v3/di"
)

import (
Expand All @@ -32,13 +32,13 @@ import (
)

// ruleid: package-import-check
import "github.com/golang/glog"
import "github.com/prebid/prebid-server/v3/di"

// ruleid: package-import-check
import "github.com/mitchellh/copystructure"

// ruleid: package-import-check
import log "github.com/golang/glog"
import log "github.com/prebid/prebid-server/v3/di"

// ruleid: package-import-check
import copy "github.com/mitchellh/copystructure"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ An option for developing Prebid Server in a reproducible environment isolated fr

Prebid Server is not currently intended to be imported by other projects. Go Modules is used to manage dependencies, which also makes it possible to import Prebid Server packages. This is not supported. We offer no guarantees regarding the stability of packages and do not adhere to semantic versioning guidelines.

## Swapping Global Dependencies

Logger is a global side-effectful dependency that sometimes needs to be swapped in order to modify the behavior. This can be done compile-time in the `di` package. It contains the `interface` sub-package with a ILogger interface definition and `provider` with the implementation of the default logger (`providers/log/default.go`) and an example of an alternative (`providers/log/alternative.go`) logger. Notice that the alternative logger is compiled only if a `custom_logger` build tag is specified while default logger will not be compiled in that case. You can replace the `alternative.go` with your logger implementation, but please adhere to the interface and expose a `ProvideLogger()` function that is called by `di/di.go` to set your dependency to be used globally. This is not a dependency injection per se, but this mechanism can be extended to other similar side-effectful global dependencies besides logger.

## Contributing
> [!IMPORTANT]
> All contributions must follow the [Prebid Code of Conduct](https://prebid.org/code-of-conduct/) and the [Prebid Module Rules](https://docs.prebid.org/dev-docs/module-rules.html).
Expand Down
14 changes: 7 additions & 7 deletions analytics/agma/agma_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

"github.com/benbjohnson/clock"
"github.com/docker/go-units"
"github.com/golang/glog"
"github.com/prebid/go-gdpr/vendorconsent"
"github.com/prebid/prebid-server/v3/analytics"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/di"
"github.com/prebid/prebid-server/v3/openrtb_ext"
)

Expand Down Expand Up @@ -93,7 +93,7 @@ func (l *AgmaLogger) start() {
for {
select {
case <-l.sigTermCh:
glog.Infof("[AgmaAnalytics] Received Close, trying to flush buffer")
di.Log.Infof("[AgmaAnalytics] Received Close, trying to flush buffer")
l.flush()
return
case event := <-l.bufferCh:
Expand Down Expand Up @@ -139,7 +139,7 @@ func (l *AgmaLogger) flush() {
if err != nil {
l.reset()
l.mux.Unlock()
glog.Warning("[AgmaAnalytics] fail to copy the buffer")
di.Log.Warning("[AgmaAnalytics] fail to copy the buffer")
return
}

Expand Down Expand Up @@ -223,7 +223,7 @@ func (l *AgmaLogger) LogAuctionObject(event *analytics.AuctionObject) {
}
data, err := serializeAnayltics(event.RequestWrapper, EventTypeAuction, code, event.StartTime)
if err != nil {
glog.Errorf("[AgmaAnalytics] Error serializing auction object: %v", err)
di.Log.Errorf("[AgmaAnalytics] Error serializing auction object: %v", err)
return
}
l.bufferCh <- data
Expand All @@ -239,7 +239,7 @@ func (l *AgmaLogger) LogAmpObject(event *analytics.AmpObject) {
}
data, err := serializeAnayltics(event.RequestWrapper, EventTypeAmp, code, event.StartTime)
if err != nil {
glog.Errorf("[AgmaAnalytics] Error serializing amp object: %v", err)
di.Log.Errorf("[AgmaAnalytics] Error serializing amp object: %v", err)
return
}
l.bufferCh <- data
Expand All @@ -255,14 +255,14 @@ func (l *AgmaLogger) LogVideoObject(event *analytics.VideoObject) {
}
data, err := serializeAnayltics(event.RequestWrapper, EventTypeVideo, code, event.StartTime)
if err != nil {
glog.Errorf("[AgmaAnalytics] Error serializing video object: %v", err)
di.Log.Errorf("[AgmaAnalytics] Error serializing video object: %v", err)
return
}
l.bufferCh <- data
}

func (l *AgmaLogger) Shutdown() {
glog.Info("[AgmaAnalytics] Shutdown, trying to flush buffer")
di.Log.Info("[AgmaAnalytics] Shutdown, trying to flush buffer")
l.flush() // mutex safe
}

Expand Down
10 changes: 5 additions & 5 deletions analytics/agma/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"net/url"
"time"

"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/di"
"github.com/prebid/prebid-server/v3/version"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func createHttpSender(httpClient *http.Client, endpoint config.AgmaAnalyticsHttp
if endpoint.Gzip {
requestBody, err = compressToGZIP(payload)
if err != nil {
glog.Errorf("[agmaAnalytics] Compressing request failed %v", err)
di.Log.Errorf("[agmaAnalytics] Compressing request failed %v", err)
return err
}
} else {
Expand All @@ -59,7 +59,7 @@ func createHttpSender(httpClient *http.Client, endpoint config.AgmaAnalyticsHttp

req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Url, bytes.NewBuffer(requestBody))
if err != nil {
glog.Errorf("[agmaAnalytics] Creating request failed %v", err)
di.Log.Errorf("[agmaAnalytics] Creating request failed %v", err)
return err
}

Expand All @@ -71,12 +71,12 @@ func createHttpSender(httpClient *http.Client, endpoint config.AgmaAnalyticsHttp

resp, err := httpClient.Do(req)
if err != nil {
glog.Errorf("[agmaAnalytics] Sending request failed %v", err)
di.Log.Errorf("[agmaAnalytics] Sending request failed %v", err)
return err
}

if resp.StatusCode != http.StatusOK {
glog.Errorf("[agmaAnalytics] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
di.Log.Errorf("[agmaAnalytics] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
return fmt.Errorf("wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions analytics/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"encoding/json"

"github.com/benbjohnson/clock"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/analytics"
"github.com/prebid/prebid-server/v3/analytics/agma"
"github.com/prebid/prebid-server/v3/analytics/clients"
"github.com/prebid/prebid-server/v3/analytics/filesystem"
"github.com/prebid/prebid-server/v3/analytics/pubstack"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/di"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/ortb"
"github.com/prebid/prebid-server/v3/privacy"
Expand All @@ -23,7 +23,7 @@ func New(analytics *config.Analytics) analytics.Runner {
if mod, err := filesystem.NewFileLogger(analytics.File.Filename); err == nil {
modules["filelogger"] = mod
} else {
glog.Fatalf("Could not initialize FileLogger for file %v :%v", analytics.File.Filename, err)
di.Log.Fatalf("Could not initialize FileLogger for file %v :%v", analytics.File.Filename, err)
}
}

Expand All @@ -40,7 +40,7 @@ func New(analytics *config.Analytics) analytics.Runner {
if err == nil {
modules["pubstack"] = pubstackModule
} else {
glog.Errorf("Could not initialize PubstackModule: %v", err)
di.Log.Errorf("Could not initialize PubstackModule: %v", err)
}
}

Expand All @@ -52,7 +52,7 @@ func New(analytics *config.Analytics) analytics.Runner {
if err == nil {
modules["agma"] = agmaModule
} else {
glog.Errorf("Could not initialize Agma Anayltics: %v", err)
di.Log.Errorf("Could not initialize Agma Anayltics: %v", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions analytics/filesystem/file_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package filesystem
import (
"bytes"
"fmt"
"github.com/prebid/prebid-server/v3/di"

cglog "github.com/chasex/glog"
"github.com/golang/glog"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v3/analytics"
"github.com/prebid/prebid-server/v3/util/jsonutil"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (f *FileLogger) LogNotificationEventObject(ne *analytics.NotificationEvent)
// Shutdown the logger
func (f *FileLogger) Shutdown() {
// clear all pending buffered data in case there is any
glog.Info("[FileLogger] Shutdown, trying to flush buffer")
di.Log.Info("[FileLogger] Shutdown, trying to flush buffer")
f.Logger.Flush()
}

Expand Down
8 changes: 4 additions & 4 deletions analytics/pubstack/eventchannel/eventchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/benbjohnson/clock"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/di"
)

type Metrics struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c *EventChannel) buffer(event []byte) {

_, err := c.gz.Write(event)
if err != nil {
glog.Warning("[pubstack] fail to compress, skip the event")
di.Log.Warning("[pubstack] fail to compress, skip the event")
return
}

Expand Down Expand Up @@ -104,15 +104,15 @@ func (c *EventChannel) flush() {
// finish writing gzip header
err := c.gz.Close()
if err != nil {
glog.Warning("[pubstack] fail to close gzipped buffer")
di.Log.Warning("[pubstack] fail to close gzipped buffer")
return
}

// copy the current buffer to send the payload in a new thread
payload := make([]byte, c.buff.Len())
_, err = c.buff.Read(payload)
if err != nil {
glog.Warning("[pubstack] fail to copy the buffer")
di.Log.Warning("[pubstack] fail to copy the buffer")
return
}

Expand Down
8 changes: 4 additions & 4 deletions analytics/pubstack/eventchannel/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/url"
"path"

"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/di"
)

type Sender = func(payload []byte) error
Expand All @@ -16,7 +16,7 @@ func NewHttpSender(client *http.Client, endpoint string) Sender {
return func(payload []byte) error {
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(payload))
if err != nil {
glog.Error(err)
di.Log.Error(err)
return err
}

Expand All @@ -30,7 +30,7 @@ func NewHttpSender(client *http.Client, endpoint string) Sender {
resp.Body.Close()

if resp.StatusCode != http.StatusOK {
glog.Errorf("[pubstack] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
di.Log.Errorf("[pubstack] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
return fmt.Errorf("wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
}
return nil
Expand All @@ -40,7 +40,7 @@ func NewHttpSender(client *http.Client, endpoint string) Sender {
func BuildEndpointSender(client *http.Client, baseUrl string, module string) Sender {
endpoint, err := url.Parse(baseUrl)
if err != nil {
glog.Error(err)
di.Log.Error(err)
}
endpoint.Path = path.Join(endpoint.Path, "intake", module)
return NewHttpSender(client, endpoint.String())
Expand Down
20 changes: 10 additions & 10 deletions analytics/pubstack/pubstack_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/benbjohnson/clock"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v3/di"

"github.com/prebid/prebid-server/v3/analytics"
"github.com/prebid/prebid-server/v3/analytics/pubstack/eventchannel"
Expand Down Expand Up @@ -64,7 +64,7 @@ func NewModule(client *http.Client, scope, endpoint, configRefreshDelay string,
}

func NewModuleWithConfigTask(client *http.Client, scope, endpoint string, maxEventCount int, maxByteSize, maxTime string, configTask ConfigUpdateTask, clock clock.Clock) (analytics.Module, error) {
glog.Infof("[pubstack] Initializing module scope=%s endpoint=%s\n", scope, endpoint)
di.Log.Infof("[pubstack] Initializing module scope=%s endpoint=%s\n", scope, endpoint)

// parse args
bufferCfg, err := newBufferConfig(maxEventCount, maxByteSize, maxTime)
Expand Down Expand Up @@ -103,7 +103,7 @@ func NewModuleWithConfigTask(client *http.Client, scope, endpoint string, maxEve
configChannel := configTask.Start(pb.stopCh)
go pb.start(configChannel)

glog.Info("[pubstack] Pubstack analytics configured and ready")
di.Log.Info("[pubstack] Pubstack analytics configured and ready")
return &pb, nil
}

Expand All @@ -118,7 +118,7 @@ func (p *PubstackModule) LogAuctionObject(ao *analytics.AuctionObject) {
// serialize event
payload, err := helpers.JsonifyAuctionObject(ao, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize auction")
di.Log.Warning("[pubstack] Cannot serialize auction")
return
}

Expand All @@ -139,7 +139,7 @@ func (p *PubstackModule) LogVideoObject(vo *analytics.VideoObject) {
// serialize event
payload, err := helpers.JsonifyVideoObject(vo, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
di.Log.Warning("[pubstack] Cannot serialize video")
return
}

Expand All @@ -157,7 +157,7 @@ func (p *PubstackModule) LogSetUIDObject(so *analytics.SetUIDObject) {
// serialize event
payload, err := helpers.JsonifySetUIDObject(so, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
di.Log.Warning("[pubstack] Cannot serialize video")
return
}

Expand All @@ -175,7 +175,7 @@ func (p *PubstackModule) LogCookieSyncObject(cso *analytics.CookieSyncObject) {
// serialize event
payload, err := helpers.JsonifyCookieSync(cso, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
di.Log.Warning("[pubstack] Cannot serialize video")
return
}

Expand All @@ -193,7 +193,7 @@ func (p *PubstackModule) LogAmpObject(ao *analytics.AmpObject) {
// serialize event
payload, err := helpers.JsonifyAmpObject(ao, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
di.Log.Warning("[pubstack] Cannot serialize video")
return
}

Expand All @@ -203,7 +203,7 @@ func (p *PubstackModule) LogAmpObject(ao *analytics.AmpObject) {
// Shutdown - no op since the analytic module already implements system signal handling
// and trying to close a closed channel will cause panic
func (p *PubstackModule) Shutdown() {
glog.Info("[PubstackModule] Shutdown")
di.Log.Info("[PubstackModule] Shutdown")
}

func (p *PubstackModule) start(c <-chan *Configuration) {
Expand All @@ -216,7 +216,7 @@ func (p *PubstackModule) start(c <-chan *Configuration) {
return
case config := <-c:
p.updateConfig(config)
glog.Infof("[pubstack] Updating config: %v", p.cfg)
di.Log.Infof("[pubstack] Updating config: %v", p.cfg)
}
}
}
Expand Down
Loading
Loading