forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate.py
More file actions
159 lines (129 loc) · 4.72 KB
/
Copy pathaggregate.py
File metadata and controls
159 lines (129 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright 2019, OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import abc
import threading
from collections import namedtuple
from opentelemetry.util import time_ns
class Aggregator(abc.ABC):
"""Base class for aggregators.
Aggregators are responsible for holding aggregated values and taking a
snapshot of these values upon export (checkpoint).
"""
def __init__(self):
self.current = None
self.checkpoint = None
@abc.abstractmethod
def update(self, value):
"""Updates the current with the new value."""
@abc.abstractmethod
def take_checkpoint(self):
"""Stores a snapshot of the current value."""
@abc.abstractmethod
def merge(self, other):
"""Combines two aggregator values."""
class CounterAggregator(Aggregator):
"""Aggregator for Counter metrics."""
def __init__(self):
super().__init__()
self.current = 0
self.checkpoint = 0
self._lock = threading.Lock()
self.last_update_timestamp = None
def update(self, value):
with self._lock:
self.current += value
self.last_update_timestamp = time_ns()
def take_checkpoint(self):
with self._lock:
self.checkpoint = self.current
self.current = 0
def merge(self, other):
with self._lock:
self.checkpoint += other.checkpoint
self.last_update_timestamp = (
other.last_update_timestamp or self.last_update_timestamp
)
class MinMaxSumCountAggregator(Aggregator):
"""Agregator for Measure metrics that keeps min, max, sum and count."""
_TYPE = namedtuple("minmaxsumcount", "min max sum count")
_EMPTY = _TYPE(None, None, None, 0)
@classmethod
def _merge_checkpoint(cls, val1, val2):
if val1 is cls._EMPTY:
return val2
if val2 is cls._EMPTY:
return val1
return cls._TYPE(
min(val1.min, val2.min),
max(val1.max, val2.max),
val1.sum + val2.sum,
val1.count + val2.count,
)
def __init__(self):
super().__init__()
self.current = self._EMPTY
self.checkpoint = self._EMPTY
self._lock = threading.Lock()
self.last_update_timestamp = None
def update(self, value):
with self._lock:
if self.current is self._EMPTY:
self.current = self._TYPE(value, value, value, 1)
else:
self.current = self._TYPE(
min(self.current.min, value),
max(self.current.max, value),
self.current.sum + value,
self.current.count + 1,
)
self.last_update_timestamp = time_ns()
def take_checkpoint(self):
with self._lock:
self.checkpoint = self.current
self.current = self._EMPTY
def merge(self, other):
with self._lock:
self.checkpoint = self._merge_checkpoint(
self.checkpoint, other.checkpoint
)
self.last_update_timestamp = (
other.last_update_timestamp or self.last_update_timestamp
)
class ObserverAggregator(Aggregator):
"""Same as MinMaxSumCount but also with last value."""
_TYPE = namedtuple("minmaxsumcountlast", "min max sum count last")
def __init__(self):
super().__init__()
self.mmsc = MinMaxSumCountAggregator()
self.current = None
self.checkpoint = self._TYPE(None, None, None, 0, None)
self.last_update_timestamp = None
def update(self, value):
self.mmsc.update(value)
self.current = value
self.last_update_timestamp = time_ns()
def take_checkpoint(self):
self.mmsc.take_checkpoint()
self.checkpoint = self._TYPE(*(self.mmsc.checkpoint + (self.current,)))
def merge(self, other):
self.mmsc.merge(other.mmsc)
self.checkpoint = self._TYPE(
*(
self.mmsc.checkpoint
+ (other.checkpoint.last or self.checkpoint.last or 0,)
)
)
self.last_update_timestamp = (
other.last_update_timestamp or self.last_update_timestamp
)