|
| 1 | +# Copyright 2018, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +try: |
| 16 | + import mock |
| 17 | +except ImportError: |
| 18 | + from unittest import mock |
| 19 | + |
| 20 | +import unittest |
| 21 | + |
| 22 | +from opencensus.metrics.export import metric_descriptor |
| 23 | +from opencensus.stats import aggregation |
| 24 | +from opencensus.stats import measure |
| 25 | +from opencensus.stats import metric_utils |
| 26 | +from opencensus.stats import view |
| 27 | + |
| 28 | + |
| 29 | +class TestMetricUtils(unittest.TestCase): |
| 30 | + def test_get_metric_type(self): |
| 31 | + measure_int = mock.Mock(spec=measure.MeasureInt) |
| 32 | + measure_float = mock.Mock(spec=measure.MeasureFloat) |
| 33 | + agg_sum = mock.Mock(spec=aggregation.SumAggregation) |
| 34 | + agg_sum.aggregation_type = aggregation.Type.SUM |
| 35 | + agg_count = mock.Mock(spec=aggregation.CountAggregation) |
| 36 | + agg_count.aggregation_type = aggregation.Type.COUNT |
| 37 | + agg_dist = mock.Mock(spec=aggregation.DistributionAggregation) |
| 38 | + agg_dist.aggregation_type = aggregation.Type.DISTRIBUTION |
| 39 | + agg_lv = mock.Mock(spec=aggregation.LastValueAggregation) |
| 40 | + agg_lv.aggregation_type = aggregation.Type.LASTVALUE |
| 41 | + |
| 42 | + view_to_metric_type = { |
| 43 | + (measure_int, agg_sum): |
| 44 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64, |
| 45 | + (measure_int, agg_count): |
| 46 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64, |
| 47 | + (measure_int, agg_dist): |
| 48 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION, |
| 49 | + (measure_int, agg_lv): |
| 50 | + metric_descriptor.MetricDescriptorType.GAUGE_INT64, |
| 51 | + (measure_float, agg_sum): |
| 52 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE, |
| 53 | + (measure_float, agg_count): |
| 54 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64, |
| 55 | + (measure_float, agg_dist): |
| 56 | + metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION, |
| 57 | + (measure_float, agg_lv): |
| 58 | + metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE, |
| 59 | + } |
| 60 | + |
| 61 | + for (mm, ma), metric_type in view_to_metric_type.items(): |
| 62 | + self.assertEqual(metric_utils.get_metric_type(mm, ma), metric_type) |
| 63 | + |
| 64 | + def test_get_metric_type_bad_aggregation(self): |
| 65 | + base_agg = mock.Mock(spec=aggregation.BaseAggregation) |
| 66 | + base_agg.aggregation_type = aggregation.Type.NONE |
| 67 | + with self.assertRaises(ValueError): |
| 68 | + metric_utils.get_metric_type(mock.Mock(), base_agg) |
| 69 | + |
| 70 | + bad_agg = mock.Mock(spec=aggregation.SumAggregation) |
| 71 | + bad_agg.aggregation_type = aggregation.Type.COUNT |
| 72 | + with self.assertRaises(AssertionError): |
| 73 | + metric_utils.get_metric_type(mock.Mock(), bad_agg) |
| 74 | + |
| 75 | + def test_get_metric_type_bad_measure(self): |
| 76 | + base_measure = mock.Mock(spec=measure.BaseMeasure) |
| 77 | + agg_sum = mock.Mock(spec=aggregation.SumAggregation) |
| 78 | + agg_sum.aggregation_type = aggregation.Type.SUM |
| 79 | + agg_lv = mock.Mock(spec=aggregation.LastValueAggregation) |
| 80 | + agg_lv.aggregation_type = aggregation.Type.LASTVALUE |
| 81 | + with self.assertRaises(ValueError): |
| 82 | + metric_utils.get_metric_type(base_measure, agg_sum) |
| 83 | + with self.assertRaises(ValueError): |
| 84 | + metric_utils.get_metric_type(base_measure, agg_lv) |
| 85 | + |
| 86 | + def test_view_to_metric_descriptor(self): |
| 87 | + mock_measure = mock.Mock(spec=measure.MeasureFloat) |
| 88 | + mock_agg = mock.Mock(spec=aggregation.SumAggregation) |
| 89 | + mock_agg.aggregation_type = aggregation.Type.SUM |
| 90 | + test_view = view.View("name", "description", ["tk1", "tk2"], |
| 91 | + mock_measure, mock_agg) |
| 92 | + |
| 93 | + md = metric_utils.view_to_metric_descriptor(test_view) |
| 94 | + self.assertTrue(isinstance(md, metric_descriptor.MetricDescriptor)) |
| 95 | + self.assertEqual(md.name, test_view.name) |
| 96 | + self.assertEqual(md.description, test_view.description) |
| 97 | + self.assertEqual(md.unit, test_view.measure.unit) |
| 98 | + self.assertEqual( |
| 99 | + md.type, metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE) |
| 100 | + self.assertTrue( |
| 101 | + all(lk.key == col |
| 102 | + for lk, col in zip(md.label_keys, test_view.columns))) |
0 commit comments