sampling.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. # Copyright The OpenTelemetry Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. For general information about sampling, see `the specification <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#sampling>`_.
  16. OpenTelemetry provides two types of samplers:
  17. - `StaticSampler`
  18. - `TraceIdRatioBased`
  19. A `StaticSampler` always returns the same sampling result regardless of the conditions. Both possible StaticSamplers are already created:
  20. - Always sample spans: ALWAYS_ON
  21. - Never sample spans: ALWAYS_OFF
  22. A `TraceIdRatioBased` sampler makes a random sampling result based on the sampling probability given.
  23. If the span being sampled has a parent, `ParentBased` will respect the parent delegate sampler. Otherwise, it returns the sampling result from the given root sampler.
  24. Currently, sampling results are always made during the creation of the span. However, this might not always be the case in the future (see `OTEP #115 <https://github.com/open-telemetry/oteps/pull/115>`_).
  25. Custom samplers can be created by subclassing `Sampler` and implementing `Sampler.should_sample` as well as `Sampler.get_description`.
  26. Samplers are able to modify the `opentelemetry.trace.span.TraceState` of the parent of the span being created. For custom samplers, it is suggested to implement `Sampler.should_sample` to utilize the
  27. parent span context's `opentelemetry.trace.span.TraceState` and pass into the `SamplingResult` instead of the explicit trace_state field passed into the parameter of `Sampler.should_sample`.
  28. To use a sampler, pass it into the tracer provider constructor. For example:
  29. .. code:: python
  30. from opentelemetry import trace
  31. from opentelemetry.sdk.trace import TracerProvider
  32. from opentelemetry.sdk.trace.export import (
  33. ConsoleSpanExporter,
  34. SimpleSpanProcessor,
  35. )
  36. from opentelemetry.sdk.trace.sampling import TraceIdRatioBased
  37. # sample 1 in every 1000 traces
  38. sampler = TraceIdRatioBased(1/1000)
  39. # set the sampler onto the global tracer provider
  40. trace.set_tracer_provider(TracerProvider(sampler=sampler))
  41. # set up an exporter for sampled spans
  42. trace.get_tracer_provider().add_span_processor(
  43. SimpleSpanProcessor(ConsoleSpanExporter())
  44. )
  45. # created spans will now be sampled by the TraceIdRatioBased sampler
  46. with trace.get_tracer(__name__).start_as_current_span("Test Span"):
  47. ...
  48. The tracer sampler can also be configured via environment variables ``OTEL_TRACES_SAMPLER`` and ``OTEL_TRACES_SAMPLER_ARG`` (only if applicable).
  49. The list of built-in values for ``OTEL_TRACES_SAMPLER`` are:
  50. * always_on - Sampler that always samples spans, regardless of the parent span's sampling decision.
  51. * always_off - Sampler that never samples spans, regardless of the parent span's sampling decision.
  52. * traceidratio - Sampler that samples probabilistically based on rate.
  53. * parentbased_always_on - (default) Sampler that respects its parent span's sampling decision, but otherwise always samples.
  54. * parentbased_always_off - Sampler that respects its parent span's sampling decision, but otherwise never samples.
  55. * parentbased_traceidratio - Sampler that respects its parent span's sampling decision, but otherwise samples probabilistically based on rate.
  56. Sampling probability can be set with ``OTEL_TRACES_SAMPLER_ARG`` if the sampler is traceidratio or parentbased_traceidratio. Rate must be in the range [0.0,1.0]. When not provided rate will be set to
  57. 1.0 (maximum rate possible).
  58. Prev example but with environment variables. Please make sure to set the env ``OTEL_TRACES_SAMPLER=traceidratio`` and ``OTEL_TRACES_SAMPLER_ARG=0.001``.
  59. .. code:: python
  60. from opentelemetry import trace
  61. from opentelemetry.sdk.trace import TracerProvider
  62. from opentelemetry.sdk.trace.export import (
  63. ConsoleSpanExporter,
  64. SimpleSpanProcessor,
  65. )
  66. trace.set_tracer_provider(TracerProvider())
  67. # set up an exporter for sampled spans
  68. trace.get_tracer_provider().add_span_processor(
  69. SimpleSpanProcessor(ConsoleSpanExporter())
  70. )
  71. # created spans will now be sampled by the TraceIdRatioBased sampler with rate 1/1000.
  72. with trace.get_tracer(__name__).start_as_current_span("Test Span"):
  73. ...
  74. When utilizing a configurator, you can configure a custom sampler. In order to create a configurable custom sampler, create an entry point for the custom sampler
  75. factory method or function under the entry point group, ``opentelemetry_traces_sampler``. The custom sampler factory method must be of type ``Callable[[str], Sampler]``, taking a single string argument and
  76. returning a Sampler object. The single input will come from the string value of the ``OTEL_TRACES_SAMPLER_ARG`` environment variable. If ``OTEL_TRACES_SAMPLER_ARG`` is not configured, the input will
  77. be an empty string. For example:
  78. .. code:: python
  79. setup(
  80. ...
  81. entry_points={
  82. ...
  83. "opentelemetry_traces_sampler": [
  84. "custom_sampler_name = path.to.sampler.factory.method:CustomSamplerFactory.get_sampler"
  85. ]
  86. }
  87. )
  88. # ...
  89. class CustomRatioSampler(Sampler):
  90. def __init__(rate):
  91. # ...
  92. # ...
  93. class CustomSamplerFactory:
  94. @staticmethod
  95. def get_sampler(sampler_argument):
  96. try:
  97. rate = float(sampler_argument)
  98. return CustomSampler(rate)
  99. except ValueError: # In case argument is empty string.
  100. return CustomSampler(0.5)
  101. In order to configure you application with a custom sampler's entry point, set the ``OTEL_TRACES_SAMPLER`` environment variable to the key name of the entry point. For example, to configured the
  102. above sampler, set ``OTEL_TRACES_SAMPLER=custom_sampler_name`` and ``OTEL_TRACES_SAMPLER_ARG=0.5``.
  103. """
  104. import abc
  105. import enum
  106. import os
  107. from logging import getLogger
  108. from types import MappingProxyType
  109. from typing import Optional, Sequence
  110. # pylint: disable=unused-import
  111. from opentelemetry.context import Context
  112. from opentelemetry.sdk.environment_variables import (
  113. OTEL_TRACES_SAMPLER,
  114. OTEL_TRACES_SAMPLER_ARG,
  115. )
  116. from opentelemetry.trace import Link, SpanKind, get_current_span
  117. from opentelemetry.trace.span import TraceState
  118. from opentelemetry.util.types import Attributes
  119. _logger = getLogger(__name__)
  120. class Decision(enum.Enum):
  121. # IsRecording() == false, span will not be recorded and all events and attributes will be dropped.
  122. DROP = 0
  123. # IsRecording() == true, but Sampled flag MUST NOT be set.
  124. RECORD_ONLY = 1
  125. # IsRecording() == true AND Sampled flag` MUST be set.
  126. RECORD_AND_SAMPLE = 2
  127. def is_recording(self):
  128. return self in (Decision.RECORD_ONLY, Decision.RECORD_AND_SAMPLE)
  129. def is_sampled(self):
  130. return self is Decision.RECORD_AND_SAMPLE
  131. class SamplingResult:
  132. """A sampling result as applied to a newly-created Span.
  133. Args:
  134. decision: A sampling decision based off of whether the span is recorded
  135. and the sampled flag in trace flags in the span context.
  136. attributes: Attributes to add to the `opentelemetry.trace.Span`.
  137. trace_state: The tracestate used for the `opentelemetry.trace.Span`.
  138. Could possibly have been modified by the sampler.
  139. """
  140. def __repr__(self) -> str:
  141. return f"{type(self).__name__}({str(self.decision)}, attributes={str(self.attributes)})"
  142. def __init__(
  143. self,
  144. decision: Decision,
  145. attributes: "Attributes" = None,
  146. trace_state: Optional["TraceState"] = None,
  147. ) -> None:
  148. self.decision = decision
  149. if attributes is None:
  150. self.attributes = MappingProxyType({})
  151. else:
  152. self.attributes = MappingProxyType(attributes)
  153. self.trace_state = trace_state
  154. class Sampler(abc.ABC):
  155. @abc.abstractmethod
  156. def should_sample(
  157. self,
  158. parent_context: Optional["Context"],
  159. trace_id: int,
  160. name: str,
  161. kind: Optional[SpanKind] = None,
  162. attributes: Attributes = None,
  163. links: Optional[Sequence["Link"]] = None,
  164. trace_state: Optional["TraceState"] = None,
  165. ) -> "SamplingResult":
  166. pass
  167. @abc.abstractmethod
  168. def get_description(self) -> str:
  169. pass
  170. class StaticSampler(Sampler):
  171. """Sampler that always returns the same decision."""
  172. def __init__(self, decision: "Decision") -> None:
  173. self._decision = decision
  174. def should_sample(
  175. self,
  176. parent_context: Optional["Context"],
  177. trace_id: int,
  178. name: str,
  179. kind: Optional[SpanKind] = None,
  180. attributes: Attributes = None,
  181. links: Optional[Sequence["Link"]] = None,
  182. trace_state: Optional["TraceState"] = None,
  183. ) -> "SamplingResult":
  184. if self._decision is Decision.DROP:
  185. attributes = None
  186. return SamplingResult(
  187. self._decision,
  188. attributes,
  189. _get_parent_trace_state(parent_context),
  190. )
  191. def get_description(self) -> str:
  192. if self._decision is Decision.DROP:
  193. return "AlwaysOffSampler"
  194. return "AlwaysOnSampler"
  195. ALWAYS_OFF = StaticSampler(Decision.DROP)
  196. """Sampler that never samples spans, regardless of the parent span's sampling decision."""
  197. ALWAYS_ON = StaticSampler(Decision.RECORD_AND_SAMPLE)
  198. """Sampler that always samples spans, regardless of the parent span's sampling decision."""
  199. class TraceIdRatioBased(Sampler):
  200. """
  201. Sampler that makes sampling decisions probabilistically based on `rate`.
  202. Args:
  203. rate: Probability (between 0 and 1) that a span will be sampled
  204. """
  205. def __init__(self, rate: float):
  206. if rate < 0.0 or rate > 1.0:
  207. raise ValueError("Probability must be in range [0.0, 1.0].")
  208. self._rate = rate
  209. self._bound = self.get_bound_for_rate(self._rate)
  210. # For compatibility with 64 bit trace IDs, the sampler checks the 64
  211. # low-order bits of the trace ID to decide whether to sample a given trace.
  212. TRACE_ID_LIMIT = (1 << 64) - 1
  213. @classmethod
  214. def get_bound_for_rate(cls, rate: float) -> int:
  215. return round(rate * (cls.TRACE_ID_LIMIT + 1))
  216. @property
  217. def rate(self) -> float:
  218. return self._rate
  219. @property
  220. def bound(self) -> int:
  221. return self._bound
  222. def should_sample(
  223. self,
  224. parent_context: Optional["Context"],
  225. trace_id: int,
  226. name: str,
  227. kind: Optional[SpanKind] = None,
  228. attributes: Attributes = None,
  229. links: Optional[Sequence["Link"]] = None,
  230. trace_state: Optional["TraceState"] = None,
  231. ) -> "SamplingResult":
  232. decision = Decision.DROP
  233. if trace_id & self.TRACE_ID_LIMIT < self.bound:
  234. decision = Decision.RECORD_AND_SAMPLE
  235. if decision is Decision.DROP:
  236. attributes = None
  237. return SamplingResult(
  238. decision,
  239. attributes,
  240. _get_parent_trace_state(parent_context),
  241. )
  242. def get_description(self) -> str:
  243. return f"TraceIdRatioBased{{{self._rate}}}"
  244. class ParentBased(Sampler):
  245. """
  246. If a parent is set, applies the respective delegate sampler.
  247. Otherwise, uses the root provided at initialization to make a
  248. decision.
  249. Args:
  250. root: Sampler called for spans with no parent (root spans).
  251. remote_parent_sampled: Sampler called for a remote sampled parent.
  252. remote_parent_not_sampled: Sampler called for a remote parent that is
  253. not sampled.
  254. local_parent_sampled: Sampler called for a local sampled parent.
  255. local_parent_not_sampled: Sampler called for a local parent that is
  256. not sampled.
  257. """
  258. def __init__(
  259. self,
  260. root: Sampler,
  261. remote_parent_sampled: Sampler = ALWAYS_ON,
  262. remote_parent_not_sampled: Sampler = ALWAYS_OFF,
  263. local_parent_sampled: Sampler = ALWAYS_ON,
  264. local_parent_not_sampled: Sampler = ALWAYS_OFF,
  265. ):
  266. self._root = root
  267. self._remote_parent_sampled = remote_parent_sampled
  268. self._remote_parent_not_sampled = remote_parent_not_sampled
  269. self._local_parent_sampled = local_parent_sampled
  270. self._local_parent_not_sampled = local_parent_not_sampled
  271. def should_sample(
  272. self,
  273. parent_context: Optional["Context"],
  274. trace_id: int,
  275. name: str,
  276. kind: Optional[SpanKind] = None,
  277. attributes: Attributes = None,
  278. links: Optional[Sequence["Link"]] = None,
  279. trace_state: Optional["TraceState"] = None,
  280. ) -> "SamplingResult":
  281. parent_span_context = get_current_span(
  282. parent_context
  283. ).get_span_context()
  284. # default to the root sampler
  285. sampler = self._root
  286. # respect the sampling and remote flag of the parent if present
  287. if parent_span_context is not None and parent_span_context.is_valid:
  288. if parent_span_context.is_remote:
  289. if parent_span_context.trace_flags.sampled:
  290. sampler = self._remote_parent_sampled
  291. else:
  292. sampler = self._remote_parent_not_sampled
  293. else:
  294. if parent_span_context.trace_flags.sampled:
  295. sampler = self._local_parent_sampled
  296. else:
  297. sampler = self._local_parent_not_sampled
  298. return sampler.should_sample(
  299. parent_context=parent_context,
  300. trace_id=trace_id,
  301. name=name,
  302. kind=kind,
  303. attributes=attributes,
  304. links=links,
  305. )
  306. def get_description(self):
  307. return f"ParentBased{{root:{self._root.get_description()},remoteParentSampled:{self._remote_parent_sampled.get_description()},remoteParentNotSampled:{self._remote_parent_not_sampled.get_description()},localParentSampled:{self._local_parent_sampled.get_description()},localParentNotSampled:{self._local_parent_not_sampled.get_description()}}}"
  308. DEFAULT_OFF = ParentBased(ALWAYS_OFF)
  309. """Sampler that respects its parent span's sampling decision, but otherwise never samples."""
  310. DEFAULT_ON = ParentBased(ALWAYS_ON)
  311. """Sampler that respects its parent span's sampling decision, but otherwise always samples."""
  312. class ParentBasedTraceIdRatio(ParentBased):
  313. """
  314. Sampler that respects its parent span's sampling decision, but otherwise
  315. samples probabilistically based on `rate`.
  316. """
  317. def __init__(self, rate: float):
  318. root = TraceIdRatioBased(rate=rate)
  319. super().__init__(root=root)
  320. class _AlwaysOff(StaticSampler):
  321. def __init__(self, _):
  322. super().__init__(Decision.DROP)
  323. class _AlwaysOn(StaticSampler):
  324. def __init__(self, _):
  325. super().__init__(Decision.RECORD_AND_SAMPLE)
  326. class _ParentBasedAlwaysOff(ParentBased):
  327. def __init__(self, _):
  328. super().__init__(ALWAYS_OFF)
  329. class _ParentBasedAlwaysOn(ParentBased):
  330. def __init__(self, _):
  331. super().__init__(ALWAYS_ON)
  332. _KNOWN_SAMPLERS = {
  333. "always_on": ALWAYS_ON,
  334. "always_off": ALWAYS_OFF,
  335. "parentbased_always_on": DEFAULT_ON,
  336. "parentbased_always_off": DEFAULT_OFF,
  337. "traceidratio": TraceIdRatioBased,
  338. "parentbased_traceidratio": ParentBasedTraceIdRatio,
  339. }
  340. def _get_from_env_or_default() -> Sampler:
  341. trace_sampler = os.getenv(
  342. OTEL_TRACES_SAMPLER, "parentbased_always_on"
  343. ).lower()
  344. if trace_sampler not in _KNOWN_SAMPLERS:
  345. _logger.warning("Couldn't recognize sampler %s.", trace_sampler)
  346. trace_sampler = "parentbased_always_on"
  347. if trace_sampler in ("traceidratio", "parentbased_traceidratio"):
  348. try:
  349. rate = float(os.getenv(OTEL_TRACES_SAMPLER_ARG))
  350. except (ValueError, TypeError):
  351. _logger.warning("Could not convert TRACES_SAMPLER_ARG to float.")
  352. rate = 1.0
  353. return _KNOWN_SAMPLERS[trace_sampler](rate)
  354. return _KNOWN_SAMPLERS[trace_sampler]
  355. def _get_parent_trace_state(
  356. parent_context: Optional[Context],
  357. ) -> Optional["TraceState"]:
  358. parent_span_context = get_current_span(parent_context).get_span_context()
  359. if parent_span_context is None or not parent_span_context.is_valid:
  360. return None
  361. return parent_span_context.trace_state