|
| 1 | +# (c) Copyright IBM Corp. 2025 |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class RedisDisableTest < Minitest::Test |
| 6 | + def setup |
| 7 | + @redis_url = if ENV.key?('REDIS_URL') |
| 8 | + ENV['REDIS_URL'] |
| 9 | + else |
| 10 | + "redis://localhost:6379" |
| 11 | + end |
| 12 | + @redis_client = Redis.new(url: @redis_url) |
| 13 | + |
| 14 | + # Reset span filtering configuration before each test |
| 15 | + ::Instana::SpanFiltering.reset |
| 16 | + ::Instana::SpanFiltering.initialize |
| 17 | + |
| 18 | + # Reset Redis configuration |
| 19 | + ::Instana.config[:redis] = { :enabled => true } |
| 20 | + |
| 21 | + clear_all! |
| 22 | + end |
| 23 | + |
| 24 | + def teardown |
| 25 | + # Reset span filtering configuration after each test |
| 26 | + ::Instana::SpanFiltering.reset |
| 27 | + ::Instana::SpanFiltering.initialize |
| 28 | + |
| 29 | + # Reset Redis configuration |
| 30 | + ::Instana.config[:redis] = { :enabled => true } |
| 31 | + end |
| 32 | + |
| 33 | + def test_redis_disabled_by_configuration |
| 34 | + # Set Redis to be disabled via configuration |
| 35 | + ::Instana.config[:redis][:enabled] = false |
| 36 | + |
| 37 | + # Execute Redis operation |
| 38 | + Instana.tracer.in_span(:redis_test) do |
| 39 | + @redis_client.set('hello', 'world') |
| 40 | + end |
| 41 | + |
| 42 | + # Verify that only the parent span is reported (Redis span is disabled) |
| 43 | + spans = ::Instana.processor.queued_spans |
| 44 | + assert_equal 1, spans.length |
| 45 | + assert_equal :sdk, spans[0][:n] |
| 46 | + end |
| 47 | + |
| 48 | + def test_redis_disabled_via_databases_category |
| 49 | + # Create a mock configuration that disables the databases category |
| 50 | + config = ::Instana::SpanFiltering::Configuration.new |
| 51 | + |
| 52 | + # Simulate disabling databases category |
| 53 | + config.send(:update_instana_config_for_disabled_technology, 'databases') |
| 54 | + |
| 55 | + # Execute Redis operation |
| 56 | + Instana.tracer.in_span(:redis_test) do |
| 57 | + @redis_client.set('hello', 'world') |
| 58 | + end |
| 59 | + |
| 60 | + # Verify that only the parent span is reported (Redis span is disabled) |
| 61 | + spans = ::Instana.processor.queued_spans |
| 62 | + assert_equal 1, spans.length |
| 63 | + assert_equal :sdk, spans[0][:n] |
| 64 | + end |
| 65 | + |
| 66 | + def test_redis_disabled_via_yaml_config |
| 67 | + # Create a test YAML configuration file |
| 68 | + yaml_content = <<~YAML |
| 69 | + tracing: |
| 70 | + disable: |
| 71 | + - redis |
| 72 | + YAML |
| 73 | + |
| 74 | + File.write('test_redis_config.yaml', yaml_content) |
| 75 | + ENV['INSTANA_CONFIG_PATH'] = 'test_redis_config.yaml' |
| 76 | + |
| 77 | + # Create a new configuration that should load from our YAML file |
| 78 | + ::Instana::SpanFiltering::Configuration.new |
| 79 | + |
| 80 | + # Execute Redis operation |
| 81 | + Instana.tracer.in_span(:redis_test) do |
| 82 | + @redis_client.set('hello', 'world') |
| 83 | + end |
| 84 | + |
| 85 | + # Verify that only the parent span is reported (Redis span is disabled) |
| 86 | + spans = ::Instana.processor.queued_spans |
| 87 | + assert_equal 1, spans.length |
| 88 | + assert_equal :sdk, spans[0][:n] |
| 89 | + ensure |
| 90 | + # Remove test config file |
| 91 | + File.unlink('test_redis_config.yaml') if File.exist?('test_redis_config.yaml') |
| 92 | + ENV.delete('INSTANA_CONFIG_PATH') |
| 93 | + end |
| 94 | + |
| 95 | + def test_redis_disabled_via_env_var |
| 96 | + # Set environment variable to disable Redis |
| 97 | + ENV['INSTANA_TRACING_DISABLE'] = 'redis' |
| 98 | + |
| 99 | + # Create a new configuration that should load from our environment variable |
| 100 | + ::Instana::SpanFiltering::Configuration.new |
| 101 | + |
| 102 | + # Execute Redis operation |
| 103 | + Instana.tracer.in_span(:redis_test) do |
| 104 | + @redis_client.set('hello', 'world') |
| 105 | + end |
| 106 | + |
| 107 | + # Verify that only the parent span is reported (Redis span is disabled) |
| 108 | + spans = ::Instana.processor.queued_spans |
| 109 | + assert_equal 1, spans.length |
| 110 | + assert_equal :sdk, spans[0][:n] |
| 111 | + ensure |
| 112 | + ENV.delete('INSTANA_TRACING_DISABLE') |
| 113 | + end |
| 114 | + |
| 115 | + def test_redis_disabled_via_databases_env_var |
| 116 | + # Set environment variable to disable databases category |
| 117 | + ENV['INSTANA_TRACING_DISABLE'] = 'databases' |
| 118 | + |
| 119 | + # Create a new configuration that should load from our environment variable |
| 120 | + ::Instana::SpanFiltering::Configuration.new |
| 121 | + |
| 122 | + # Execute Redis operation |
| 123 | + Instana.tracer.in_span(:redis_test) do |
| 124 | + @redis_client.set('hello', 'world') |
| 125 | + end |
| 126 | + |
| 127 | + # Verify that only the parent span is reported (Redis span is disabled) |
| 128 | + spans = ::Instana.processor.queued_spans |
| 129 | + assert_equal 1, spans.length |
| 130 | + assert_equal :sdk, spans[0][:n] |
| 131 | + ensure |
| 132 | + ENV.delete('INSTANA_TRACING_DISABLE') |
| 133 | + end |
| 134 | + |
| 135 | + def test_redis_not_disabled_by_default |
| 136 | + # Execute Redis operation |
| 137 | + Instana.tracer.in_span(:redis_test) do |
| 138 | + @redis_client.set('hello', 'world') |
| 139 | + end |
| 140 | + |
| 141 | + # Verify that both spans are reported (Redis span is not disabled) |
| 142 | + spans = ::Instana.processor.queued_spans |
| 143 | + assert_equal 2, spans.length |
| 144 | + first_span, second_span = spans.to_a.reverse |
| 145 | + assert_equal :sdk, first_span[:n] |
| 146 | + assert_equal :redis, second_span[:n] |
| 147 | + end |
| 148 | + |
| 149 | + private |
| 150 | + |
| 151 | + def clear_all! |
| 152 | + ::Instana.processor.clear! |
| 153 | + ::Instana.tracer.clear! |
| 154 | + end |
| 155 | +end |
0 commit comments