@@ -11,6 +11,8 @@ import (
1111
1212 "github.com/rudderlabs/rudder-go-kit/config"
1313 "github.com/rudderlabs/rudder-go-kit/logger"
14+ "github.com/rudderlabs/rudder-go-kit/stats"
15+ "github.com/rudderlabs/rudder-go-kit/stats/memstats"
1416)
1517
1618func TestBadger (t * testing.T ) {
@@ -23,15 +25,40 @@ func TestBadger(t *testing.T) {
2325
2426 t .Run ("should put and get keys" , func (t * testing.T ) {
2527 assert .Equal (t , 3000 * time .Millisecond , ttl .Load ())
26- es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , BadgerEventSamplerMetricsPathName , conf , log )
28+ statsStore , err := memstats .New ()
29+ require .NoError (t , err )
30+ es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , MetricsReporting , conf , log , statsStore )
2731 _ = es .Put ("key1" )
2832 _ = es .Put ("key2" )
2933 _ = es .Put ("key3" )
34+
35+ require .Equal (t , statsStore .Get (StatReportingEventSamplerRequestsTotal , map [string ]string {
36+ "type" : BadgerTypeEventSampler ,
37+ "module" : MetricsReporting ,
38+ "operation" : "put" ,
39+ }).LastValue (), float64 (3 ))
40+ require .Equal (t , len (statsStore .Get (StatReportingEventSamplerRequestDuration , map [string ]string {
41+ "type" : BadgerTypeEventSampler ,
42+ "module" : MetricsReporting ,
43+ "operation" : "put" ,
44+ }).Durations ()), 3 )
45+
3046 val1 , _ := es .Get ("key1" )
3147 val2 , _ := es .Get ("key2" )
3248 val3 , _ := es .Get ("key3" )
3349 val4 , _ := es .Get ("key4" )
3450
51+ require .Equal (t , statsStore .Get (StatReportingEventSamplerRequestsTotal , map [string ]string {
52+ "type" : BadgerTypeEventSampler ,
53+ "module" : MetricsReporting ,
54+ "operation" : "get" ,
55+ }).LastValue (), float64 (4 ))
56+ require .Equal (t , len (statsStore .Get (StatReportingEventSamplerRequestDuration , map [string ]string {
57+ "type" : BadgerTypeEventSampler ,
58+ "module" : MetricsReporting ,
59+ "operation" : "get" ,
60+ }).Durations ()), 4 )
61+
3562 assert .True (t , val1 , "Expected key1 to be present" )
3663 assert .True (t , val2 , "Expected key2 to be present" )
3764 assert .True (t , val3 , "Expected key3 to be present" )
@@ -43,7 +70,7 @@ func TestBadger(t *testing.T) {
4370 conf .Set ("Reporting.eventSampling.durationInMinutes" , 100 )
4471 assert .Equal (t , 100 * time .Millisecond , ttl .Load ())
4572
46- es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , BadgerEventSamplerMetricsPathName , conf , log )
73+ es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , MetricsReporting , conf , log , stats . NOP )
4774 defer es .Close ()
4875
4976 _ = es .Put ("key1" )
@@ -65,15 +92,40 @@ func TestInMemoryCache(t *testing.T) {
6592
6693 t .Run ("should put and get keys" , func (t * testing.T ) {
6794 assert .Equal (t , 3000 * time .Millisecond , ttl .Load ())
68- es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , BadgerEventSamplerMetricsPathName , conf , log )
95+ statsStore , err := memstats .New ()
96+ require .NoError (t , err )
97+ es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , MetricsReporting , conf , log , statsStore )
6998 _ = es .Put ("key1" )
7099 _ = es .Put ("key2" )
71100 _ = es .Put ("key3" )
101+
102+ require .Equal (t , statsStore .Get (StatReportingEventSamplerRequestsTotal , map [string ]string {
103+ "type" : InMemoryCacheTypeEventSampler ,
104+ "module" : MetricsReporting ,
105+ "operation" : "put" ,
106+ }).LastValue (), float64 (3 ))
107+ require .Equal (t , len (statsStore .Get (StatReportingEventSamplerRequestDuration , map [string ]string {
108+ "type" : InMemoryCacheTypeEventSampler ,
109+ "module" : MetricsReporting ,
110+ "operation" : "put" ,
111+ }).Durations ()), 3 )
112+
72113 val1 , _ := es .Get ("key1" )
73114 val2 , _ := es .Get ("key2" )
74115 val3 , _ := es .Get ("key3" )
75116 val4 , _ := es .Get ("key4" )
76117
118+ require .Equal (t , statsStore .Get (StatReportingEventSamplerRequestsTotal , map [string ]string {
119+ "type" : InMemoryCacheTypeEventSampler ,
120+ "module" : MetricsReporting ,
121+ "operation" : "get" ,
122+ }).LastValue (), float64 (4 ))
123+ require .Equal (t , len (statsStore .Get (StatReportingEventSamplerRequestDuration , map [string ]string {
124+ "type" : InMemoryCacheTypeEventSampler ,
125+ "module" : MetricsReporting ,
126+ "operation" : "get" ,
127+ }).Durations ()), 4 )
128+
77129 assert .True (t , val1 , "Expected key1 to be present" )
78130 assert .True (t , val2 , "Expected key2 to be present" )
79131 assert .True (t , val3 , "Expected key3 to be present" )
@@ -83,7 +135,7 @@ func TestInMemoryCache(t *testing.T) {
83135 t .Run ("should not get evicted keys" , func (t * testing.T ) {
84136 conf .Set ("Reporting.eventSampling.durationInMinutes" , 100 )
85137 assert .Equal (t , 100 * time .Millisecond , ttl .Load ())
86- es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , BadgerEventSamplerMetricsPathName , conf , log )
138+ es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , MetricsReporting , conf , log , stats . NOP )
87139 _ = es .Put ("key1" )
88140
89141 require .Eventually (t , func () bool {
@@ -95,19 +147,43 @@ func TestInMemoryCache(t *testing.T) {
95147 t .Run ("should not add keys if length exceeds" , func (t * testing.T ) {
96148 conf .Set ("Reporting.eventSampling.durationInMinutes" , 3000 )
97149 assert .Equal (t , 3000 * time .Millisecond , ttl .Load ())
98- es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , BadgerEventSamplerMetricsPathName , conf , log )
150+ statsStore , err := memstats .New ()
151+ require .NoError (t , err )
152+ es , _ := NewEventSampler (ctx , ttl , eventSamplerType , eventSamplingCardinality , MetricsReporting , conf , log , statsStore )
99153 _ = es .Put ("key1" )
100154 _ = es .Put ("key2" )
101155 _ = es .Put ("key3" )
102156 _ = es .Put ("key4" )
103157 _ = es .Put ("key5" )
104158
159+ require .Equal (t , statsStore .Get (StatReportingEventSamplerRequestsTotal , map [string ]string {
160+ "type" : InMemoryCacheTypeEventSampler ,
161+ "module" : MetricsReporting ,
162+ "operation" : "put" ,
163+ }).LastValue (), float64 (3 ))
164+ require .Equal (t , len (statsStore .Get (StatReportingEventSamplerRequestDuration , map [string ]string {
165+ "type" : InMemoryCacheTypeEventSampler ,
166+ "module" : MetricsReporting ,
167+ "operation" : "put" ,
168+ }).Durations ()), 3 )
169+
105170 val1 , _ := es .Get ("key1" )
106171 val2 , _ := es .Get ("key2" )
107172 val3 , _ := es .Get ("key3" )
108173 val4 , _ := es .Get ("key4" )
109174 val5 , _ := es .Get ("key5" )
110175
176+ require .Equal (t , statsStore .Get (StatReportingEventSamplerRequestsTotal , map [string ]string {
177+ "type" : InMemoryCacheTypeEventSampler ,
178+ "module" : MetricsReporting ,
179+ "operation" : "get" ,
180+ }).LastValue (), float64 (5 ))
181+ require .Equal (t , len (statsStore .Get (StatReportingEventSamplerRequestDuration , map [string ]string {
182+ "type" : InMemoryCacheTypeEventSampler ,
183+ "module" : MetricsReporting ,
184+ "operation" : "get" ,
185+ }).Durations ()), 5 )
186+
111187 assert .True (t , val1 , "Expected key1 to be present" )
112188 assert .True (t , val2 , "Expected key2 to be present" )
113189 assert .True (t , val3 , "Expected key3 to be present" )
@@ -147,9 +223,10 @@ func BenchmarkEventSampler(b *testing.B) {
147223 ttl ,
148224 eventSamplerType ,
149225 eventSamplingCardinality ,
150- BadgerEventSamplerMetricsPathName ,
226+ MetricsReporting ,
151227 conf ,
152228 log ,
229+ stats .NOP ,
153230 )
154231 require .NoError (b , err )
155232
0 commit comments