|
| 1 | +# Copyright 2019, 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 | +import abc |
| 16 | +from typing import Dict, Mapping, Optional, Sequence |
| 17 | + |
| 18 | +# pylint: disable=unused-import |
| 19 | +from opentelemetry.trace import Link, SpanContext |
| 20 | +from opentelemetry.util.types import AttributeValue |
| 21 | + |
| 22 | + |
| 23 | +class Decision: |
| 24 | + """A sampling decision as applied to a newly-created Span. |
| 25 | +
|
| 26 | + Args: |
| 27 | + sampled: Whether the `Span` should be sampled. |
| 28 | + attributes: Attributes to add to the `Span`. |
| 29 | + """ |
| 30 | + |
| 31 | + def __repr__(self) -> str: |
| 32 | + return "{}({}, attributes={})".format( |
| 33 | + type(self).__name__, str(self.sampled), str(self.attributes) |
| 34 | + ) |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + sampled: bool = False, |
| 39 | + attributes: Mapping[str, "AttributeValue"] = None, |
| 40 | + ) -> None: |
| 41 | + self.sampled = sampled # type: bool |
| 42 | + if attributes is None: |
| 43 | + self.attributes = {} # type: Dict[str, "AttributeValue"] |
| 44 | + else: |
| 45 | + self.attributes = dict(attributes) |
| 46 | + |
| 47 | + |
| 48 | +class Sampler(abc.ABC): |
| 49 | + @abc.abstractmethod |
| 50 | + def should_sample( |
| 51 | + self, |
| 52 | + parent_context: Optional["SpanContext"], |
| 53 | + trace_id: int, |
| 54 | + span_id: int, |
| 55 | + name: str, |
| 56 | + links: Sequence["Link"] = (), |
| 57 | + ) -> "Decision": |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +class StaticSampler(Sampler): |
| 62 | + """Sampler that always returns the same decision.""" |
| 63 | + |
| 64 | + def __init__(self, decision: "Decision"): |
| 65 | + self._decision = decision |
| 66 | + |
| 67 | + def should_sample( |
| 68 | + self, |
| 69 | + parent_context: Optional["SpanContext"], |
| 70 | + trace_id: int, |
| 71 | + span_id: int, |
| 72 | + name: str, |
| 73 | + links: Sequence["Link"] = (), |
| 74 | + ) -> "Decision": |
| 75 | + return self._decision |
| 76 | + |
| 77 | + |
| 78 | +class ProbabilitySampler(Sampler): |
| 79 | + def __init__(self, rate: float): |
| 80 | + self._rate = rate |
| 81 | + self._bound = self.get_bound_for_rate(self._rate) |
| 82 | + |
| 83 | + # The sampler checks the last 8 bytes of the trace ID to decide whether to |
| 84 | + # sample a given trace. |
| 85 | + CHECK_BYTES = 0xFFFFFFFFFFFFFFFF |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def get_bound_for_rate(cls, rate: float) -> int: |
| 89 | + return round(rate * (cls.CHECK_BYTES + 1)) |
| 90 | + |
| 91 | + @property |
| 92 | + def rate(self) -> float: |
| 93 | + return self._rate |
| 94 | + |
| 95 | + @rate.setter |
| 96 | + def rate(self, new_rate: float) -> None: |
| 97 | + self._rate = new_rate |
| 98 | + self._bound = self.get_bound_for_rate(self._rate) |
| 99 | + |
| 100 | + @property |
| 101 | + def bound(self) -> int: |
| 102 | + return self._bound |
| 103 | + |
| 104 | + def should_sample( |
| 105 | + self, |
| 106 | + parent_context: Optional["SpanContext"], |
| 107 | + trace_id: int, |
| 108 | + span_id: int, |
| 109 | + name: str, |
| 110 | + links: Sequence["Link"] = (), |
| 111 | + ) -> "Decision": |
| 112 | + if parent_context is not None: |
| 113 | + return Decision(parent_context.trace_options.sampled) |
| 114 | + |
| 115 | + return Decision(trace_id & self.CHECK_BYTES < self.bound) |
| 116 | + |
| 117 | + |
| 118 | +# Samplers that ignore the parent sampling decision and never/always sample. |
| 119 | +ALWAYS_OFF = StaticSampler(Decision(False)) |
| 120 | +ALWAYS_ON = StaticSampler(Decision(True)) |
| 121 | + |
| 122 | +# Samplers that respect the parent sampling decision, but otherwise |
| 123 | +# never/always sample. |
| 124 | +DEFAULT_OFF = ProbabilitySampler(0.0) |
| 125 | +DEFAULT_ON = ProbabilitySampler(1.0) |
0 commit comments