-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathmonitoring_infos_test.py
More file actions
170 lines (145 loc) · 6.88 KB
/
monitoring_infos_test.py
File metadata and controls
170 lines (145 loc) · 6.88 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
160
161
162
163
164
165
166
167
168
169
170
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
# pytype: skip-file
import unittest
from apache_beam.metrics import monitoring_infos
from apache_beam.metrics.cells import CounterCell
from apache_beam.metrics.cells import GaugeCell
from apache_beam.metrics.cells import HistogramCell
from apache_beam.metrics.cells import HistogramData
from apache_beam.metrics.cells import StringSetCell
from apache_beam.utils.histogram import Histogram
from apache_beam.utils.histogram import LinearBucket
class MonitoringInfosTest(unittest.TestCase):
def test_parse_namespace_and_name_for_nonuser_metric(self):
input = monitoring_infos.create_monitoring_info(
"beam:dummy:metric", "typeurn", None)
namespace, name = monitoring_infos.parse_namespace_and_name(input)
self.assertEqual(namespace, "beam")
self.assertEqual(name, "dummy:metric")
def test_parse_namespace_and_name_for_user_counter_metric(self):
urn = monitoring_infos.USER_COUNTER_URN
labels = {}
labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace"
labels[monitoring_infos.NAME_LABEL] = "countername"
input = monitoring_infos.create_monitoring_info(
urn, "typeurn", None, labels)
namespace, name = monitoring_infos.parse_namespace_and_name(input)
self.assertEqual(namespace, "counternamespace")
self.assertEqual(name, "countername")
def test_parse_namespace_and_name_for_user_distribution_metric(self):
urn = monitoring_infos.USER_DISTRIBUTION_URN
labels = {}
labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace"
labels[monitoring_infos.NAME_LABEL] = "countername"
input = monitoring_infos.create_monitoring_info(
urn, "typeurn", None, labels)
namespace, name = monitoring_infos.parse_namespace_and_name(input)
self.assertEqual(namespace, "counternamespace")
self.assertEqual(name, "countername")
def test_parse_namespace_and_name_for_user_gauge_metric(self):
urn = monitoring_infos.USER_GAUGE_URN
labels = {}
labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace"
labels[monitoring_infos.NAME_LABEL] = "countername"
input = monitoring_infos.create_monitoring_info(
urn, "typeurn", None, labels)
namespace, name = monitoring_infos.parse_namespace_and_name(input)
self.assertEqual(namespace, "counternamespace")
self.assertEqual(name, "countername")
def test_parse_namespace_and_name_for_user_string_set_metric(self):
urn = monitoring_infos.USER_STRING_SET_URN
labels = {}
labels[monitoring_infos.NAMESPACE_LABEL] = "stringsetnamespace"
labels[monitoring_infos.NAME_LABEL] = "stringsetname"
input = monitoring_infos.create_monitoring_info(
urn, "typeurn", None, labels)
namespace, name = monitoring_infos.parse_namespace_and_name(input)
self.assertEqual(namespace, "stringsetnamespace")
self.assertEqual(name, "stringsetname")
def test_parse_namespace_and_name_for_user_histogram_metric(self):
urn = monitoring_infos.USER_HISTOGRAM_URN
labels = {}
labels[monitoring_infos.NAMESPACE_LABEL] = "histogramnamespace"
labels[monitoring_infos.NAME_LABEL] = "histogramname"
input = monitoring_infos.create_monitoring_info(
urn, "typeurn", None, labels)
namespace, name = monitoring_infos.parse_namespace_and_name(input)
self.assertEqual(name, "histogramname")
self.assertEqual(namespace, "histogramnamespace")
def test_int64_user_gauge(self):
metric = GaugeCell().get_cumulative()
result = monitoring_infos.int64_user_gauge(
'gaugenamespace', 'gaugename', metric)
_, gauge_value = monitoring_infos.extract_gauge_value(result)
self.assertEqual(0, gauge_value)
def test_int64_user_counter(self):
expected_labels = {}
expected_labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace"
expected_labels[monitoring_infos.NAME_LABEL] = "countername"
metric = CounterCell().get_cumulative()
result = monitoring_infos.int64_user_counter(
'counternamespace', 'countername', metric)
counter_value = monitoring_infos.extract_counter_value(result)
self.assertEqual(0, counter_value)
self.assertEqual(result.labels, expected_labels)
def test_int64_counter(self):
expected_labels = {}
expected_labels[monitoring_infos.PCOLLECTION_LABEL] = "collectionname"
expected_labels[monitoring_infos.PTRANSFORM_LABEL] = "ptransformname"
expected_labels[monitoring_infos.SERVICE_LABEL] = "BigQuery"
labels = {
monitoring_infos.SERVICE_LABEL: "BigQuery",
}
metric = CounterCell().get_cumulative()
result = monitoring_infos.int64_counter(
monitoring_infos.API_REQUEST_COUNT_URN,
metric,
ptransform="ptransformname",
pcollection="collectionname",
labels=labels)
counter_value = monitoring_infos.extract_counter_value(result)
self.assertEqual(0, counter_value)
self.assertEqual(result.labels, expected_labels)
def test_user_set_string(self):
expected_labels = {}
expected_labels[monitoring_infos.NAMESPACE_LABEL] = "stringsetnamespace"
expected_labels[monitoring_infos.NAME_LABEL] = "stringsetname"
metric = StringSetCell().get_cumulative()
result = monitoring_infos.user_set_string(
'stringsetnamespace', 'stringsetname', metric)
string_set_value = monitoring_infos.extract_string_set_value(result)
self.assertEqual(set(), string_set_value)
self.assertEqual(result.labels, expected_labels)
def test_user_histogram(self):
datapoints = [5, 50, 90]
expected_labels = {}
expected_labels[monitoring_infos.NAMESPACE_LABEL] = "histogramnamespace"
expected_labels[monitoring_infos.NAME_LABEL] = "histogramname"
cell = HistogramCell(LinearBucket(0, 1, 100))
for point in datapoints:
cell.update(point)
metric = cell.get_cumulative()
result = monitoring_infos.user_histogram(
'histogramnamespace', 'histogramname', metric)
histogramvalue = monitoring_infos.extract_histogram_value(result)
self.assertEqual(result.labels, expected_labels)
exp_histogram = Histogram(LinearBucket(0, 1, 100))
for point in datapoints:
exp_histogram.record(point)
self.assertEqual(HistogramData(exp_histogram), histogramvalue)
if __name__ == '__main__':
unittest.main()