-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathrecords_reporter_test.go
More file actions
354 lines (321 loc) · 13.6 KB
/
Copy pathrecords_reporter_test.go
File metadata and controls
354 lines (321 loc) · 13.6 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package activationrecords
import (
"fmt"
"testing"
"github.com/google/uuid"
"github.com/segmentio/go-hll"
"github.com/stretchr/testify/require"
"github.com/rudderlabs/rudder-go-kit/config"
"github.com/rudderlabs/rudder-go-kit/logger"
"github.com/rudderlabs/rudder-go-kit/stats"
"github.com/rudderlabs/rudder-go-kit/stats/memstats"
"github.com/rudderlabs/rudder-server/jobsdb"
)
func TestUniqueActivationRecordsReporter(t *testing.T) {
// prepareJob builds a job with the standard activation payload shape. The Parameters
// intentionally omit source_category: on the internal-batch ingestion path it is empty,
// and the reporter classifies reverse-ETL sources from the config map (metadataByID
// below), not the param.
prepareJob := func(sourceID, destinationID, fingerprint, origin, workspaceID string) *jobsdb.JobT {
return &jobsdb.JobT{
Parameters: fmt.Appendf(nil, `{"source_id":%q,"destination_id":%q}`, sourceID, destinationID),
EventPayload: fmt.Appendf(nil, `{"batch":[{"context":{"activation":{"fingerprint":%q,"origin":%q}}}]}`, fingerprint, origin),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: workspaceID,
}
}
// prepareJobNoDstID builds a job whose Parameters JSON omits the destination_id key entirely.
prepareJobNoDstID := func(sourceID, fingerprint, origin, workspaceID string) *jobsdb.JobT {
return &jobsdb.JobT{
Parameters: fmt.Appendf(nil, `{"source_id":%q}`, sourceID),
EventPayload: fmt.Appendf(nil, `{"batch":[{"context":{"activation":{"fingerprint":%q,"origin":%q}}}]}`, fingerprint, origin),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: workspaceID,
}
}
// metadataByID maps source_id -> SourceDefinition metadata, mirroring the map the
// processor passes from the backend config. "src1" is a reverse-ETL SQL warehouse
// source; "src-eventstream" is not. Unknown source_ids resolve to zero-value metadata
// and are treated as non-rETL.
metadataByID := map[string]SourceMetadata{
"src1": {Category: "warehouse", Name: "Snowflake"},
"src-eventstream": {Category: "eventStream", Name: "javascript"},
"src-s3": {Category: "warehouse", Name: "s3"},
}
t.Run("constructor validates HLL settings", func(t *testing.T) {
// Default settings are valid.
_, err := NewUniqueActivationRecordsReporter(logger.NOP, config.Default, stats.NOP)
require.NoError(t, err)
// An out-of-range precision (Log2m valid range is [4, 31]) must fail fast at
// construction rather than panicking later in the processor hot path.
badConf := config.New()
badConf.Set("ActivationRecords.precision", 99)
_, err = NewUniqueActivationRecordsReporter(logger.NOP, badConf, stats.NOP)
require.Error(t, err)
})
t.Run("GenerateReportsFromJobs", func(t *testing.T) {
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, config.Default, stats.NOP)
require.NoError(t, err)
testCases := []struct {
name string
jobs []*jobsdb.JobT
// verify is called with the resulting reports slice.
verify func(t *testing.T, reports []*ActivationRecord)
}{
{
name: "both fields present - one report on right grain, origin carried",
jobs: []*jobsdb.JobT{
prepareJob("src1", "dst1", "fp1", "org1", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Len(t, reports, 1)
r := reports[0]
require.Equal(t, "ws1", r.WorkspaceID)
require.Equal(t, "src1", r.SourceID)
require.Equal(t, "dst1", r.DestinationID)
require.Equal(t, "org1", r.Origin)
require.NotNil(t, r.FingerprintHll)
require.Equal(t, uint64(1), r.FingerprintHll.Cardinality())
},
},
{
name: "allow-listed warehouse missing fingerprint - skip",
jobs: []*jobsdb.JobT{
prepareJob("src1", "dst1", "", "org1", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "missing origin - skip",
jobs: []*jobsdb.JobT{
prepareJob("src1", "dst1", "fp1", "", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "missing destination_id in params - skip",
jobs: []*jobsdb.JobT{
prepareJobNoDstID("src1", "fp1", "org1", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "non-rETL source - skip even with fingerprint",
jobs: []*jobsdb.JobT{
{
// An event-stream source (per the config map) whose payload carries a
// client-stamped fingerprint must NOT be metered: MAR meters warehouse
// sources only, classified from config.
Parameters: fmt.Appendf(nil, `{"source_id":%q,"destination_id":%q}`, "src-eventstream", "dst1"),
EventPayload: []byte(`{"batch":[{"context":{"activation":{"fingerprint":"fp-injected","origin":"org1"}}}]}`),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: "ws1",
},
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "warehouse source not in allow-list - skip silently even with fingerprint",
jobs: []*jobsdb.JobT{
prepareJob("src-s3", "dst1", "fp-cloud-storage", "org1", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "config category is authoritative over a spoofed source_category param - skip",
jobs: []*jobsdb.JobT{
{
// The job param claims warehouse, but the source is event-stream in the
// config map; the param is not trusted, so the record is not metered.
Parameters: fmt.Appendf(nil, `{"source_id":%q,"destination_id":%q,"source_category":"warehouse"}`, "src-eventstream", "dst1"),
EventPayload: []byte(`{"batch":[{"context":{"activation":{"fingerprint":"fp-spoof","origin":"org1"}}}]}`),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: "ws1",
},
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "unknown source not in config - skip fail-closed",
jobs: []*jobsdb.JobT{
{
// A source_id absent from the config lookup resolves to "" and is
// skipped, even with a valid fingerprint.
Parameters: fmt.Appendf(nil, `{"source_id":%q,"destination_id":%q}`, "src-unknown", "dst1"),
EventPayload: []byte(`{"batch":[{"context":{"activation":{"fingerprint":"fp-unknown","origin":"org1"}}}]}`),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: "ws1",
},
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Empty(t, reports)
},
},
{
name: "duplicate fingerprint same connection - cardinality 1",
jobs: []*jobsdb.JobT{
prepareJob("src1", "dst1", "fp1", "org1", "ws1"),
prepareJob("src1", "dst1", "fp1", "org1", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Len(t, reports, 1)
require.Equal(t, uint64(1), reports[0].FingerprintHll.Cardinality())
},
},
{
name: "two destinations same fingerprint - two reports",
jobs: []*jobsdb.JobT{
prepareJob("src1", "dst1", "fp1", "org1", "ws1"),
prepareJob("src1", "dst2", "fp1", "org1", "ws1"),
},
verify: func(t *testing.T, reports []*ActivationRecord) {
require.Len(t, reports, 2)
dstIDs := make([]string, 0, len(reports))
for _, r := range reports {
dstIDs = append(dstIDs, r.DestinationID)
require.Equal(t, uint64(1), r.FingerprintHll.Cardinality())
}
require.ElementsMatch(t, []string{"dst1", "dst2"}, dstIDs)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
reports := reporter.GenerateReportsFromJobs(tc.jobs, metadataByID)
tc.verify(t, reports)
})
}
})
t.Run("GenerateReportsFromJobs_SourceDefinitionGateStats", func(t *testing.T) {
t.Run("warehouse source not in allow-list skips silently without missing fingerprint stat", func(t *testing.T) {
statsStore, err := memstats.New()
require.NoError(t, err)
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, config.New(), statsStore)
require.NoError(t, err)
reports := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{
prepareJob("src-s3", "dst1", "", "org1", "ws1"),
}, metadataByID)
require.Empty(t, reports)
require.Empty(t, statsStore.GetByName("activation_records_skipped"))
})
t.Run("allow-listed warehouse missing fingerprint increments missing_fingerprint", func(t *testing.T) {
statsStore, err := memstats.New()
require.NoError(t, err)
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, config.New(), statsStore)
require.NoError(t, err)
reports := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{
prepareJob("src1", "dst1", "", "org1", "ws1"),
}, metadataByID)
require.Empty(t, reports)
metric := statsStore.Get("activation_records_skipped", stats.Tags{"reason": "missing_fingerprint"})
require.NotNil(t, metric)
require.Equal(t, float64(1), metric.LastValue())
})
t.Run("allow-list config changes are observed without reconstructing reporter", func(t *testing.T) {
conf := config.New()
conf.Set("ActivationRecords.allowedSourceDefinitionNames", []string{"snowflake"})
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, conf, stats.NOP)
require.NoError(t, err)
customMetadataByID := map[string]SourceMetadata{
"src-custom": {Category: "warehouse", Name: "custom_sql"},
}
job := prepareJob("src-custom", "dst1", "fp1", "org1", "ws1")
require.Empty(t, reporter.GenerateReportsFromJobs([]*jobsdb.JobT{job}, customMetadataByID))
conf.Set("ActivationRecords.allowedSourceDefinitionNames", []string{"custom_sql"})
reports := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{job}, customMetadataByID)
require.Len(t, reports, 1)
require.Equal(t, uint64(1), reports[0].FingerprintHll.Cardinality())
})
})
t.Run("GenerateReportsFromJobs_MultiEventBatch", func(t *testing.T) {
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, config.Default, stats.NOP)
require.NoError(t, err)
// prepareTwoEventJob builds a job whose batch has TWO events with distinct fingerprints.
prepareTwoEventJob := func(sourceID, destinationID, fp1, fp2, origin, workspaceID string) *jobsdb.JobT {
return &jobsdb.JobT{
Parameters: fmt.Appendf(nil, `{"source_id":%q,"destination_id":%q}`, sourceID, destinationID),
EventPayload: fmt.Appendf(nil,
`{"batch":[{"context":{"activation":{"fingerprint":%q,"origin":%q}}},{"context":{"activation":{"fingerprint":%q,"origin":%q}}}]}`,
fp1, origin, fp2, origin,
),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: workspaceID,
}
}
t.Run("two distinct fingerprints in same batch => cardinality 2", func(t *testing.T) {
job := prepareTwoEventJob("src1", "dst1", "fp-1", "fp-2", "org1", "ws1")
reports := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{job}, metadataByID)
require.Len(t, reports, 1)
require.Equal(t, "org1", reports[0].Origin)
require.NotNil(t, reports[0].FingerprintHll)
require.Equal(t, uint64(2), reports[0].FingerprintHll.Cardinality())
})
t.Run("batch with one valid and one invalid element - valid element still metered", func(t *testing.T) {
// batch[0] has no fingerprint; batch[1] is valid.
job := &jobsdb.JobT{
Parameters: fmt.Appendf(nil, `{"source_id":%q,"destination_id":%q}`, "src1", "dst1"),
EventPayload: []byte(`{"batch":[{"context":{"activation":{"origin":"org1"}}},{"context":{"activation":{"fingerprint":"fp-valid","origin":"org1"}}}]}`),
UserID: uuid.NewString(),
UUID: uuid.New(),
CustomVal: "GW",
WorkspaceId: "ws1",
}
reports := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{job}, metadataByID)
require.Len(t, reports, 1)
require.Equal(t, uint64(1), reports[0].FingerprintHll.Cardinality())
})
})
t.Run("HLLSettings", func(t *testing.T) {
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, config.Default, stats.NOP)
require.NoError(t, err)
require.Equal(t, 16, reporter.hllSettings.Log2m)
require.Equal(t, 5, reporter.hllSettings.Regwidth)
})
t.Run("ForceFullSyncReplay", func(t *testing.T) {
// A force-full-sync replays the SAME jobs in a SEPARATE reporting cycle.
// The backend merges sketches across cycles (postgres hll union), so a
// replay of the same fingerprint MUST NOT inflate the cardinality —
// otherwise every retried sync would double-count monthly active records.
reporter, err := NewUniqueActivationRecordsReporter(logger.NOP, config.Default, stats.NOP)
require.NoError(t, err)
job := prepareJob("src1", "dst1", "fp1", "org1", "ws1")
first := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{job}, metadataByID)
require.Len(t, first, 1)
require.Equal(t, uint64(1), first[0].FingerprintHll.Cardinality())
second := reporter.GenerateReportsFromJobs([]*jobsdb.JobT{job}, metadataByID)
require.Len(t, second, 1)
require.Equal(t, uint64(1), second[0].FingerprintHll.Cardinality())
// Merge the two cycles the way the backend would: union both sketches into
// a fresh HLL built with the reporter's settings. Same fingerprint twice
// across cycles still collapses to cardinality 1.
union, err := hll.NewHll(*reporter.hllSettings)
require.NoError(t, err)
union.Union(*first[0].FingerprintHll)
union.Union(*second[0].FingerprintHll)
require.Equal(t, uint64(1), union.Cardinality())
})
}