Skip to content

Commit 9935e63

Browse files
committed
Add based classes
1 parent f639606 commit 9935e63

File tree

4 files changed

+449
-0
lines changed

4 files changed

+449
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 .exemplar import Exemplar
16+
from .exemplar_filter import (
17+
AlwaysOffExemplarFilter,
18+
AlwaysOnExemplarFilter,
19+
TraceBasedExemplarFilter,
20+
)
21+
from .exemplar_reservoir import (
22+
AlignedHistogramBucketExemplarReservoir,
23+
ExemplarReservoir,
24+
SimpleFixedSizeExemplarReservoir,
25+
)
26+
27+
__all__ = [
28+
"Exemplar",
29+
"AlwaysOffExemplarFilter",
30+
"AlwaysOnExemplarFilter",
31+
"TraceBasedExemplarFilter",
32+
"AlignedHistogramBucketExemplarReservoir",
33+
"ExemplarReservoir",
34+
"SimpleFixedSizeExemplarReservoir",
35+
]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import dataclasses
16+
from typing import Optional, Union
17+
18+
from opentelemetry.util.types import Attributes
19+
20+
21+
@dataclasses.dataclass(frozen=True)
22+
class Exemplar:
23+
"""A representation of an exemplar, which is a sample input measurement.
24+
25+
Exemplars also hold information about the environment when the measurement
26+
was recorded, for example the span and trace ID of the active span when the
27+
exemplar was recorded.
28+
29+
Attributes:
30+
trace_id: (optional) The trace associated with a recording
31+
span_id: (optional) The span associated with a recording
32+
time_unix_nano: The time of the observation
33+
value: The recorded value
34+
filtered_attributes: A set of filtered attributes which provide additional insight into the Context when the observation was made.
35+
36+
References:
37+
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#exemplars
38+
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#exemplar
39+
"""
40+
filtered_attributes: Attributes
41+
value: Union[int, float]
42+
time_unix_nano: int
43+
span_id: Optional[str] = None
44+
trace_id: Optional[str] = None
45+
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)