def test_create_stats_exporter_initialization(self, mock_transport): event = threading.Event() def _helper(request_iterator, context): for request in request_iterator: event.set() yield self._add_and_start_service(GenericRpcHandler(_helper)) exporter = ocagent.new_stats_exporter(SERVICE_NAME, endpoint='localhost:%s' % self._port, interval=0.1) self.assertEqual(mock_transport.call_args[0][0], stats_module.stats) self.assertEqual(mock_transport.call_args[0][1], exporter) self.assertEqual(mock_transport.call_args[0][2], 0.1) exporter.export_metrics([ _create_metric(points=[ point.Point(value.ValueLong(1), timestamp=datetime.now()) ]) ]) self.assertTrue(event.wait(timeout=1))
def to_point_value(self): """Get a point value conversion of the current value. :rtype: :class:`opencensus.metrics.export.value.ValueLong` :return: A converted `ValueLong`. """ return value_module.ValueLong(self.value)
def test_export_metrics(self): lv = label_value.LabelValue('val') val = value.ValueLong(value=123) dt = datetime(2019, 3, 20, 21, 34, 0, 537954) pp = point.Point(value=val, timestamp=dt) ts = [ time_series.TimeSeries(label_values=[lv], points=[pp], start_timestamp=utils.to_iso_str(dt)) ] desc = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) mm = metric.Metric(descriptor=desc, time_series=ts) exporter = stackdriver.StackdriverStatsExporter(client=mock.Mock()) exporter.export_metrics([mm]) self.assertEqual(exporter.client.create_time_series.call_count, 1) sd_args = exporter.client.create_time_series.call_args[0][1] self.assertEqual(len(sd_args), 1) [sd_arg] = exporter.client.create_time_series.call_args[0][1] self.assertEqual(sd_arg.points[0].value.int64_value, 123)
def to_point(self, timestamp): """Get a Point conversion of this aggregation. :type timestamp: :class: `datetime.datetime` :param timestamp: The time to report the point as having been recorded. :rtype: :class: `opencensus.metrics.export.point.Point` :return: a :class: `opencensus.metrics.export.value.ValueLong`-valued Point with value equal to `count_data`. """ return point.Point(value.ValueLong(self.count_data), timestamp)
def setUp(self): self.double_value = value_module.ValueDouble(55.5) self.long_value = value_module.ValueLong(9876543210) self.timestamp = '2018-10-06T17:57:57.936475Z' value_at_percentile = [summary_module.ValueAtPercentile(99.5, 10.2)] snapshot = summary_module.Snapshot(10, 87.07, value_at_percentile) self.summary = summary_module.Summary(10, 6.6, snapshot) self.summary_value = value_module.ValueSummary(self.summary) self.distribution_value = value_module.ValueDistribution( 100, 1000.0, 10.0, value_module.BucketOptions( value_module.Explicit(list(range(1, 10)))), [value_module.Bucket(10, None) for ii in range(10)], )
def test_export_single_metric(self, mock_stats, mock_client): """Check that we can export a set of a single metric.""" lv = label_value.LabelValue('val') val = value.ValueLong(value=123) dt = datetime(2019, 3, 20, 21, 34, 0, 537954) pp = point.Point(value=val, timestamp=dt) ts = [ time_series.TimeSeries(label_values=[lv], points=[pp], start_timestamp=utils.to_iso_str(dt)) ] desc = metric_descriptor.MetricDescriptor( name='name2', description='description2', unit='unit2', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) mm = metric.Metric(descriptor=desc, time_series=ts) mock_stats.get_metrics.return_value = [mm] with MockGetExporterThread() as mget: exporter = stackdriver.new_stats_exporter( stackdriver.Options(project_id=1)) mget.transport.step() exporter.client.create_metric_descriptor.assert_called() self.assertEqual(exporter.client.create_metric_descriptor.call_count, 1) md_call_arg =\ exporter.client.create_metric_descriptor.call_args[0][1] self.assertEqual(md_call_arg.metric_kind, monitoring_v3.enums.MetricDescriptor.MetricKind.GAUGE) self.assertEqual(md_call_arg.value_type, monitoring_v3.enums.MetricDescriptor.ValueType.INT64) exporter.client.create_time_series.assert_called() self.assertEqual(exporter.client.create_time_series.call_count, 1) ts_call_arg = exporter.client.create_time_series.call_args[0][1] self.assertEqual(len(ts_call_arg), 1) self.assertEqual(len(ts_call_arg[0].points), 1) self.assertEqual(ts_call_arg[0].points[0].value.int64_value, 123)
def create_metric(): lv = label_value.LabelValue('val') val = value.ValueLong(value=123) dt = datetime(2019, 3, 20, 21, 34, 0, 537954) pp = point.Point(value=val, timestamp=dt) ts = [ time_series.TimeSeries(label_values=[lv], points=[pp], start_timestamp=utils.to_iso_str(dt)) ] desc = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) return metric.Metric(descriptor=desc, time_series=ts)
def test_create_long_value(self): long_value = value_module.ValueLong(123456789) self.assertIsNotNone(long_value) self.assertIsInstance(long_value, value_module.ValueLong) self.assertEqual(long_value.value, 123456789)
# 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. import unittest from opencensus.metrics import label_value from opencensus.metrics.export import point from opencensus.metrics.export import time_series from opencensus.metrics.export import value START_TIMESTAMP = '2018-10-09T22:33:44.012345Z' LABEL_VALUE1 = label_value.LabelValue('value one') LABEL_VALUE2 = label_value.LabelValue('价值二') LABEL_VALUES = (LABEL_VALUE1, LABEL_VALUE2) POINTS = (point.Point(value.ValueLong(1), "2018-10-09T23:33:44.012345Z"), point.Point(value.ValueLong(2), "2018-10-10T00:33:44.012345Z"), point.Point(value.ValueLong(3), "2018-10-10T01:33:44.012345Z"), point.Point(value.ValueLong(4), "2018-10-10T02:33:44.012345Z"), point.Point(value.ValueLong(5), "2018-10-10T03:33:44.012345Z")) class TestTimeSeries(unittest.TestCase): def test_init(self): ts = time_series.TimeSeries(LABEL_VALUES, POINTS, START_TIMESTAMP) self.assertEqual(ts.start_timestamp, START_TIMESTAMP) self.assertEqual(ts.label_values, LABEL_VALUES) self.assertEqual(ts.points, POINTS) def test_init_invalid(self):
# See the License for the specific language governing permissions and # limitations under the License. import unittest from opencensus.metrics import label_value from opencensus.metrics.export import point from opencensus.metrics.export import time_series from opencensus.metrics.export import value START_TIMESTAMP = '2018-10-09T22:33:44.012345Z' LABEL_VALUE1 = label_value.LabelValue('value one') LABEL_VALUE2 = label_value.LabelValue('价值二') LABEL_VALUES = (LABEL_VALUE1, LABEL_VALUE2) POINTS = (point.Point( value.ValueLong(1), "2018-10-09T23:33:44.012345Z"), point.Point( value.ValueLong(2), "2018-10-10T00:33:44.012345Z"), point.Point( value.ValueLong(3), "2018-10-10T01:33:44.012345Z"), point.Point( value.ValueLong(4), "2018-10-10T02:33:44.012345Z"), point.Point( value.ValueLong(5), "2018-10-10T03:33:44.012345Z")) class TestTimeSeries(unittest.TestCase): def test_init(self): ts = time_series.TimeSeries(LABEL_VALUES, POINTS, START_TIMESTAMP) self.assertEqual(ts.start_timestamp, START_TIMESTAMP)