forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_resources.py
235 lines (196 loc) · 8.44 KB
/
test_resources.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
# 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.
# pylint: disable=protected-access
import os
import unittest
from unittest import mock
from opentelemetry.sdk import resources
class TestResources(unittest.TestCase):
def setUp(self) -> None:
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = ""
def tearDown(self) -> None:
os.environ.pop(resources.OTEL_RESOURCE_ATTRIBUTES)
def test_create(self):
attributes = {
"service": "ui",
"version": 1,
"has_bugs": True,
"cost": 112.12,
}
expected_attributes = {
"service": "ui",
"version": 1,
"has_bugs": True,
"cost": 112.12,
resources.TELEMETRY_SDK_NAME: "opentelemetry",
resources.TELEMETRY_SDK_LANGUAGE: "python",
resources.TELEMETRY_SDK_VERSION: resources.OPENTELEMETRY_SDK_VERSION,
}
resource = resources.Resource.create(attributes)
self.assertIsInstance(resource, resources.Resource)
self.assertEqual(resource.attributes, expected_attributes)
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = "key=value"
resource = resources.Resource.create(attributes)
self.assertIsInstance(resource, resources.Resource)
expected_with_envar = expected_attributes.copy()
expected_with_envar["key"] = "value"
self.assertEqual(resource.attributes, expected_with_envar)
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = ""
resource = resources.Resource.create_empty()
self.assertEqual(resource, resources._EMPTY_RESOURCE)
resource = resources.Resource.create(None)
self.assertEqual(resource, resources._DEFAULT_RESOURCE)
resource = resources.Resource.create({})
self.assertEqual(resource, resources._DEFAULT_RESOURCE)
def test_resource_merge(self):
left = resources.Resource({"service": "ui"})
right = resources.Resource({"host": "service-host"})
self.assertEqual(
left.merge(right),
resources.Resource({"service": "ui", "host": "service-host"}),
)
def test_resource_merge_empty_string(self):
"""Verify Resource.merge behavior with the empty string.
Attributes from the source Resource take precedence, with
the exception of the empty string.
"""
left = resources.Resource({"service": "ui", "host": ""})
right = resources.Resource(
{"host": "service-host", "service": "not-ui"}
)
self.assertEqual(
left.merge(right),
resources.Resource({"service": "ui", "host": "service-host"}),
)
def test_immutability(self):
attributes = {
"service": "ui",
"version": 1,
"has_bugs": True,
"cost": 112.12,
}
default_attributes = {
resources.TELEMETRY_SDK_NAME: "opentelemetry",
resources.TELEMETRY_SDK_LANGUAGE: "python",
resources.TELEMETRY_SDK_VERSION: resources.OPENTELEMETRY_SDK_VERSION,
}
attributes_copy = attributes.copy()
attributes_copy.update(default_attributes)
resource = resources.Resource.create(attributes)
self.assertEqual(resource.attributes, attributes_copy)
resource.attributes["has_bugs"] = False
self.assertEqual(resource.attributes, attributes_copy)
attributes["cost"] = 999.91
self.assertEqual(resource.attributes, attributes_copy)
def test_aggregated_resources_no_detectors(self):
aggregated_resources = resources.get_aggregated_resources([])
self.assertEqual(
aggregated_resources, resources.Resource.create_empty()
)
def test_aggregated_resources_with_static_resource(self):
static_resource = resources.Resource({"static_key": "static_value"})
self.assertEqual(
resources.get_aggregated_resources(
[], initial_resource=static_resource
),
static_resource,
)
# Static resource values should never be overwritten
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.return_value = resources.Resource(
{"static_key": "try_to_overwrite_existing_value", "key": "value"}
)
self.assertEqual(
resources.get_aggregated_resources(
[resource_detector], initial_resource=static_resource
),
resources.Resource({"static_key": "static_value", "key": "value"}),
)
def test_aggregated_resources_multiple_detectors(self):
resource_detector1 = mock.Mock(spec=resources.ResourceDetector)
resource_detector1.detect.return_value = resources.Resource(
{"key1": "value1"}
)
resource_detector2 = mock.Mock(spec=resources.ResourceDetector)
resource_detector2.detect.return_value = resources.Resource(
{"key2": "value2", "key3": "value3"}
)
resource_detector3 = mock.Mock(spec=resources.ResourceDetector)
resource_detector3.detect.return_value = resources.Resource(
{
"key2": "try_to_overwrite_existing_value",
"key3": "try_to_overwrite_existing_value",
"key4": "value4",
}
)
# New values should not overwrite existing values
self.assertEqual(
resources.get_aggregated_resources(
[resource_detector1, resource_detector2, resource_detector3]
),
resources.Resource(
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
}
),
)
def test_resource_detector_ignore_error(self):
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.side_effect = Exception()
resource_detector.raise_on_error = False
self.assertEqual(
resources.get_aggregated_resources([resource_detector]),
resources.Resource.create_empty(),
)
def test_resource_detector_raise_error(self):
resource_detector = mock.Mock(spec=resources.ResourceDetector)
resource_detector.detect.side_effect = Exception()
resource_detector.raise_on_error = True
self.assertRaises(
Exception, resources.get_aggregated_resources, [resource_detector],
)
class TestOTELResourceDetector(unittest.TestCase):
def setUp(self) -> None:
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = ""
def tearDown(self) -> None:
os.environ.pop(resources.OTEL_RESOURCE_ATTRIBUTES)
def test_empty(self):
detector = resources.OTELResourceDetector()
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = ""
self.assertEqual(detector.detect(), resources.Resource.create_empty())
def test_one(self):
detector = resources.OTELResourceDetector()
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = "k=v"
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
def test_one_with_whitespace(self):
detector = resources.OTELResourceDetector()
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = " k = v "
self.assertEqual(detector.detect(), resources.Resource({"k": "v"}))
def test_multiple(self):
detector = resources.OTELResourceDetector()
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = "k=v,k2=v2"
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)
def test_multiple_with_whitespace(self):
detector = resources.OTELResourceDetector()
os.environ[
resources.OTEL_RESOURCE_ATTRIBUTES
] = " k = v , k2 = v2 "
self.assertEqual(
detector.detect(), resources.Resource({"k": "v", "k2": "v2"})
)