-
Notifications
You must be signed in to change notification settings - Fork 651
/
test_base.py
278 lines (242 loc) · 9.03 KB
/
test_base.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright The 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 logging
import unittest
from contextlib import contextmanager
from typing import Optional, Sequence, Tuple
from opentelemetry import metrics as metrics_api
from opentelemetry import trace as trace_api
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics._internal.point import Metric
from opentelemetry.sdk.metrics.export import (
DataPointT,
HistogramDataPoint,
InMemoryMetricReader,
MetricReader,
NumberDataPoint,
)
from opentelemetry.sdk.trace import TracerProvider, export
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
InMemorySpanExporter,
)
from opentelemetry.test.globals_test import (
reset_metrics_globals,
reset_trace_globals,
)
class TestBase(unittest.TestCase):
# pylint: disable=C0103
def setUp(self):
super().setUp()
result = self.create_tracer_provider()
self.tracer_provider, self.memory_exporter = result
# This is done because set_tracer_provider cannot override the
# current tracer provider.
reset_trace_globals()
trace_api.set_tracer_provider(self.tracer_provider)
self.memory_exporter.clear()
# This is done because set_meter_provider cannot override the
# current meter provider.
reset_metrics_globals()
(
self.meter_provider,
self.memory_metrics_reader,
) = self.create_meter_provider()
metrics_api.set_meter_provider(self.meter_provider)
def tearDown(self):
super().tearDown()
reset_trace_globals()
reset_metrics_globals()
def get_finished_spans(self):
return FinishedTestSpans(
self, self.memory_exporter.get_finished_spans()
)
def assertEqualSpanInstrumentationInfo(self, span, module):
self.assertEqual(span.instrumentation_info.name, module.__name__)
self.assertEqual(span.instrumentation_info.version, module.__version__)
def assertEqualSpanInstrumentationScope(self, span, module):
self.assertEqual(span.instrumentation_scope.name, module.__name__)
self.assertEqual(
span.instrumentation_scope.version, module.__version__
)
def assertSpanHasAttributes(self, span, attributes):
for key, val in attributes.items():
self.assertIn(key, span.attributes)
self.assertEqual(val, span.attributes[key])
def sorted_spans(self, spans): # pylint: disable=R0201
"""
Sorts spans by span creation time.
Note: This method should not be used to sort spans in a deterministic way as the
order depends on timing precision provided by the platform.
"""
return sorted(
spans,
key=lambda s: s._start_time, # pylint: disable=W0212
reverse=True,
)
@staticmethod
def create_tracer_provider(**kwargs):
"""Helper to create a configured tracer provider.
Creates and configures a `TracerProvider` with a
`SimpleSpanProcessor` and a `InMemorySpanExporter`.
All the parameters passed are forwarded to the TracerProvider
constructor.
Returns:
A list with the tracer provider in the first element and the
in-memory span exporter in the second.
"""
tracer_provider = TracerProvider(**kwargs)
memory_exporter = InMemorySpanExporter()
span_processor = export.SimpleSpanProcessor(memory_exporter)
tracer_provider.add_span_processor(span_processor)
return tracer_provider, memory_exporter
@staticmethod
def create_meter_provider(**kwargs) -> Tuple[MeterProvider, MetricReader]:
"""Helper to create a configured meter provider
Creates a `MeterProvider` and an `InMemoryMetricReader`.
Returns:
A tuple with the meter provider in the first element and the
in-memory metrics exporter in the second
"""
memory_reader = InMemoryMetricReader()
metric_readers = kwargs.get("metric_readers", [])
metric_readers.append(memory_reader)
kwargs["metric_readers"] = metric_readers
meter_provider = MeterProvider(**kwargs)
return meter_provider, memory_reader
@staticmethod
@contextmanager
def disable_logging(highest_level=logging.CRITICAL):
logging.disable(highest_level)
try:
yield
finally:
logging.disable(logging.NOTSET)
def get_sorted_metrics(self):
metrics_data = self.memory_metrics_reader.get_metrics_data()
resource_metrics = (
metrics_data.resource_metrics if metrics_data else []
)
all_metrics = []
for metrics in resource_metrics:
for scope_metrics in metrics.scope_metrics:
all_metrics.extend(scope_metrics.metrics)
return self.sorted_metrics(all_metrics)
@staticmethod
def sorted_metrics(metrics):
"""
Sorts metrics by metric name.
"""
return sorted(
metrics,
key=lambda m: m.name,
)
def assert_metric_expected(
self,
metric: Metric,
expected_data_points: Sequence[DataPointT],
est_value_delta: Optional[float] = 0,
):
self.assertEqual(
len(expected_data_points), len(metric.data.data_points)
)
for expected_data_point in expected_data_points:
self.assert_data_point_expected(
expected_data_point, metric.data.data_points, est_value_delta
)
# pylint: disable=unidiomatic-typecheck
@staticmethod
def is_data_points_equal(
expected_data_point: DataPointT,
data_point: DataPointT,
est_value_delta: Optional[float] = 0,
):
if type(expected_data_point) != type( # noqa: E721
data_point
) or not isinstance(
expected_data_point, (HistogramDataPoint, NumberDataPoint)
):
return False
values_diff = None
if isinstance(data_point, NumberDataPoint):
values_diff = abs(expected_data_point.value - data_point.value)
elif isinstance(data_point, HistogramDataPoint):
values_diff = abs(expected_data_point.sum - data_point.sum)
if expected_data_point.count != data_point.count or (
est_value_delta == 0
and (
expected_data_point.min != data_point.min
or expected_data_point.max != data_point.max
)
):
return False
return (
values_diff <= est_value_delta
and expected_data_point.attributes == dict(data_point.attributes)
)
def assert_data_point_expected(
self,
expected_data_point: DataPointT,
data_points: Sequence[DataPointT],
est_value_delta: Optional[float] = 0,
):
is_data_point_exist = False
for data_point in data_points:
if self.is_data_points_equal(
expected_data_point, data_point, est_value_delta
):
is_data_point_exist = True
break
self.assertTrue(
is_data_point_exist,
msg=f"Data point {expected_data_point} does not exist",
)
@staticmethod
def create_number_data_point(value, attributes):
return NumberDataPoint(
value=value,
attributes=attributes,
start_time_unix_nano=0,
time_unix_nano=0,
)
@staticmethod
def create_histogram_data_point(
sum_data_point, count, max_data_point, min_data_point, attributes
):
return HistogramDataPoint(
count=count,
sum=sum_data_point,
min=min_data_point,
max=max_data_point,
attributes=attributes,
start_time_unix_nano=0,
time_unix_nano=0,
bucket_counts=[],
explicit_bounds=[],
)
class FinishedTestSpans(list):
def __init__(self, test, spans):
super().__init__(spans)
self.test = test
def by_name(self, name):
for span in self:
if span.name == name:
return span
self.test.fail(f"Did not find span with name {name}")
return None
def by_attr(self, key, value):
for span in self:
if span.attributes.get(key) == value:
return span
self.test.fail(f"Did not find span with attrs {key}={value}")
return None