14
14
except ImportError :
15
15
from typing_extensions import TypedDict
16
16
17
- from ddtrace ._trace .sampling_rule import SamplingRule # noqa:F401
18
- from ddtrace .constants import _SAMPLING_AGENT_DECISION
19
- from ddtrace .constants import _SAMPLING_RULE_DECISION
20
- from ddtrace .constants import _SINGLE_SPAN_SAMPLING_MAX_PER_SEC
21
17
from ddtrace .constants import _SINGLE_SPAN_SAMPLING_MAX_PER_SEC_NO_LIMIT
22
- from ddtrace .constants import _SINGLE_SPAN_SAMPLING_MECHANISM
23
- from ddtrace .constants import _SINGLE_SPAN_SAMPLING_RATE
24
- from ddtrace .internal .constants import _KEEP_PRIORITY_INDEX
25
- from ddtrace .internal .constants import _REJECT_PRIORITY_INDEX
26
18
from ddtrace .internal .constants import SAMPLING_DECISION_TRACE_TAG_KEY
27
- from ddtrace .internal .constants import SAMPLING_MECHANISM_TO_PRIORITIES
28
- from ddtrace .internal .constants import SamplingMechanism
29
19
from ddtrace .internal .glob_matching import GlobMatcher
30
20
from ddtrace .internal .logger import get_logger
31
21
from ddtrace .settings ._config import config
43
33
44
34
if TYPE_CHECKING : # pragma: no cover
45
35
from ddtrace ._trace .context import Context # noqa:F401
46
- from ddtrace ._trace .span import Span # noqa:F401
47
36
48
37
# Big prime number to make hashing better distributed
49
38
KNUTH_FACTOR = 1111111111111111111
@@ -126,28 +115,16 @@ def __init__(
126
115
self ._service_matcher = GlobMatcher (service ) if service is not None else None
127
116
self ._name_matcher = GlobMatcher (name ) if name is not None else None
128
117
129
- def sample (self , span ):
130
- # type: (Span) -> bool
131
- if self ._sample (span ):
132
- if self ._limiter .is_allowed ():
133
- self .apply_span_sampling_tags (span )
134
- return True
135
- return False
136
-
137
- def _sample (self , span ):
138
- # type: (Span) -> bool
118
+ def _sample (self , span_id : int ) -> bool :
139
119
if self ._sample_rate == 1 :
140
120
return True
141
121
elif self ._sample_rate == 0 :
142
122
return False
143
123
144
- return ((span . span_id * KNUTH_FACTOR ) % MAX_SPAN_ID ) <= self ._sampling_id_threshold
124
+ return ((span_id * KNUTH_FACTOR ) % MAX_SPAN_ID ) <= self ._sampling_id_threshold
145
125
146
- def match (self , span ):
147
- # type: (Span) -> bool
126
+ def match (self , name : Optional [str ], service : Optional [str ]) -> bool :
148
127
"""Determines if the span's service and name match the configured patterns"""
149
- name = span .name
150
- service = span .service
151
128
# If a span lacks a name and service, we can't match on it
152
129
if service is None and name is None :
153
130
return False
@@ -169,14 +146,6 @@ def match(self, span):
169
146
name_match = self ._name_matcher .match (name )
170
147
return service_match and name_match
171
148
172
- def apply_span_sampling_tags (self , span ):
173
- # type: (Span) -> None
174
- span .set_metric (_SINGLE_SPAN_SAMPLING_MECHANISM , SamplingMechanism .SPAN_SAMPLING_RULE )
175
- span .set_metric (_SINGLE_SPAN_SAMPLING_RATE , self ._sample_rate )
176
- # Only set this tag if it's not the default -1
177
- if self ._max_per_second != _SINGLE_SPAN_SAMPLING_MAX_PER_SEC_NO_LIMIT :
178
- span .set_metric (_SINGLE_SPAN_SAMPLING_MAX_PER_SEC , self ._max_per_second )
179
-
180
149
181
150
def get_span_sampling_rules () -> List [SpanSamplingRule ]:
182
151
json_rules = _get_span_sampling_json ()
@@ -258,38 +227,3 @@ def _check_unsupported_pattern(string: str) -> None:
258
227
for char in string :
259
228
if char in unsupported_chars and config ._raise :
260
229
raise ValueError ("Unsupported Glob pattern found, character:%r is not supported" % char )
261
-
262
-
263
- def is_single_span_sampled (span ):
264
- # type: (Span) -> bool
265
- return span .get_metric (_SINGLE_SPAN_SAMPLING_MECHANISM ) == SamplingMechanism .SPAN_SAMPLING_RULE
266
-
267
-
268
- def _set_sampling_tags (span , sampled , sample_rate , mechanism ):
269
- # type: (Span, bool, float, int) -> None
270
- # Set the sampling mechanism
271
- set_sampling_decision_maker (span .context , mechanism )
272
- # Set the sampling psr rate
273
- if mechanism in (
274
- SamplingMechanism .LOCAL_USER_TRACE_SAMPLING_RULE ,
275
- SamplingMechanism .REMOTE_USER_TRACE_SAMPLING_RULE ,
276
- SamplingMechanism .REMOTE_DYNAMIC_TRACE_SAMPLING_RULE ,
277
- ):
278
- span .set_metric (_SAMPLING_RULE_DECISION , sample_rate )
279
- elif mechanism == SamplingMechanism .AGENT_RATE_BY_SERVICE :
280
- span .set_metric (_SAMPLING_AGENT_DECISION , sample_rate )
281
- # Set the sampling priority
282
- priorities = SAMPLING_MECHANISM_TO_PRIORITIES [mechanism ]
283
- priority_index = _KEEP_PRIORITY_INDEX if sampled else _REJECT_PRIORITY_INDEX
284
- span .context .sampling_priority = priorities [priority_index ]
285
-
286
-
287
- def _get_highest_precedence_rule_matching (span , rules ):
288
- # type: (Span, List[SamplingRule]) -> Optional[SamplingRule]
289
- if not rules :
290
- return None
291
-
292
- for rule in rules :
293
- if rule .matches (span ):
294
- return rule
295
- return None
0 commit comments