-
Notifications
You must be signed in to change notification settings - Fork 718
/
pubstack_module.go
253 lines (207 loc) · 5.82 KB
/
pubstack_module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package pubstack
import (
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/benbjohnson/clock"
"github.com/golang/glog"
"github.com/prebid/prebid-server/analytics"
"github.com/prebid/prebid-server/analytics/pubstack/eventchannel"
"github.com/prebid/prebid-server/analytics/pubstack/helpers"
)
type Configuration struct {
ScopeID string `json:"scopeId"`
Endpoint string `json:"endpoint"`
Features map[string]bool `json:"features"`
}
// routes for events
const (
auction = "auction"
cookieSync = "cookiesync"
amp = "amp"
setUID = "setuid"
video = "video"
)
type bufferConfig struct {
timeout time.Duration
count int64
size int64
}
type PubstackModule struct {
eventChannels map[string]*eventchannel.EventChannel
httpClient *http.Client
sigTermCh chan os.Signal
stopCh chan struct{}
scope string
cfg *Configuration
buffsCfg *bufferConfig
muxConfig sync.RWMutex
clock clock.Clock
}
func NewModule(client *http.Client, scope, endpoint, configRefreshDelay string, maxEventCount int, maxByteSize, maxTime string, clock clock.Clock) (analytics.PBSAnalyticsModule, error) {
configUpdateTask, err := NewConfigUpdateHttpTask(
client,
scope,
endpoint,
configRefreshDelay)
if err != nil {
return nil, err
}
return NewModuleWithConfigTask(client, scope, endpoint, maxEventCount, maxByteSize, maxTime, configUpdateTask, clock)
}
func NewModuleWithConfigTask(client *http.Client, scope, endpoint string, maxEventCount int, maxByteSize, maxTime string, configTask ConfigUpdateTask, clock clock.Clock) (analytics.PBSAnalyticsModule, error) {
glog.Infof("[pubstack] Initializing module scope=%s endpoint=%s\n", scope, endpoint)
// parse args
bufferCfg, err := newBufferConfig(maxEventCount, maxByteSize, maxTime)
if err != nil {
return nil, fmt.Errorf("fail to parse the module args, arg=analytics.pubstack.buffers, :%v", err)
}
defaultFeatures := map[string]bool{
auction: false,
video: false,
amp: false,
cookieSync: false,
setUID: false,
}
defaultConfig := &Configuration{
ScopeID: scope,
Endpoint: endpoint,
Features: defaultFeatures,
}
pb := PubstackModule{
scope: scope,
httpClient: client,
cfg: defaultConfig,
buffsCfg: bufferCfg,
sigTermCh: make(chan os.Signal),
stopCh: make(chan struct{}),
eventChannels: make(map[string]*eventchannel.EventChannel),
muxConfig: sync.RWMutex{},
clock: clock,
}
signal.Notify(pb.sigTermCh, os.Interrupt, syscall.SIGTERM)
configChannel := configTask.Start(pb.stopCh)
go pb.start(configChannel)
glog.Info("[pubstack] Pubstack analytics configured and ready")
return &pb, nil
}
func (p *PubstackModule) LogAuctionObject(ao *analytics.AuctionObject) {
p.muxConfig.RLock()
defer p.muxConfig.RUnlock()
if !p.isFeatureEnable(auction) {
return
}
// serialize event
payload, err := helpers.JsonifyAuctionObject(ao, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize auction")
return
}
p.eventChannels[auction].Push(payload)
}
func (p *PubstackModule) LogNotificationEventObject(ne *analytics.NotificationEvent) {
}
func (p *PubstackModule) LogVideoObject(vo *analytics.VideoObject) {
p.muxConfig.RLock()
defer p.muxConfig.RUnlock()
if !p.isFeatureEnable(video) {
return
}
// serialize event
payload, err := helpers.JsonifyVideoObject(vo, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
return
}
p.eventChannels[video].Push(payload)
}
func (p *PubstackModule) LogSetUIDObject(so *analytics.SetUIDObject) {
p.muxConfig.RLock()
defer p.muxConfig.RUnlock()
if !p.isFeatureEnable(setUID) {
return
}
// serialize event
payload, err := helpers.JsonifySetUIDObject(so, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
return
}
p.eventChannels[setUID].Push(payload)
}
func (p *PubstackModule) LogCookieSyncObject(cso *analytics.CookieSyncObject) {
p.muxConfig.RLock()
defer p.muxConfig.RUnlock()
if !p.isFeatureEnable(cookieSync) {
return
}
// serialize event
payload, err := helpers.JsonifyCookieSync(cso, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
return
}
p.eventChannels[cookieSync].Push(payload)
}
func (p *PubstackModule) LogAmpObject(ao *analytics.AmpObject) {
p.muxConfig.RLock()
defer p.muxConfig.RUnlock()
if !p.isFeatureEnable(amp) {
return
}
// serialize event
payload, err := helpers.JsonifyAmpObject(ao, p.scope)
if err != nil {
glog.Warning("[pubstack] Cannot serialize video")
return
}
p.eventChannels[amp].Push(payload)
}
func (p *PubstackModule) start(c <-chan *Configuration) {
for {
select {
case <-p.sigTermCh:
close(p.stopCh)
cfg := p.cfg.clone().disableAllFeatures()
p.updateConfig(cfg)
return
case config := <-c:
p.updateConfig(config)
glog.Infof("[pubstack] Updating config: %v", p.cfg)
}
}
}
func (p *PubstackModule) updateConfig(config *Configuration) {
p.muxConfig.Lock()
defer p.muxConfig.Unlock()
if p.cfg.isSameAs(config) {
return
}
p.cfg = config
p.closeAllEventChannels()
p.registerChannel(amp)
p.registerChannel(auction)
p.registerChannel(cookieSync)
p.registerChannel(video)
p.registerChannel(setUID)
}
func (p *PubstackModule) isFeatureEnable(feature string) bool {
val, ok := p.cfg.Features[feature]
return ok && val
}
func (p *PubstackModule) registerChannel(feature string) {
if p.isFeatureEnable(feature) {
sender := eventchannel.BuildEndpointSender(p.httpClient, p.cfg.Endpoint, feature)
p.eventChannels[feature] = eventchannel.NewEventChannel(sender, p.clock, p.buffsCfg.size, p.buffsCfg.count, p.buffsCfg.timeout)
}
}
func (p *PubstackModule) closeAllEventChannels() {
for key, ch := range p.eventChannels {
ch.Close()
delete(p.eventChannels, key)
}
}