|
| 1 | +defmodule Sentry.Opentelemetry.SamplingContextTest do |
| 2 | + use Sentry.Case, async: true |
| 3 | + |
| 4 | + alias SamplingContext |
| 5 | + |
| 6 | + describe "Access functions" do |
| 7 | + test "fetch/2 returns {:ok, value} for existing keys" do |
| 8 | + transaction_context = %{ |
| 9 | + name: "GET /users", |
| 10 | + op: "http.server", |
| 11 | + trace_id: 123, |
| 12 | + attributes: %{"http.method" => "GET"} |
| 13 | + } |
| 14 | + |
| 15 | + sampling_context = %SamplingContext{ |
| 16 | + transaction_context: transaction_context, |
| 17 | + parent_sampled: true |
| 18 | + } |
| 19 | + |
| 20 | + assert {:ok, ^transaction_context} = |
| 21 | + SamplingContext.fetch(sampling_context, :transaction_context) |
| 22 | + |
| 23 | + assert {:ok, true} = SamplingContext.fetch(sampling_context, :parent_sampled) |
| 24 | + end |
| 25 | + |
| 26 | + test "fetch/2 returns :error for non-existing keys" do |
| 27 | + sampling_context = %SamplingContext{ |
| 28 | + transaction_context: %{name: "test", op: "test", trace_id: 123, attributes: %{}}, |
| 29 | + parent_sampled: nil |
| 30 | + } |
| 31 | + |
| 32 | + assert :error = SamplingContext.fetch(sampling_context, :non_existing_key) |
| 33 | + assert :error = SamplingContext.fetch(sampling_context, :invalid) |
| 34 | + end |
| 35 | + |
| 36 | + test "get_and_update/3 updates existing keys" do |
| 37 | + transaction_context = %{ |
| 38 | + name: "GET /users", |
| 39 | + op: "http.server", |
| 40 | + trace_id: 123, |
| 41 | + attributes: %{"http.method" => "GET"} |
| 42 | + } |
| 43 | + |
| 44 | + sampling_context = %SamplingContext{ |
| 45 | + transaction_context: transaction_context, |
| 46 | + parent_sampled: false |
| 47 | + } |
| 48 | + |
| 49 | + update_fun = fn current_value -> |
| 50 | + {current_value, !current_value} |
| 51 | + end |
| 52 | + |
| 53 | + {old_value, updated_context} = |
| 54 | + SamplingContext.get_and_update(sampling_context, :parent_sampled, update_fun) |
| 55 | + |
| 56 | + assert old_value == false |
| 57 | + assert updated_context.parent_sampled == true |
| 58 | + assert updated_context.transaction_context == transaction_context |
| 59 | + end |
| 60 | + |
| 61 | + test "get_and_update/3 handles :pop operation" do |
| 62 | + sampling_context = %SamplingContext{ |
| 63 | + transaction_context: %{name: "test", op: "test", trace_id: 123, attributes: %{}}, |
| 64 | + parent_sampled: true |
| 65 | + } |
| 66 | + |
| 67 | + pop_fun = fn _current_value -> :pop end |
| 68 | + |
| 69 | + {old_value, updated_context} = |
| 70 | + SamplingContext.get_and_update(sampling_context, :parent_sampled, pop_fun) |
| 71 | + |
| 72 | + assert old_value == true |
| 73 | + refute Map.has_key?(updated_context, :parent_sampled) |
| 74 | + end |
| 75 | + |
| 76 | + test "get_and_update/3 works with non-existing keys" do |
| 77 | + sampling_context = %SamplingContext{ |
| 78 | + transaction_context: %{name: "test", op: "test", trace_id: 123, attributes: %{}}, |
| 79 | + parent_sampled: nil |
| 80 | + } |
| 81 | + |
| 82 | + update_fun = fn current_value -> |
| 83 | + {current_value, "new_value"} |
| 84 | + end |
| 85 | + |
| 86 | + {old_value, updated_context} = |
| 87 | + SamplingContext.get_and_update(sampling_context, :new_key, update_fun) |
| 88 | + |
| 89 | + assert old_value == nil |
| 90 | + assert Map.get(updated_context, :new_key) == "new_value" |
| 91 | + end |
| 92 | + |
| 93 | + test "pop/2 removes existing keys and returns value" do |
| 94 | + transaction_context = %{ |
| 95 | + name: "POST /api", |
| 96 | + op: "http.server", |
| 97 | + trace_id: 456, |
| 98 | + attributes: %{"http.method" => "POST"} |
| 99 | + } |
| 100 | + |
| 101 | + sampling_context = %SamplingContext{ |
| 102 | + transaction_context: transaction_context, |
| 103 | + parent_sampled: true |
| 104 | + } |
| 105 | + |
| 106 | + {popped_value, updated_context} = SamplingContext.pop(sampling_context, :parent_sampled) |
| 107 | + |
| 108 | + assert popped_value == true |
| 109 | + refute Map.has_key?(updated_context, :parent_sampled) |
| 110 | + assert updated_context.transaction_context == transaction_context |
| 111 | + end |
| 112 | + |
| 113 | + test "pop/2 returns nil for non-existing keys" do |
| 114 | + sampling_context = %SamplingContext{ |
| 115 | + transaction_context: %{name: "test", op: "test", trace_id: 123, attributes: %{}}, |
| 116 | + parent_sampled: nil |
| 117 | + } |
| 118 | + |
| 119 | + {popped_value, updated_context} = SamplingContext.pop(sampling_context, :non_existing_key) |
| 120 | + |
| 121 | + assert popped_value == nil |
| 122 | + assert updated_context == sampling_context |
| 123 | + end |
| 124 | + |
| 125 | + test "Access behavior works with bracket notation" do |
| 126 | + transaction_context = %{ |
| 127 | + name: "DELETE /resource", |
| 128 | + op: "http.server", |
| 129 | + trace_id: 789, |
| 130 | + attributes: %{"http.method" => "DELETE"} |
| 131 | + } |
| 132 | + |
| 133 | + sampling_context = %SamplingContext{ |
| 134 | + transaction_context: transaction_context, |
| 135 | + parent_sampled: false |
| 136 | + } |
| 137 | + |
| 138 | + # Test bracket access for reading |
| 139 | + assert sampling_context[:transaction_context] == transaction_context |
| 140 | + assert sampling_context[:parent_sampled] == false |
| 141 | + assert sampling_context[:non_existing] == nil |
| 142 | + |
| 143 | + # Test get_in/2 |
| 144 | + assert get_in(sampling_context, [:transaction_context, :name]) == "DELETE /resource" |
| 145 | + |
| 146 | + assert get_in(sampling_context, [:transaction_context, :attributes, "http.method"]) == |
| 147 | + "DELETE" |
| 148 | + end |
| 149 | + |
| 150 | + test "Access behavior works with put_in/3" do |
| 151 | + sampling_context = %SamplingContext{ |
| 152 | + transaction_context: %{name: "test", op: "test", trace_id: 123, attributes: %{}}, |
| 153 | + parent_sampled: nil |
| 154 | + } |
| 155 | + |
| 156 | + updated_context = put_in(sampling_context[:parent_sampled], true) |
| 157 | + |
| 158 | + assert updated_context.parent_sampled == true |
| 159 | + assert updated_context.transaction_context == sampling_context.transaction_context |
| 160 | + end |
| 161 | + |
| 162 | + test "Access behavior works with update_in/3" do |
| 163 | + transaction_context = %{ |
| 164 | + name: "PUT /update", |
| 165 | + op: "http.server", |
| 166 | + trace_id: 999, |
| 167 | + attributes: %{"http.method" => "PUT", "http.status_code" => 200} |
| 168 | + } |
| 169 | + |
| 170 | + sampling_context = %SamplingContext{ |
| 171 | + transaction_context: transaction_context, |
| 172 | + parent_sampled: false |
| 173 | + } |
| 174 | + |
| 175 | + updated_context = |
| 176 | + update_in(sampling_context[:transaction_context][:attributes], fn attrs -> |
| 177 | + Map.put(attrs, "http.status_code", 404) |
| 178 | + end) |
| 179 | + |
| 180 | + assert get_in(updated_context, [:transaction_context, :attributes, "http.status_code"]) == |
| 181 | + 404 |
| 182 | + |
| 183 | + assert get_in(updated_context, [:transaction_context, :attributes, "http.method"]) == "PUT" |
| 184 | + assert updated_context.parent_sampled == false |
| 185 | + end |
| 186 | + end |
| 187 | +end |
0 commit comments