-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathtest_base.py
More file actions
73 lines (60 loc) · 2.48 KB
/
test_base.py
File metadata and controls
73 lines (60 loc) · 2.48 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
# 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 opentelemetry import trace as trace_api
from opentelemetry.sdk.trace import TracerProvider, export
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
InMemorySpanExporter,
)
class TestBase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.original_provider = trace_api.get_tracer_provider()
result = cls.create_tracer_provider()
cls.tracer_provider, cls.memory_exporter = result
trace_api.set_tracer_provider(cls.tracer_provider)
@classmethod
def tearDownClass(cls):
trace_api.set_tracer_provider(cls.original_provider)
def setUp(self):
self.memory_exporter.clear()
def check_span_instrumentation_info(self, span, module):
self.assertEqual(span.instrumentation_info.name, module.__name__)
self.assertEqual(span.instrumentation_info.version, module.__version__)
@staticmethod
def create_tracer_provider(**kwargs):
"""Helper to create a configured tracer provider.
Creates and configures a `TracerProvider` with a
`SimpleExportSpanProcessor` 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
memory exporter in the second.
"""
tracer_provider = TracerProvider(**kwargs)
memory_exporter = InMemorySpanExporter()
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
tracer_provider.add_span_processor(span_processor)
return tracer_provider, memory_exporter
@staticmethod
@contextmanager
def disable_logging(highest_level=logging.CRITICAL):
logging.disable(highest_level)
try:
yield
finally:
logging.disable(logging.NOTSET)