|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe Sentry::Rails::Tracing::ActiveSupportSubscriber, :subscriber, type: :request do |
| 6 | + let(:transport) do |
| 7 | + Sentry.get_current_client.transport |
| 8 | + end |
| 9 | + |
| 10 | + context "when transaction is sampled" do |
| 11 | + before do |
| 12 | + make_basic_app do |config, app| |
| 13 | + config.traces_sample_rate = 1.0 |
| 14 | + config.rails.tracing_subscribers = [described_class] |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + it "tracks cache write" do |
| 19 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 20 | + Sentry.get_current_scope.set_span(transaction) |
| 21 | + |
| 22 | + Rails.cache.write("my_cache_key", "my_cache_value") |
| 23 | + transaction.finish |
| 24 | + |
| 25 | + expect(transport.events.count).to eq(1) |
| 26 | + cache_transaction = transport.events.first.to_hash |
| 27 | + expect(cache_transaction[:type]).to eq("transaction") |
| 28 | + |
| 29 | + expect(cache_transaction[:spans].count).to eq(1) |
| 30 | + expect(cache_transaction[:spans][0][:op]).to eq("cache.put") |
| 31 | + expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails") |
| 32 | + end |
| 33 | + |
| 34 | + # |
| 35 | + it "tracks cache increment" do |
| 36 | + skip("Tracks on Rails 8.0 for all Cache Stores; Until then only MemCached and Redis Stores.") if Rails.version.to_f < 8.0 |
| 37 | + |
| 38 | + Rails.cache.write("my_cache_key", 0) |
| 39 | + |
| 40 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 41 | + Sentry.get_current_scope.set_span(transaction) |
| 42 | + Rails.cache.increment("my_cache_key") |
| 43 | + |
| 44 | + transaction.finish |
| 45 | + |
| 46 | + expect(Rails.cache.read("my_cache_key")).to eq(1) |
| 47 | + expect(transport.events.count).to eq(1) |
| 48 | + cache_transaction = transport.events.first.to_hash |
| 49 | + expect(cache_transaction[:type]).to eq("transaction") |
| 50 | + expect(cache_transaction[:spans].count).to eq(2) |
| 51 | + expect(cache_transaction[:spans][1][:op]).to eq("cache.put") |
| 52 | + expect(cache_transaction[:spans][1][:origin]).to eq("auto.cache.rails") |
| 53 | + end |
| 54 | + |
| 55 | + # |
| 56 | + it "tracks cache decrement" do |
| 57 | + skip("Tracks on Rails 8.0 for all Cache Stores; Until then only MemCached and Redis Stores.") if Rails.version.to_f < 8.0 |
| 58 | + |
| 59 | + Rails.cache.write("my_cache_key", 0) |
| 60 | + |
| 61 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 62 | + Sentry.get_current_scope.set_span(transaction) |
| 63 | + Rails.cache.decrement("my_cache_key") |
| 64 | + |
| 65 | + transaction.finish |
| 66 | + |
| 67 | + expect(transport.events.count).to eq(1) |
| 68 | + cache_transaction = transport.events.first.to_hash |
| 69 | + expect(cache_transaction[:type]).to eq("transaction") |
| 70 | + expect(cache_transaction[:spans].count).to eq(2) |
| 71 | + expect(cache_transaction[:spans][1][:op]).to eq("cache.put") |
| 72 | + expect(cache_transaction[:spans][1][:origin]).to eq("auto.cache.rails") |
| 73 | + end |
| 74 | + |
| 75 | + it "tracks cache read" do |
| 76 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 77 | + Sentry.get_current_scope.set_span(transaction) |
| 78 | + Rails.cache.read("my_cache_key") |
| 79 | + |
| 80 | + transaction.finish |
| 81 | + |
| 82 | + expect(transport.events.count).to eq(1) |
| 83 | + cache_transaction = transport.events.first.to_hash |
| 84 | + expect(cache_transaction[:type]).to eq("transaction") |
| 85 | + expect(cache_transaction[:spans].count).to eq(1) |
| 86 | + expect(cache_transaction[:spans][0][:op]).to eq("cache.get") |
| 87 | + expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails") |
| 88 | + end |
| 89 | + |
| 90 | + it "tracks cache delete" do |
| 91 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 92 | + Sentry.get_current_scope.set_span(transaction) |
| 93 | + |
| 94 | + Rails.cache.read("my_cache_key") |
| 95 | + |
| 96 | + transaction.finish |
| 97 | + |
| 98 | + expect(transport.events.count).to eq(1) |
| 99 | + cache_transaction = transport.events.first.to_hash |
| 100 | + expect(cache_transaction[:type]).to eq("transaction") |
| 101 | + expect(cache_transaction[:spans].count).to eq(1) |
| 102 | + expect(cache_transaction[:spans][0][:op]).to eq("cache.get") |
| 103 | + expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails") |
| 104 | + end |
| 105 | + it "tracks cache prune" do |
| 106 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 107 | + Sentry.get_current_scope.set_span(transaction) |
| 108 | + |
| 109 | + Rails.cache.write("my_cache_key", 123, expires_in: 0.seconds) |
| 110 | + |
| 111 | + Rails.cache.prune(0) |
| 112 | + |
| 113 | + transaction.finish |
| 114 | + |
| 115 | + expect(transport.events.count).to eq(1) |
| 116 | + cache_transaction = transport.events.first.to_hash |
| 117 | + expect(cache_transaction[:type]).to eq("transaction") |
| 118 | + expect(cache_transaction[:spans].count).to eq(2) |
| 119 | + expect(cache_transaction[:spans][1][:op]).to eq("cache.flush") |
| 120 | + expect(cache_transaction[:spans][1][:origin]).to eq("auto.cache.rails") |
| 121 | + end |
| 122 | + |
| 123 | + it "tracks sets cache hit" do |
| 124 | + skip("cache.hit is unset on Rails 6.0.x.") if Rails.version.to_i == 6 |
| 125 | + |
| 126 | + Rails.cache.write("my_cache_key", "my_cache_value") |
| 127 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 128 | + Sentry.get_current_scope.set_span(transaction) |
| 129 | + Rails.cache.read("my_cache_key") |
| 130 | + Rails.cache.read("my_cache_key_non_existing") |
| 131 | + |
| 132 | + transaction.finish |
| 133 | + expect(transport.events.count).to eq(1) |
| 134 | + cache_transaction = transport.events.first.to_hash |
| 135 | + expect(cache_transaction[:type]).to eq("transaction") |
| 136 | + expect(cache_transaction[:spans].count).to eq(2) |
| 137 | + expect(cache_transaction[:spans][0][:op]).to eq("cache.get") |
| 138 | + expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails") |
| 139 | + expect(cache_transaction[:spans][0][:data]['cache.key']).to eq(["my_cache_key"]) |
| 140 | + expect(cache_transaction[:spans][0][:data]['cache.hit']).to eq(true) |
| 141 | + |
| 142 | + expect(cache_transaction[:spans][1][:op]).to eq("cache.get") |
| 143 | + expect(cache_transaction[:spans][1][:origin]).to eq("auto.cache.rails") |
| 144 | + expect(cache_transaction[:spans][1][:data]['cache.key']).to eq(["my_cache_key_non_existing"]) |
| 145 | + expect(cache_transaction[:spans][1][:data]['cache.hit']).to eq(false) |
| 146 | + end |
| 147 | + |
| 148 | + it "tracks cache delete" do |
| 149 | + Rails.cache.write("my_cache_key", "my_cache_value") |
| 150 | + transaction = Sentry::Transaction.new(sampled: true, hub: Sentry.get_current_hub) |
| 151 | + Sentry.get_current_scope.set_span(transaction) |
| 152 | + Rails.cache.delete("my_cache_key") |
| 153 | + |
| 154 | + transaction.finish |
| 155 | + expect(transport.events.count).to eq(1) |
| 156 | + cache_transaction = transport.events.first.to_hash |
| 157 | + expect(cache_transaction[:type]).to eq("transaction") |
| 158 | + expect(cache_transaction[:spans].count).to eq(1) |
| 159 | + expect(cache_transaction[:spans][0][:op]).to eq("cache.remove") |
| 160 | + expect(cache_transaction[:spans][0][:origin]).to eq("auto.cache.rails") |
| 161 | + expect(cache_transaction[:spans][0][:data]['cache.key']).to eq(["my_cache_key"]) |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + context "when transaction is not sampled" do |
| 166 | + before do |
| 167 | + make_basic_app |
| 168 | + end |
| 169 | + |
| 170 | + it "doesn't record spans" do |
| 171 | + Rails.cache.write("my_cache_key", "my_cache_value") |
| 172 | + |
| 173 | + expect(transport.events.count).to eq(0) |
| 174 | + end |
| 175 | + end |
| 176 | +end |
0 commit comments