|
208 | 208 | end |
209 | 209 |
|
210 | 210 | describe ".start_transaction" do |
| 211 | + describe "sampler example" do |
| 212 | + before do |
| 213 | + perform_basic_setup do |config| |
| 214 | + config.traces_sampler = lambda do |sampling_context| |
| 215 | + # if this is the continuation of a trace, just use that decision (rate controlled by the caller) |
| 216 | + unless sampling_context[:parent_sampled].nil? |
| 217 | + next sampling_context[:parent_sampled] |
| 218 | + end |
| 219 | + |
| 220 | + # transaction_context is the transaction object in hash form |
| 221 | + # keep in mind that sampling happens right after the transaction is initialized |
| 222 | + # e.g. at the beginning of the request |
| 223 | + transaction_context = sampling_context[:transaction_context] |
| 224 | + |
| 225 | + # transaction_context helps you sample transactions with more sophistication |
| 226 | + # for example, you can provide different sample rates based on the operation or name |
| 227 | + op = transaction_context[:op] |
| 228 | + transaction_name = transaction_context[:name] |
| 229 | + |
| 230 | + case op |
| 231 | + when /request/ |
| 232 | + case transaction_name |
| 233 | + when /health_check/ |
| 234 | + 0.0 |
| 235 | + when /payment/ |
| 236 | + 0.5 |
| 237 | + when /api/ |
| 238 | + 0.2 |
| 239 | + else |
| 240 | + 0.1 |
| 241 | + end |
| 242 | + when /sidekiq/ |
| 243 | + 0.01 # you may want to set a lower rate for background jobs if the number is large |
| 244 | + else |
| 245 | + 0.0 # ignore all other transactions |
| 246 | + end |
| 247 | + end |
| 248 | + end |
| 249 | + end |
| 250 | + |
| 251 | + it "prioritizes parent's sampling decision" do |
| 252 | + sampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-1" |
| 253 | + unsampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-0" |
| 254 | + not_sampled_trace = "d298e6b033f84659928a2267c3879aaa-2a35b8e9a1b974f4-" |
| 255 | + |
| 256 | + transaction = Sentry::Transaction.from_sentry_trace(sampled_trace, op: "rack.request", name: "/payment") |
| 257 | + described_class.start_transaction(transaction: transaction) |
| 258 | + |
| 259 | + expect(transaction.sampled).to eq(true) |
| 260 | + |
| 261 | + transaction = Sentry::Transaction.from_sentry_trace(unsampled_trace, op: "rack.request", name: "/payment") |
| 262 | + described_class.start_transaction(transaction: transaction) |
| 263 | + |
| 264 | + expect(transaction.sampled).to eq(false) |
| 265 | + |
| 266 | + allow(Random).to receive(:rand).and_return(0.4) |
| 267 | + transaction = Sentry::Transaction.from_sentry_trace(not_sampled_trace, op: "rack.request", name: "/payment") |
| 268 | + described_class.start_transaction(transaction: transaction) |
| 269 | + |
| 270 | + expect(transaction.sampled).to eq(true) |
| 271 | + end |
| 272 | + |
| 273 | + it "skips /health_check" do |
| 274 | + transaction = described_class.start_transaction(op: "rack.request", name: "/health_check") |
| 275 | + expect(transaction.sampled).to eq(false) |
| 276 | + end |
| 277 | + |
| 278 | + it "gives /payment 0.5 of rate" do |
| 279 | + allow(Random).to receive(:rand).and_return(0.4) |
| 280 | + transaction = described_class.start_transaction(op: "rack.request", name: "/payment") |
| 281 | + expect(transaction.sampled).to eq(true) |
| 282 | + |
| 283 | + allow(Random).to receive(:rand).and_return(0.6) |
| 284 | + transaction = described_class.start_transaction(op: "rack.request", name: "/payment") |
| 285 | + expect(transaction.sampled).to eq(false) |
| 286 | + end |
| 287 | + |
| 288 | + it "gives /api 0.2 of rate" do |
| 289 | + allow(Random).to receive(:rand).and_return(0.1) |
| 290 | + transaction = described_class.start_transaction(op: "rack.request", name: "/api") |
| 291 | + expect(transaction.sampled).to eq(true) |
| 292 | + |
| 293 | + allow(Random).to receive(:rand).and_return(0.3) |
| 294 | + transaction = described_class.start_transaction(op: "rack.request", name: "/api") |
| 295 | + expect(transaction.sampled).to eq(false) |
| 296 | + end |
| 297 | + |
| 298 | + it "gives other paths 0.1 of rate" do |
| 299 | + allow(Random).to receive(:rand).and_return(0.05) |
| 300 | + transaction = described_class.start_transaction(op: "rack.request", name: "/orders") |
| 301 | + expect(transaction.sampled).to eq(true) |
| 302 | + |
| 303 | + allow(Random).to receive(:rand).and_return(0.2) |
| 304 | + transaction = described_class.start_transaction(op: "rack.request", name: "/orders") |
| 305 | + expect(transaction.sampled).to eq(false) |
| 306 | + end |
| 307 | + |
| 308 | + it "gives sidekiq ops 0.01 of rate" do |
| 309 | + allow(Random).to receive(:rand).and_return(0.005) |
| 310 | + transaction = described_class.start_transaction(op: "sidekiq") |
| 311 | + expect(transaction.sampled).to eq(true) |
| 312 | + |
| 313 | + allow(Random).to receive(:rand).and_return(0.02) |
| 314 | + transaction = described_class.start_transaction(op: "sidekiq") |
| 315 | + expect(transaction.sampled).to eq(false) |
| 316 | + end |
| 317 | + end |
| 318 | + |
211 | 319 | context "when tracing is enabled" do |
212 | 320 | before do |
213 | 321 | Sentry.configuration.traces_sample_rate = 1.0 |
|
0 commit comments