forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_view_instrument_match.py
200 lines (184 loc) · 6.54 KB
/
test_view_instrument_match.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
# 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.
from unittest import TestCase
from unittest.mock import Mock
from opentelemetry.sdk._metrics._view_instrument_match import (
_ViewInstrumentMatch,
)
from opentelemetry.sdk._metrics.aggregation import (
DropAggregation,
_DropAggregation,
)
from opentelemetry.sdk._metrics.measurement import Measurement
from opentelemetry.sdk._metrics.point import AggregationTemporality, Metric
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
from opentelemetry.sdk._metrics.view import View
class Test_ViewInstrumentMatch(TestCase):
@classmethod
def setUpClass(cls):
cls.mock_aggregation_factory = Mock()
cls.mock_created_aggregation = (
cls.mock_aggregation_factory._create_aggregation()
)
cls.mock_resource = Mock()
cls.mock_instrumentation_info = Mock()
def test_consume_measurement(self):
instrument1 = Mock(name="instrument1")
instrument1.instrumentation_info = self.mock_instrumentation_info
sdk_config = SdkConfiguration(
resource=self.mock_resource,
metric_readers=[],
views=[],
)
view_instrument_match = _ViewInstrumentMatch(
view=View(
instrument_name="instrument1",
name="name",
aggregation=self.mock_aggregation_factory,
attribute_keys={"a", "c"},
),
instrument=instrument1,
sdk_config=sdk_config,
)
view_instrument_match.consume_measurement(
Measurement(
value=0,
instrument=instrument1,
attributes={"c": "d", "f": "g"},
)
)
self.assertEqual(
view_instrument_match._attributes_aggregation,
{frozenset([("c", "d")]): self.mock_created_aggregation},
)
view_instrument_match.consume_measurement(
Measurement(
value=0,
instrument=instrument1,
attributes={"w": "x", "y": "z"},
)
)
self.assertEqual(
view_instrument_match._attributes_aggregation,
{
frozenset(): self.mock_created_aggregation,
frozenset([("c", "d")]): self.mock_created_aggregation,
},
)
# None attribute_keys (default) will keep all attributes
view_instrument_match = _ViewInstrumentMatch(
view=View(
instrument_name="instrument1",
name="name",
aggregation=self.mock_aggregation_factory,
),
instrument=instrument1,
sdk_config=sdk_config,
)
view_instrument_match.consume_measurement(
Measurement(
value=0,
instrument=instrument1,
attributes={"c": "d", "f": "g"},
)
)
self.assertEqual(
view_instrument_match._attributes_aggregation,
{
frozenset(
[("c", "d"), ("f", "g")]
): self.mock_created_aggregation
},
)
# empty set attribute_keys will drop all labels and aggregate everything together
view_instrument_match = _ViewInstrumentMatch(
view=View(
instrument_name="instrument1",
name="name",
aggregation=self.mock_aggregation_factory,
attribute_keys={},
),
instrument=instrument1,
sdk_config=sdk_config,
)
view_instrument_match.consume_measurement(
Measurement(value=0, instrument=instrument1, attributes=None)
)
self.assertEqual(
view_instrument_match._attributes_aggregation,
{frozenset({}): self.mock_created_aggregation},
)
# Test that a drop aggregation is handled in the same way as any
# other aggregation.
drop_aggregation = DropAggregation()
view_instrument_match = _ViewInstrumentMatch(
view=View(
instrument_name="instrument1",
name="name",
aggregation=drop_aggregation,
attribute_keys={},
),
instrument=instrument1,
sdk_config=sdk_config,
)
view_instrument_match.consume_measurement(
Measurement(value=0, instrument=instrument1, attributes=None)
)
self.assertIsInstance(
view_instrument_match._attributes_aggregation[frozenset({})],
_DropAggregation,
)
def test_collect(self):
instrument1 = Mock(
name="instrument1", description="description", unit="unit"
)
instrument1.instrumentation_info = self.mock_instrumentation_info
sdk_config = SdkConfiguration(
resource=self.mock_resource,
metric_readers=[],
views=[],
)
view_instrument_match = _ViewInstrumentMatch(
view=View(
instrument_name="instrument1",
name="name",
aggregation=self.mock_aggregation_factory,
attribute_keys={"a", "c"},
),
instrument=instrument1,
sdk_config=sdk_config,
)
view_instrument_match.consume_measurement(
Measurement(
value=0,
instrument=Mock(name="instrument1"),
attributes={"c": "d", "f": "g"},
)
)
self.assertEqual(
next(
view_instrument_match.collect(
AggregationTemporality.CUMULATIVE
)
),
Metric(
attributes={"c": "d"},
description="description",
instrumentation_info=self.mock_instrumentation_info,
name="name",
resource=self.mock_resource,
unit="unit",
point=None,
),
)