github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/metrics/monitoring_infos_test.py (about) 1 # Licensed to the Apache Software Foundation (ASF) under one or more 2 # contributor license agreements. See the NOTICE file distributed with 3 # this work for additional information regarding copyright ownership. 4 # The ASF licenses this file to You under the Apache License, Version 2.0 5 # (the "License"); you may not use this file except in compliance with 6 # the License. You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 17 # pytype: skip-file 18 19 import unittest 20 21 from apache_beam.metrics import monitoring_infos 22 from apache_beam.metrics.cells import CounterCell 23 from apache_beam.metrics.cells import GaugeCell 24 25 26 class MonitoringInfosTest(unittest.TestCase): 27 def test_parse_namespace_and_name_for_nonuser_metric(self): 28 input = monitoring_infos.create_monitoring_info( 29 "beam:dummy:metric", "typeurn", None) 30 namespace, name = monitoring_infos.parse_namespace_and_name(input) 31 self.assertEqual(namespace, "beam") 32 self.assertEqual(name, "dummy:metric") 33 34 def test_parse_namespace_and_name_for_user_counter_metric(self): 35 urn = monitoring_infos.USER_COUNTER_URN 36 labels = {} 37 labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace" 38 labels[monitoring_infos.NAME_LABEL] = "countername" 39 input = monitoring_infos.create_monitoring_info( 40 urn, "typeurn", None, labels) 41 namespace, name = monitoring_infos.parse_namespace_and_name(input) 42 self.assertEqual(namespace, "counternamespace") 43 self.assertEqual(name, "countername") 44 45 def test_parse_namespace_and_name_for_user_distribution_metric(self): 46 urn = monitoring_infos.USER_DISTRIBUTION_URN 47 labels = {} 48 labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace" 49 labels[monitoring_infos.NAME_LABEL] = "countername" 50 input = monitoring_infos.create_monitoring_info( 51 urn, "typeurn", None, labels) 52 namespace, name = monitoring_infos.parse_namespace_and_name(input) 53 self.assertEqual(namespace, "counternamespace") 54 self.assertEqual(name, "countername") 55 56 def test_parse_namespace_and_name_for_user_gauge_metric(self): 57 urn = monitoring_infos.USER_GAUGE_URN 58 labels = {} 59 labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace" 60 labels[monitoring_infos.NAME_LABEL] = "countername" 61 input = monitoring_infos.create_monitoring_info( 62 urn, "typeurn", None, labels) 63 namespace, name = monitoring_infos.parse_namespace_and_name(input) 64 self.assertEqual(namespace, "counternamespace") 65 self.assertEqual(name, "countername") 66 67 def test_int64_user_gauge(self): 68 metric = GaugeCell().get_cumulative() 69 result = monitoring_infos.int64_user_gauge( 70 'gaugenamespace', 'gaugename', metric) 71 _, gauge_value = monitoring_infos.extract_gauge_value(result) 72 self.assertEqual(0, gauge_value) 73 74 def test_int64_user_counter(self): 75 expected_labels = {} 76 expected_labels[monitoring_infos.NAMESPACE_LABEL] = "counternamespace" 77 expected_labels[monitoring_infos.NAME_LABEL] = "countername" 78 79 metric = CounterCell().get_cumulative() 80 result = monitoring_infos.int64_user_counter( 81 'counternamespace', 'countername', metric) 82 counter_value = monitoring_infos.extract_counter_value(result) 83 84 self.assertEqual(0, counter_value) 85 self.assertEqual(result.labels, expected_labels) 86 87 def test_int64_counter(self): 88 expected_labels = {} 89 expected_labels[monitoring_infos.PCOLLECTION_LABEL] = "collectionname" 90 expected_labels[monitoring_infos.PTRANSFORM_LABEL] = "ptransformname" 91 expected_labels[monitoring_infos.SERVICE_LABEL] = "BigQuery" 92 93 labels = { 94 monitoring_infos.SERVICE_LABEL: "BigQuery", 95 } 96 metric = CounterCell().get_cumulative() 97 result = monitoring_infos.int64_counter( 98 monitoring_infos.API_REQUEST_COUNT_URN, 99 metric, 100 ptransform="ptransformname", 101 pcollection="collectionname", 102 labels=labels) 103 counter_value = monitoring_infos.extract_counter_value(result) 104 105 self.assertEqual(0, counter_value) 106 self.assertEqual(result.labels, expected_labels) 107 108 109 if __name__ == '__main__': 110 unittest.main()