@@ -47,7 +47,7 @@ const (
47
47
notInSpanAttrName0 = "shouldBeInMetric"
48
48
notInSpanAttrName1 = "shouldNotBeInMetric"
49
49
regionResourceAttrName = "region"
50
- DimensionsCacheSize = 2
50
+ DimensionsCacheSize = 1000
51
51
52
52
sampleRegion = "us-east-1"
53
53
sampleLatency = float64 (11 )
@@ -144,7 +144,7 @@ func TestProcessorConcurrentShutdown(t *testing.T) {
144
144
ticker := mockClock .NewTicker (time .Nanosecond )
145
145
146
146
// Test
147
- p := newProcessorImp (mexp , tcon , nil , cumulative , logger , ticker )
147
+ p := newProcessorImp (mexp , tcon , nil , cumulative , logger , ticker , DimensionsCacheSize )
148
148
err := p .Start (ctx , mhost )
149
149
require .NoError (t , err )
150
150
@@ -230,7 +230,7 @@ func TestProcessorConsumeTracesErrors(t *testing.T) {
230
230
tcon := & mocks.TracesConsumer {}
231
231
tcon .On ("ConsumeTraces" , mock .Anything , mock .Anything ).Return (fakeErr )
232
232
233
- p := newProcessorImp (mexp , tcon , nil , cumulative , logger , nil )
233
+ p := newProcessorImp (mexp , tcon , nil , cumulative , logger , nil , DimensionsCacheSize )
234
234
235
235
traces := buildSampleTrace ()
236
236
@@ -262,7 +262,7 @@ func TestProcessorConsumeMetricsErrors(t *testing.T) {
262
262
263
263
mockClock := clock .NewMock (time .Now ())
264
264
ticker := mockClock .NewTicker (time .Nanosecond )
265
- p := newProcessorImp (mexp , tcon , nil , cumulative , logger , ticker )
265
+ p := newProcessorImp (mexp , tcon , nil , cumulative , logger , ticker , DimensionsCacheSize )
266
266
267
267
exporters := map [component.DataType ]map [component.ID ]component.Component {}
268
268
mhost := & mocks.Host {}
@@ -362,7 +362,7 @@ func TestProcessorConsumeTraces(t *testing.T) {
362
362
mockClock := clock .NewMock (time .Now ())
363
363
ticker := mockClock .NewTicker (time .Nanosecond )
364
364
365
- p := newProcessorImp (mexp , tcon , & defaultNullValue , tc .aggregationTemporality , zaptest .NewLogger (t ), ticker )
365
+ p := newProcessorImp (mexp , tcon , & defaultNullValue , tc .aggregationTemporality , zaptest .NewLogger (t ), ticker , DimensionsCacheSize )
366
366
367
367
exporters := map [component.DataType ]map [component.ID ]component.Component {}
368
368
mhost := & mocks.Host {}
@@ -387,39 +387,61 @@ func TestProcessorConsumeTraces(t *testing.T) {
387
387
}
388
388
}
389
389
390
- func TestMetricKeyCache (t * testing.T ) {
390
+ func TestMetricCache (t * testing.T ) {
391
+ var wg sync.WaitGroup
392
+
391
393
mexp := & mocks.MetricsConsumer {}
392
- tcon := & mocks.TracesConsumer {}
394
+ mexp .On ("ConsumeMetrics" , mock .Anything , mock .MatchedBy (func (input pmetric.Metrics ) bool {
395
+ wg .Done ()
396
+ return true
397
+ })).Return (nil )
393
398
394
- mexp . On ( "ConsumeMetrics" , mock . Anything , mock . Anything ). Return ( nil )
399
+ tcon := & mocks. TracesConsumer {}
395
400
tcon .On ("ConsumeTraces" , mock .Anything , mock .Anything ).Return (nil )
396
401
397
402
defaultNullValue := pcommon .NewValueStr ("defaultNullValue" )
398
- p := newProcessorImp (mexp , tcon , & defaultNullValue , cumulative , zaptest .NewLogger (t ), nil )
399
- traces := buildSampleTrace ()
403
+ mockClock := clock .NewMock (time .Now ())
404
+ ticker := mockClock .NewTicker (time .Nanosecond )
405
+ dimensionsCacheSize := 2
406
+
407
+ p := newProcessorImp (mexp , tcon , & defaultNullValue , cumulative , zaptest .NewLogger (t ), ticker , dimensionsCacheSize )
408
+
409
+ exporters := map [component.DataType ]map [component.ID ]component.Component {}
410
+ mhost := & mocks.Host {}
411
+ mhost .On ("GetExporters" ).Return (exporters )
400
412
401
413
// Test
402
414
ctx := metadata .NewIncomingContext (context .Background (), nil )
415
+ err := p .Start (ctx , mhost )
416
+ require .NoError (t , err )
403
417
404
418
// 0 key was cached at beginning
405
- assert .Zero (t , p .metricKeyToDimensions .Len ())
419
+ assert .Zero (t , len (p .histograms ))
420
+
421
+ traces := buildSampleTrace ()
422
+ require .Condition (t , func () bool {
423
+ return traces .SpanCount () >= dimensionsCacheSize
424
+ })
425
+
426
+ err = p .ConsumeTraces (ctx , traces )
427
+ wg .Add (1 )
428
+ mockClock .Add (time .Nanosecond )
429
+ wg .Wait ()
406
430
407
- err := p .ConsumeTraces (ctx , traces )
408
431
// Validate
409
432
require .NoError (t , err )
410
433
// 2 key was cached, 1 key was evicted and cleaned after the processing
411
- assert .Eventually (t , func () bool {
412
- return p .metricKeyToDimensions .Len () == DimensionsCacheSize
413
- }, 10 * time .Second , time .Millisecond * 100 )
434
+ assert .Equal (t , len (p .histograms ), dimensionsCacheSize )
414
435
415
436
// consume another batch of traces
416
437
err = p .ConsumeTraces (ctx , traces )
417
438
require .NoError (t , err )
439
+ wg .Add (1 )
440
+ mockClock .Add (time .Nanosecond )
441
+ wg .Wait ()
418
442
419
443
// 2 key was cached, other keys were evicted and cleaned after the processing
420
- assert .Eventually (t , func () bool {
421
- return p .metricKeyToDimensions .Len () == DimensionsCacheSize
422
- }, 10 * time .Second , time .Millisecond * 100 )
444
+ assert .Equal (t , len (p .histograms ), dimensionsCacheSize )
423
445
}
424
446
425
447
func BenchmarkProcessorConsumeTraces (b * testing.B ) {
@@ -431,7 +453,7 @@ func BenchmarkProcessorConsumeTraces(b *testing.B) {
431
453
tcon .On ("ConsumeTraces" , mock .Anything , mock .Anything ).Return (nil )
432
454
433
455
defaultNullValue := pcommon .NewValueStr ("defaultNullValue" )
434
- p := newProcessorImp (mexp , tcon , & defaultNullValue , cumulative , zaptest .NewLogger (b ), nil )
456
+ p := newProcessorImp (mexp , tcon , & defaultNullValue , cumulative , zaptest .NewLogger (b ), nil , DimensionsCacheSize )
435
457
436
458
traces := buildSampleTrace ()
437
459
@@ -442,10 +464,10 @@ func BenchmarkProcessorConsumeTraces(b *testing.B) {
442
464
}
443
465
}
444
466
445
- func newProcessorImp (mexp * mocks.MetricsConsumer , tcon * mocks.TracesConsumer , defaultNullValue * pcommon.Value , temporality string , logger * zap.Logger , ticker * clock.Ticker ) * processorImp {
467
+ func newProcessorImp (mexp * mocks.MetricsConsumer , tcon * mocks.TracesConsumer , defaultNullValue * pcommon.Value , temporality string , logger * zap.Logger , ticker * clock.Ticker , cacheSize int ) * processorImp {
446
468
defaultNotInSpanAttrVal := pcommon .NewValueStr ("defaultNotInSpanAttrVal" )
447
469
// use size 2 for LRU cache for testing purpose
448
- metricKeyToDimensions , err := cache.NewCache [metricKey , pcommon.Map ](DimensionsCacheSize )
470
+ metricKeyToDimensions , err := cache.NewCache [metricKey , pcommon.Map ](cacheSize )
449
471
if err != nil {
450
472
panic (err )
451
473
}
@@ -979,8 +1001,7 @@ func TestConsumeTracesEvictedCacheKey(t *testing.T) {
979
1001
mockClock := clock .NewMock (time .Now ())
980
1002
ticker := mockClock .NewTicker (time .Nanosecond )
981
1003
982
- // Note: default dimension key cache size is 2.
983
- p := newProcessorImp (mexp , tcon , & defaultNullValue , cumulative , zaptest .NewLogger (t ), ticker )
1004
+ p := newProcessorImp (mexp , tcon , & defaultNullValue , cumulative , zaptest .NewLogger (t ), ticker , DimensionsCacheSize )
984
1005
985
1006
exporters := map [component.DataType ]map [component.ID ]component.Component {}
986
1007
0 commit comments