|
| 1 | +# Copyright 2019 Google Inc. All Rights Reserved. |
| 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 | +from unittest import mock |
| 16 | +import unittest |
| 17 | + |
| 18 | +from opencensus.ext.stackdriver import stats_exporter |
| 19 | +from opencensus.stats import aggregation |
| 20 | +from opencensus.stats import measure |
| 21 | +from opencensus.stats import stats |
| 22 | +from opencensus.stats import view |
| 23 | + |
| 24 | + |
| 25 | +class TestMetricsQuickstart(unittest.TestCase): |
| 26 | + """Sanity checks for the OpenCensus metrics quickstart examples. |
| 27 | +
|
| 28 | + These tests check that a few basic features of the library work as expected |
| 29 | + in order for the demo to run. See the opencensus and |
| 30 | + opencensus-ext-stackdriver source for actual library tests. |
| 31 | + """ |
| 32 | + def test_measure_creation(self): |
| 33 | + measure.MeasureFloat( |
| 34 | + "task_latency", |
| 35 | + "The task latency in milliseconds", |
| 36 | + "ms") |
| 37 | + |
| 38 | + def test_view_creation(self): |
| 39 | + test_view = view.View( |
| 40 | + "task_latency_distribution", |
| 41 | + "The distribution of the task latencies", |
| 42 | + [], |
| 43 | + mock.Mock(), |
| 44 | + aggregation.DistributionAggregation([1.0, 2.0, 3.0])) |
| 45 | + # Check that metric descriptor conversion doesn't crash |
| 46 | + test_view.get_metric_descriptor() |
| 47 | + |
| 48 | + # Don't modify global stats |
| 49 | + @mock.patch('opencensus.ext.stackdriver.stats_exporter.stats.stats', |
| 50 | + stats._Stats()) |
| 51 | + def test_measurement_map_record(self): |
| 52 | + mock_measure = mock.Mock() |
| 53 | + mock_measure_name = mock.Mock() |
| 54 | + mock_measure.name = mock_measure_name |
| 55 | + mock_view = mock.Mock() |
| 56 | + mock_view.columns = [] |
| 57 | + mock_view.measure = mock_measure |
| 58 | + |
| 59 | + stats.stats.view_manager.register_view(mock_view) |
| 60 | + |
| 61 | + mmap = stats.stats.stats_recorder.new_measurement_map() |
| 62 | + mmap.measure_float_put(mock_measure, 1.0) |
| 63 | + mmap.record() |
| 64 | + |
| 65 | + # Reaching into the stats internals here to check that recording the |
| 66 | + # measurement map affects view data. |
| 67 | + m2vd = (stats.stats.view_manager.measure_to_view_map |
| 68 | + ._measure_to_view_data_list_map) |
| 69 | + self.assertIn(mock_measure_name, m2vd) |
| 70 | + [view_data] = m2vd[mock_measure_name] |
| 71 | + agg_data = view_data.tag_value_aggregation_data_map[tuple()] |
| 72 | + agg_data.add_sample.assert_called_once() |
| 73 | + |
| 74 | + @mock.patch('opencensus.ext.stackdriver.stats_exporter' |
| 75 | + '.monitoring_v3.MetricServiceClient') |
| 76 | + def test_new_stats_exporter(self, mock_client): |
| 77 | + transport = stats_exporter.new_stats_exporter() |
| 78 | + self.assertIsNotNone(transport) |
| 79 | + self.assertIsNotNone(transport.options) |
| 80 | + self.assertIsNotNone(transport.options.project_id) |
0 commit comments