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

HTTP Analytics Module #3299

Closed
wants to merge 18 commits into from
Closed
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
4 changes: 4 additions & 0 deletions adapters/appnexus/appnexus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ type FakeRandomNumberGenerator struct {
func (f FakeRandomNumberGenerator) GenerateInt63() int64 {
return f.Number
}

func (f FakeRandomNumberGenerator) GenerateFloat64() float64 {
return 0
}
14 changes: 14 additions & 0 deletions analytics/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/prebid/prebid-server/v2/analytics"
"github.com/prebid/prebid-server/v2/analytics/clients"
"github.com/prebid/prebid-server/v2/analytics/filesystem"
"github.com/prebid/prebid-server/v2/analytics/http"
"github.com/prebid/prebid-server/v2/analytics/pubstack"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/privacy"
Expand Down Expand Up @@ -38,6 +39,19 @@ func New(analytics *config.Analytics) analytics.Runner {
glog.Errorf("Could not initialize PubstackModule: %v", err)
}
}

if analytics.Http.Enabled {
httpModule, err := http.NewModule(
clients.GetDefaultHttpInstance(),
analytics.Http,
clock.New())
if err == nil {
modules["http"] = httpModule
} else {
glog.Errorf("Could not initialize Http Anayltics: %v", err)
}
}
steffenmllr marked this conversation as resolved.
Show resolved Hide resolved

return modules
}

Expand Down
37 changes: 37 additions & 0 deletions analytics/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,43 @@ func TestNewPBSAnalytics_Pubstack(t *testing.T) {
assert.Equal(t, len(instanceWithError), 0)
}

func TestNewModuleHttp(t *testing.T) {
httpAnalyticsWithoutError := New(&config.Analytics{
Http: config.AnalyticsHttp{
Enabled: true,
Endpoint: config.AnalyticsHttpEndpoint{
Url: "http://localhost:8080",
Timeout: "1s",
},
Buffers: config.AnalyticsBuffer{
BufferSize: "100KB",
EventCount: 50,
Timeout: "30s",
},
Auction: config.AnalyticsFeature{
SampleRate: 1,
},
},
})
instanceWithoutError := httpAnalyticsWithoutError.(enabledAnalytics)

assert.Equal(t, len(instanceWithoutError), 1)

httpAnalyticsWithError := New(&config.Analytics{
Http: config.AnalyticsHttp{
Enabled: true,
Endpoint: config.AnalyticsHttpEndpoint{
Url: "http://localhost:8080",
},
Auction: config.AnalyticsFeature{
SampleRate: 1,
Filter: "invalid == filter",
},
},
})
instanceWithError := httpAnalyticsWithError.(enabledAnalytics)
assert.Equal(t, len(instanceWithError), 0)
}
func TestSampleModuleActivitiesAllowed(t *testing.T) {
var count int
am := initAnalytics(&count)
Expand Down
61 changes: 61 additions & 0 deletions analytics/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Http Analytics

This module sends selected analytics events to a http endpoint.

Please make sure you take a look at the possible configuration and the filter options

## Configuration

```yaml
analytics:
http:
# Required: enable the module
enabled: true
endpoint:
# Required: url where the endpoint post data to
url: "https://my-rest-endpoint.com"
# Required: timeout for the request (parsed as golang duration)
timeout: "2s"
# Optional: enables gzip compression for the payload
gzip: false
# Optional: additional headers send in every request
additional_headers:
X-My-header: "some-thing"
buffer: # Flush events when (first condition reached)
# Size of the buffer in bytes
size: "2MB" # greater than 2MB (size using SI standard eg. "44kB", "17MB")
count : 100 # greater than 100 events
timeout: "15m" # greater than 15 minutes (parsed as golang duration)
auction:
sample_rate: 1 # sample rate 0-1.0 to sample the event, 0 (default) disables the collector for those events
filter: "RequestWrapper.BidRequest.App.ID == '123'" # Optional filter
video:
sample_rate: 1 # Sample rate, f 0-1 set sample rate, 1 is 100%
filter: ""
amp:
sample_rate: 0.5 # 50% of the events are sampled
filter: ""
setuid:
sample_rate: 0.25 # 25% of the events are sampled
filter: ""
cookie_sync:
sample_rate: 0 # events are not sampled
filter: ""
notification:
sample_rate: 1
filter: ""

```

### Sample Rate

The sample rate has to be between `0.0` (never sample) and `1.0` (always sample). The sample rate is always evaluated and defaults to 0

### Filter

The module uses [github.com/antonmedv/expr](github.com/antonmedv/expr) for complexer filter options. The analytics object is always passed into the expression.

#### Samples:

- Auction: `RequestWrapper.BidRequest.App.ID == '123'`
- Video: `VideoRequest.Video.MinDuration > 200`
47 changes: 47 additions & 0 deletions analytics/http/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package http

import (
"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 filterObjectFunc[T analytics.AuctionObject | analytics.AmpObject | analytics.CookieSyncObject | analytics.NotificationEvent | analytics.SetUIDObject | analytics.VideoObject] func(event *T) bool

func createFilter[T analytics.AuctionObject | analytics.AmpObject | analytics.VideoObject | analytics.SetUIDObject | analytics.CookieSyncObject | analytics.NotificationEvent](
feature config.AnalyticsFeature,
randomGenerator randomutil.RandomGenerator,
) (filterObjectFunc[T], error) {
var filterProgram *vm.Program
var err error
if feature.Filter != "" {
var obj T
// precompile the filter expression for performance, make sure we return a boolean from the expression
filterProgram, err = expr.Compile(feature.Filter, expr.Env(obj), expr.AsBool())
if err != nil {
return nil, err
}
}

return func(event *T) bool {
// Disable tracking for nil events or events with a sample rate of 0
if event == nil || feature.SampleRate <= 0 || randomGenerator.GenerateFloat64() > feature.SampleRate {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. May I ask why do we need this random generator here? Would this part only event == nil || feature.SampleRate <= 0 satisfy the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false
}

// Use a filter if one is defined
if filterProgram != nil {
output, err := expr.Run(filterProgram, event)
if err != nil {
glog.Errorf("[HttpAnalytics] Error filter: %v", err)
return false
}
return output.(bool)
}

return true
}, nil
}
Loading
Loading