|
17 | 17 | from typing import TYPE_CHECKING |
18 | 18 |
|
19 | 19 | if TYPE_CHECKING: |
20 | | - from typing import Optional, Sequence, Union |
| 20 | + from typing import Any, Optional, Sequence, Union |
21 | 21 | from opentelemetry.context import Context |
22 | 22 | from opentelemetry.trace import Link, SpanKind |
23 | 23 | from opentelemetry.trace.span import SpanContext |
@@ -152,15 +152,9 @@ def should_sample( |
152 | 152 | has_traces_sampler = callable(client.options.get("traces_sampler")) |
153 | 153 |
|
154 | 154 | if is_root_span and has_traces_sampler: |
155 | | - sampling_context = { |
156 | | - "transaction_context": { |
157 | | - "name": name, |
158 | | - "op": attributes.get(SentrySpanAttribute.OP), |
159 | | - "source": attributes.get(SentrySpanAttribute.SOURCE), |
160 | | - }, |
161 | | - "parent_sampled": get_parent_sampled(parent_span_context, trace_id), |
162 | | - } |
163 | | - sampling_context.update(attributes) |
| 155 | + sampling_context = create_sampling_context( |
| 156 | + name, attributes, parent_span_context, trace_id |
| 157 | + ) |
164 | 158 | sample_rate = client.options["traces_sampler"](sampling_context) |
165 | 159 | else: |
166 | 160 | # Check if there is a parent with a sampling decision |
@@ -193,3 +187,19 @@ def should_sample( |
193 | 187 |
|
194 | 188 | def get_description(self) -> str: |
195 | 189 | return self.__class__.__name__ |
| 190 | + |
| 191 | + |
| 192 | +def create_sampling_context(name, attributes, parent_span_context, trace_id): |
| 193 | + # type: (str, Attributes, SpanContext, str) -> dict[str, Any] |
| 194 | + sampling_context = { |
| 195 | + "transaction_context": { |
| 196 | + "name": name, |
| 197 | + "op": attributes.get(SentrySpanAttribute.OP), |
| 198 | + "source": attributes.get(SentrySpanAttribute.SOURCE), |
| 199 | + }, |
| 200 | + "parent_sampled": get_parent_sampled(parent_span_context, trace_id), |
| 201 | + } |
| 202 | + |
| 203 | + sampling_context.update(attributes) |
| 204 | + |
| 205 | + return sampling_context |
0 commit comments