github.com/prebid/prebid-server/v2@v2.18.0/analytics/filesystem/file_module.go (about)

     1  package filesystem
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/chasex/glog"
     8  	"github.com/prebid/openrtb/v20/openrtb2"
     9  	"github.com/prebid/prebid-server/v2/analytics"
    10  	"github.com/prebid/prebid-server/v2/util/jsonutil"
    11  )
    12  
    13  type RequestType string
    14  
    15  const (
    16  	COOKIE_SYNC        RequestType = "/cookie_sync"
    17  	AUCTION            RequestType = "/openrtb2/auction"
    18  	VIDEO              RequestType = "/openrtb2/video"
    19  	SETUID             RequestType = "/set_uid"
    20  	AMP                RequestType = "/openrtb2/amp"
    21  	NOTIFICATION_EVENT RequestType = "/event"
    22  )
    23  
    24  // Module that can perform transactional logging
    25  type FileLogger struct {
    26  	Logger *glog.Logger
    27  }
    28  
    29  // Writes AuctionObject to file
    30  func (f *FileLogger) LogAuctionObject(ao *analytics.AuctionObject) {
    31  	var b bytes.Buffer
    32  	b.WriteString(jsonifyAuctionObject(ao))
    33  	f.Logger.Debug(b.String())
    34  	f.Logger.Flush()
    35  }
    36  
    37  // Writes VideoObject to file
    38  func (f *FileLogger) LogVideoObject(vo *analytics.VideoObject) {
    39  	//Code to parse the object and log in a way required
    40  	var b bytes.Buffer
    41  	b.WriteString(jsonifyVideoObject(vo))
    42  	f.Logger.Debug(b.String())
    43  	f.Logger.Flush()
    44  }
    45  
    46  // Logs SetUIDObject to file
    47  func (f *FileLogger) LogSetUIDObject(so *analytics.SetUIDObject) {
    48  	//Code to parse the object and log in a way required
    49  	var b bytes.Buffer
    50  	b.WriteString(jsonifySetUIDObject(so))
    51  	f.Logger.Debug(b.String())
    52  	f.Logger.Flush()
    53  }
    54  
    55  // Logs CookieSyncObject to file
    56  func (f *FileLogger) LogCookieSyncObject(cso *analytics.CookieSyncObject) {
    57  	//Code to parse the object and log in a way required
    58  	var b bytes.Buffer
    59  	b.WriteString(jsonifyCookieSync(cso))
    60  	f.Logger.Debug(b.String())
    61  	f.Logger.Flush()
    62  }
    63  
    64  // Logs AmpObject to file
    65  func (f *FileLogger) LogAmpObject(ao *analytics.AmpObject) {
    66  	if ao == nil {
    67  		return
    68  	}
    69  	//Code to parse the object and log in a way required
    70  	var b bytes.Buffer
    71  	b.WriteString(jsonifyAmpObject(ao))
    72  	f.Logger.Debug(b.String())
    73  	f.Logger.Flush()
    74  }
    75  
    76  // Logs NotificationEvent to file
    77  func (f *FileLogger) LogNotificationEventObject(ne *analytics.NotificationEvent) {
    78  	if ne == nil {
    79  		return
    80  	}
    81  	//Code to parse the object and log in a way required
    82  	var b bytes.Buffer
    83  	b.WriteString(jsonifyNotificationEventObject(ne))
    84  	f.Logger.Debug(b.String())
    85  	f.Logger.Flush()
    86  }
    87  
    88  // Method to initialize the analytic module
    89  func NewFileLogger(filename string) (analytics.Module, error) {
    90  	options := glog.LogOptions{
    91  		File:  filename,
    92  		Flag:  glog.LstdFlags,
    93  		Level: glog.Ldebug,
    94  		Mode:  glog.R_Day,
    95  	}
    96  	if logger, err := glog.New(options); err == nil {
    97  		return &FileLogger{
    98  			logger,
    99  		}, nil
   100  	} else {
   101  		return nil, err
   102  	}
   103  }
   104  
   105  func jsonifyAuctionObject(ao *analytics.AuctionObject) string {
   106  	var logEntry *logAuction
   107  	if ao != nil {
   108  		var request *openrtb2.BidRequest
   109  		if ao.RequestWrapper != nil {
   110  			request = ao.RequestWrapper.BidRequest
   111  		}
   112  		logEntry = &logAuction{
   113  			Status:               ao.Status,
   114  			Errors:               ao.Errors,
   115  			Request:              request,
   116  			Response:             ao.Response,
   117  			Account:              ao.Account,
   118  			StartTime:            ao.StartTime,
   119  			HookExecutionOutcome: ao.HookExecutionOutcome,
   120  		}
   121  	}
   122  
   123  	b, err := jsonutil.Marshal(&struct {
   124  		Type RequestType `json:"type"`
   125  		*logAuction
   126  	}{
   127  		Type:       AUCTION,
   128  		logAuction: logEntry,
   129  	})
   130  
   131  	if err == nil {
   132  		return string(b)
   133  	} else {
   134  		return fmt.Sprintf("Transactional Logs Error: Auction object badly formed %v", err)
   135  	}
   136  }
   137  
   138  func jsonifyVideoObject(vo *analytics.VideoObject) string {
   139  	var logEntry *logVideo
   140  	if vo != nil {
   141  		var request *openrtb2.BidRequest
   142  		if vo.RequestWrapper != nil {
   143  			request = vo.RequestWrapper.BidRequest
   144  		}
   145  		logEntry = &logVideo{
   146  			Status:        vo.Status,
   147  			Errors:        vo.Errors,
   148  			Request:       request,
   149  			Response:      vo.Response,
   150  			VideoRequest:  vo.VideoRequest,
   151  			VideoResponse: vo.VideoResponse,
   152  			StartTime:     vo.StartTime,
   153  		}
   154  	}
   155  
   156  	b, err := jsonutil.Marshal(&struct {
   157  		Type RequestType `json:"type"`
   158  		*logVideo
   159  	}{
   160  		Type:     VIDEO,
   161  		logVideo: logEntry,
   162  	})
   163  
   164  	if err == nil {
   165  		return string(b)
   166  	} else {
   167  		return fmt.Sprintf("Transactional Logs Error: Video object badly formed %v", err)
   168  	}
   169  }
   170  
   171  func jsonifyCookieSync(cso *analytics.CookieSyncObject) string {
   172  	var logEntry *logUserSync
   173  	if cso != nil {
   174  		logEntry = &logUserSync{
   175  			Status:       cso.Status,
   176  			Errors:       cso.Errors,
   177  			BidderStatus: cso.BidderStatus,
   178  		}
   179  	}
   180  
   181  	b, err := jsonutil.Marshal(&struct {
   182  		Type RequestType `json:"type"`
   183  		*logUserSync
   184  	}{
   185  		Type:        COOKIE_SYNC,
   186  		logUserSync: logEntry,
   187  	})
   188  
   189  	if err == nil {
   190  		return string(b)
   191  	} else {
   192  		return fmt.Sprintf("Transactional Logs Error: Cookie sync object badly formed %v", err)
   193  	}
   194  }
   195  
   196  func jsonifySetUIDObject(so *analytics.SetUIDObject) string {
   197  	var logEntry *logSetUID
   198  	if so != nil {
   199  		logEntry = &logSetUID{
   200  			Status:  so.Status,
   201  			Bidder:  so.Bidder,
   202  			UID:     so.UID,
   203  			Errors:  so.Errors,
   204  			Success: so.Success,
   205  		}
   206  	}
   207  
   208  	b, err := jsonutil.Marshal(&struct {
   209  		Type RequestType `json:"type"`
   210  		*logSetUID
   211  	}{
   212  		Type:      SETUID,
   213  		logSetUID: logEntry,
   214  	})
   215  
   216  	if err == nil {
   217  		return string(b)
   218  	} else {
   219  		return fmt.Sprintf("Transactional Logs Error: Set UID object badly formed %v", err)
   220  	}
   221  }
   222  
   223  func jsonifyAmpObject(ao *analytics.AmpObject) string {
   224  	var logEntry *logAMP
   225  	if ao != nil {
   226  		var request *openrtb2.BidRequest
   227  		if ao.RequestWrapper != nil {
   228  			request = ao.RequestWrapper.BidRequest
   229  		}
   230  		logEntry = &logAMP{
   231  			Status:               ao.Status,
   232  			Errors:               ao.Errors,
   233  			Request:              request,
   234  			AuctionResponse:      ao.AuctionResponse,
   235  			AmpTargetingValues:   ao.AmpTargetingValues,
   236  			Origin:               ao.Origin,
   237  			StartTime:            ao.StartTime,
   238  			HookExecutionOutcome: ao.HookExecutionOutcome,
   239  		}
   240  	}
   241  
   242  	b, err := jsonutil.Marshal(&struct {
   243  		Type RequestType `json:"type"`
   244  		*logAMP
   245  	}{
   246  		Type:   AMP,
   247  		logAMP: logEntry,
   248  	})
   249  
   250  	if err == nil {
   251  		return string(b)
   252  	} else {
   253  		return fmt.Sprintf("Transactional Logs Error: Amp object badly formed %v", err)
   254  	}
   255  }
   256  
   257  func jsonifyNotificationEventObject(ne *analytics.NotificationEvent) string {
   258  	var logEntry *logNotificationEvent
   259  	if ne != nil {
   260  		logEntry = &logNotificationEvent{
   261  			Request: ne.Request,
   262  			Account: ne.Account,
   263  		}
   264  	}
   265  
   266  	b, err := jsonutil.Marshal(&struct {
   267  		Type RequestType `json:"type"`
   268  		*logNotificationEvent
   269  	}{
   270  		Type:                 NOTIFICATION_EVENT,
   271  		logNotificationEvent: logEntry,
   272  	})
   273  
   274  	if err == nil {
   275  		return string(b)
   276  	} else {
   277  		return fmt.Sprintf("Transactional Logs Error: NotificationEvent object badly formed %v", err)
   278  	}
   279  }