|
| 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 | +from abc import ABC, abstractmethod |
| 16 | +from typing import Union |
| 17 | + |
| 18 | +from opentelemetry import trace |
| 19 | +from opentelemetry.context import Context |
| 20 | +from opentelemetry.trace.span import INVALID_SPAN |
| 21 | +from opentelemetry.util.types import Attributes |
| 22 | + |
| 23 | + |
| 24 | +class ExemplarFilter(ABC): |
| 25 | + """``ExemplarFilter`` determines which measurements are eligible for becoming an |
| 26 | + ``Exemplar``. |
| 27 | +
|
| 28 | + Exemplar filters are used to filter measurements before attempting to store them |
| 29 | + in a reservoir. |
| 30 | +
|
| 31 | + Reference: |
| 32 | + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#exemplarfilter |
| 33 | + """ |
| 34 | + |
| 35 | + @abstractmethod |
| 36 | + def should_sample( |
| 37 | + self, |
| 38 | + value: Union[int, float], |
| 39 | + time_unix_nano: int, |
| 40 | + attributes: Attributes, |
| 41 | + ctx: Context, |
| 42 | + ) -> bool: |
| 43 | + """Returns whether or not a reservoir should attempt to filter a measurement. |
| 44 | +
|
| 45 | + Attributes: |
| 46 | + value: The value of the measurement |
| 47 | + timestamp: A timestamp that best represents when the measurement was taken |
| 48 | + attributes: The complete set of measurement attributes |
| 49 | + ctx: The Context of the measurement |
| 50 | + """ |
| 51 | + raise NotImplementedError("ExemplarFilter.should_sample is not implemented") |
| 52 | + |
| 53 | + |
| 54 | +class AlwaysOnExemplarFilter(ExemplarFilter): |
| 55 | + """An ExemplarFilter which makes all measurements eligible for being an Exemplar. |
| 56 | + |
| 57 | + Reference: |
| 58 | + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#alwayson |
| 59 | + """ |
| 60 | + |
| 61 | + def should_sample( |
| 62 | + self, |
| 63 | + value: Union[int, float], |
| 64 | + time_unix_nano: int, |
| 65 | + attributes: Attributes, |
| 66 | + ctx: Context, |
| 67 | + ) -> bool: |
| 68 | + """Returns whether or not a reservoir should attempt to filter a measurement. |
| 69 | +
|
| 70 | + Attributes: |
| 71 | + value: The value of the measurement |
| 72 | + timestamp: A timestamp that best represents when the measurement was taken |
| 73 | + attributes: The complete set of measurement attributes |
| 74 | + ctx: The Context of the measurement |
| 75 | + """ |
| 76 | + return True |
| 77 | + |
| 78 | + |
| 79 | +class AlwaysOffExemplarFilter(ExemplarFilter): |
| 80 | + """An ExemplarFilter which makes no measurements eligible for being an Exemplar. |
| 81 | + |
| 82 | + Using this ExemplarFilter is as good as disabling Exemplar feature. |
| 83 | + |
| 84 | + Reference: |
| 85 | + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#alwaysoff |
| 86 | + """ |
| 87 | + |
| 88 | + def should_sample( |
| 89 | + self, |
| 90 | + value: Union[int, float], |
| 91 | + time_unix_nano: int, |
| 92 | + attributes: Attributes, |
| 93 | + ctx: Context, |
| 94 | + ) -> bool: |
| 95 | + """Returns whether or not a reservoir should attempt to filter a measurement. |
| 96 | +
|
| 97 | + Attributes: |
| 98 | + value: The value of the measurement |
| 99 | + timestamp: A timestamp that best represents when the measurement was taken |
| 100 | + attributes: The complete set of measurement attributes |
| 101 | + ctx: The Context of the measurement |
| 102 | + """ |
| 103 | + return False |
| 104 | + |
| 105 | + |
| 106 | +class TraceBasedExemplarFilter(ExemplarFilter): |
| 107 | + """An ExemplarFilter which makes those measurements eligible for being an Exemplar, |
| 108 | + which are recorded in the context of a sampled parent span. |
| 109 | + |
| 110 | + Reference: |
| 111 | + https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#tracebased |
| 112 | + """ |
| 113 | + |
| 114 | + def should_sample( |
| 115 | + self, |
| 116 | + value: Union[int, float], |
| 117 | + time_unix_nano: int, |
| 118 | + attributes: Attributes, |
| 119 | + ctx: Context, |
| 120 | + ) -> bool: |
| 121 | + """Returns whether or not a reservoir should attempt to filter a measurement. |
| 122 | +
|
| 123 | + Attributes: |
| 124 | + value: The value of the measurement |
| 125 | + timestamp: A timestamp that best represents when the measurement was taken |
| 126 | + attributes: The complete set of measurement attributes |
| 127 | + ctx: The Context of the measurement |
| 128 | + """ |
| 129 | + span = trace.get_current_span(ctx) |
| 130 | + if span == INVALID_SPAN: |
| 131 | + return False |
| 132 | + return span.get_span_context().trace_flags.sampled |
0 commit comments