Skip to content

Commit

Permalink
Move rand.Float64 into randomutil to make it testable
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenmllr committed Nov 17, 2023
1 parent 3296c4e commit 0193a3e
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 81 deletions.
52 changes: 38 additions & 14 deletions analytics/http/filters.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package http

import (
"math/rand"

"github.com/antonmedv/expr"
"github.com/antonmedv/expr/vm"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v2/analytics"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/util/randomutil"
)

type (
Expand All @@ -18,7 +18,10 @@ type (
videoFilter func(event *analytics.VideoObject) bool
)

func createAuctionFilter(feature config.AnalyticsFeature) (auctionFilter, error) {
func createAuctionFilter(
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (auctionFilter, error) {
var filterProgram *vm.Program
var err error
if feature.Filter != "" {
Expand All @@ -31,14 +34,15 @@ func createAuctionFilter(feature config.AnalyticsFeature) (auctionFilter, error)

return func(event *analytics.AuctionObject) bool {
// Disable tracking for nil events or events with a sample rate of 0
if event == nil || feature.SampleRate <= 0 || rand.Float64() > feature.SampleRate {
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
return false
}

// Use a filter is one is defined
if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter auction: %v", err)
return false
}
return output.(bool)
Expand All @@ -48,7 +52,10 @@ func createAuctionFilter(feature config.AnalyticsFeature) (auctionFilter, error)
}, nil
}

func createAmpFilter(feature config.AnalyticsFeature) (ampFilter, error) {
func createAmpFilter(
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (ampFilter, error) {
var filterProgram *vm.Program
var err error

Expand All @@ -60,13 +67,14 @@ func createAmpFilter(feature config.AnalyticsFeature) (ampFilter, error) {
}

return func(event *analytics.AmpObject) bool {
if event == nil || feature.SampleRate <= 0 || rand.Float64() > feature.SampleRate {
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
return false
}

if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter amp: %v", err)
return false
}
return output.(bool)
Expand All @@ -76,7 +84,10 @@ func createAmpFilter(feature config.AnalyticsFeature) (ampFilter, error) {
}, nil
}

func createCookieSyncFilter(feature config.AnalyticsFeature) (cookieSyncFilter, error) {
func createCookieSyncFilter(
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (cookieSyncFilter, error) {
var filterProgram *vm.Program
var err error

Expand All @@ -88,13 +99,14 @@ func createCookieSyncFilter(feature config.AnalyticsFeature) (cookieSyncFilter,
}

return func(event *analytics.CookieSyncObject) bool {
if event == nil || feature.SampleRate <= 0 || rand.Float64() > feature.SampleRate {
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
return false
}

if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter cookie sync: %v", err)
return false
}
return output.(bool)
Expand All @@ -104,7 +116,10 @@ func createCookieSyncFilter(feature config.AnalyticsFeature) (cookieSyncFilter,
}, nil
}

func createNotificationFilter(feature config.AnalyticsFeature) (notificationFilter, error) {
func createNotificationFilter(
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (notificationFilter, error) {
var filterProgram *vm.Program
var err error

Expand All @@ -116,13 +131,14 @@ func createNotificationFilter(feature config.AnalyticsFeature) (notificationFilt
}

return func(event *analytics.NotificationEvent) bool {
if event == nil || feature.SampleRate <= 0 || rand.Float64() > feature.SampleRate {
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
return false
}

if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter notification: %v", err)
return false
}
return output.(bool)
Expand All @@ -132,7 +148,10 @@ func createNotificationFilter(feature config.AnalyticsFeature) (notificationFilt
}, nil
}

func createSetUIDFilter(feature config.AnalyticsFeature) (setUIDFilter, error) {
func createSetUIDFilter(
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (setUIDFilter, error) {
var filterProgram *vm.Program
var err error

Expand All @@ -144,13 +163,14 @@ func createSetUIDFilter(feature config.AnalyticsFeature) (setUIDFilter, error) {
}

return func(event *analytics.SetUIDObject) bool {
if event == nil || feature.SampleRate <= 0 || rand.Float64() > feature.SampleRate {
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
return false
}

if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter setUID: %v", err)
return false
}
return output.(bool)
Expand All @@ -160,7 +180,10 @@ func createSetUIDFilter(feature config.AnalyticsFeature) (setUIDFilter, error) {
}, nil
}

func createVideoFilter(feature config.AnalyticsFeature) (videoFilter, error) {
func createVideoFilter(
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (videoFilter, error) {
var filterProgram *vm.Program
var err error

Expand All @@ -172,13 +195,14 @@ func createVideoFilter(feature config.AnalyticsFeature) (videoFilter, error) {
}

return func(event *analytics.VideoObject) bool {
if event == nil || feature.SampleRate <= 0 || rand.Float64() > feature.SampleRate {
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
return false
}

if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter video: %v", err)
return false
}
return output.(bool)
Expand Down
Loading

0 comments on commit 0193a3e

Please sign in to comment.