@@ -41,9 +41,10 @@ def redis_gte_5?
41
41
redis_version_major &.>=( 5 )
42
42
end
43
43
44
+ let ( :config ) { { db_statement : :include } }
45
+
44
46
before do
45
47
# ensure obfuscation is off if it was previously set in a different test
46
- config = { db_statement : :include }
47
48
instrumentation . install ( config )
48
49
exporter . reset
49
50
end
@@ -389,4 +390,96 @@ def redis_gte_5?
389
390
end
390
391
end
391
392
end
393
+
394
+ if defined? ( OpenTelemetry ::Metrics )
395
+ describe 'metrics not enabled' do
396
+ it 'will not be enabled' do
397
+ assert ( instrumentation . metrics_defined? )
398
+ refute ( instrumentation . metrics_enabled? )
399
+ end
400
+ end
401
+
402
+ describe 'metrics enabled' do
403
+ let ( :config ) { { db_statement : :include , metrics : true } }
404
+ let ( :metric_snapshot ) do
405
+ metrics_exporter . pull
406
+ metrics_exporter . metric_snapshots . last
407
+ end
408
+
409
+ it 'will be enabled' do
410
+ assert ( instrumentation . metrics_defined? )
411
+ assert ( instrumentation . metrics_enabled? )
412
+ end
413
+
414
+ it 'works' , with_metrics_sdk : true do
415
+ skip if redis_gte_5?
416
+
417
+ redis = redis_with_auth
418
+ key = SecureRandom . hex
419
+ 10 . times { redis . incr ( key ) }
420
+ redis . expire ( key , 1 )
421
+
422
+ _ ( metric_snapshot . data_points . length ) . must_equal ( 3 )
423
+
424
+ metric_snapshot . data_points . each do |data_point |
425
+ _ ( data_point . attributes [ 'db.system' ] ) . must_equal ( 'redis' )
426
+ end
427
+
428
+ by_operation_name = metric_snapshot . data_points . each_with_object ( { } ) { |d , res | res [ d . attributes [ 'db.operation.name' ] ] = d }
429
+ _ ( by_operation_name . keys . sort ) . must_equal ( %w[ auth expire incr ] )
430
+
431
+ _ ( by_operation_name [ 'auth' ] . count ) . must_equal ( 1 )
432
+ _ ( by_operation_name [ 'incr' ] . count ) . must_equal ( 10 )
433
+ _ ( by_operation_name [ 'expire' ] . count ) . must_equal ( 1 )
434
+ end
435
+
436
+ it 'works v5' , with_metrics_sdk : true do
437
+ skip unless redis_gte_5?
438
+
439
+ redis = redis_with_auth
440
+ key = SecureRandom . hex
441
+ 10 . times { redis . incr ( key ) }
442
+ redis . expire ( key , 1 )
443
+
444
+ _ ( metric_snapshot . data_points . length ) . must_equal ( 3 )
445
+
446
+ metric_snapshot . data_points . each do |data_point |
447
+ _ ( data_point . attributes [ 'db.system' ] ) . must_equal ( 'redis' )
448
+ end
449
+
450
+ by_operation_name = metric_snapshot . data_points . each_with_object ( { } ) { |d , res | res [ d . attributes [ 'db.operation.name' ] ] = d }
451
+ _ ( by_operation_name . keys . sort ) . must_equal ( %w[ PIPELINED expire incr ] )
452
+
453
+ _ ( by_operation_name [ 'PIPELINED' ] . count ) . must_equal ( 1 )
454
+ _ ( by_operation_name [ 'incr' ] . count ) . must_equal ( 10 )
455
+ _ ( by_operation_name [ 'expire' ] . count ) . must_equal ( 1 )
456
+ end
457
+
458
+ it 'adds errors' , with_metrics_sdk : true do
459
+ skip if redis_gte_5?
460
+
461
+ redis = redis_with_auth
462
+ key = SecureRandom . hex
463
+ redis . setex ( key , 100 , 'string_value' )
464
+ expect { redis . incr ( key ) } . must_raise ( Redis ::CommandError )
465
+
466
+ last_data_point = metric_snapshot . data_points . last
467
+ _ ( last_data_point . attributes [ 'db.operation.name' ] ) . must_equal ( 'incr' )
468
+ _ ( last_data_point . attributes [ 'error.type' ] ) . must_equal ( 'RedisClient::CommandError' )
469
+ end
470
+
471
+ it 'adds errors v5' , with_metrics_sdk : true do
472
+ skip unless redis_gte_5?
473
+
474
+ redis = redis_with_auth
475
+ key = SecureRandom . hex
476
+ redis . setex ( key , 100 , 'string_value' )
477
+ expect { redis . incr ( key ) } . must_raise ( Redis ::CommandError )
478
+
479
+ last_data_point = metric_snapshot . data_points . last
480
+ _ ( last_data_point . attributes [ 'db.operation.name' ] ) . must_equal ( 'incr' )
481
+ _ ( last_data_point . attributes [ 'error.type' ] ) . must_equal ( 'RedisClient::CommandError' )
482
+ end
483
+ end
484
+ end
392
485
end unless ENV [ 'OMIT_SERVICES' ]
0 commit comments