def testGetAllMetricsMetadataWorksCorrectlyOnSimpleMetrics(self):
    stats.STATS.RegisterCounterMetric("test_counter")
    stats.STATS.RegisterGaugeMetric("test_int_gauge", int,
                                    fields=[("dimension", str)])
    stats.STATS.RegisterEventMetric("test_event_metric")

    metrics = stats.STATS.GetAllMetricsMetadata()
    self.assertEqual(metrics["test_counter"].metric_type,
                     stats.MetricType.COUNTER)
    self.assertFalse(metrics["test_counter"].fields_defs)

    self.assertEqual(metrics["test_int_gauge"].metric_type,
                     stats.MetricType.GAUGE)
    self.assertEqual(metrics["test_int_gauge"].fields_defs,
                     [rdfvalue.MetricFieldDefinition(
                         field_name="dimension",
                         field_type=stats.MetricFieldDefinition.FieldType.STR)])

    self.assertEqual(metrics["test_event_metric"].metric_type,
                     stats.MetricType.EVENT)
    self.assertFalse(metrics["test_event_metric"].fields_defs)
示例#2
0
    def testReadMetadataReturnsAllUsedMetadata(self):
        # Register metrics
        stats.STATS.RegisterCounterMetric("counter")
        stats.STATS.RegisterCounterMetric("counter_with_fields",
                                          fields=[("source", str)])

        stats.STATS.RegisterEventMetric("events")
        stats.STATS.RegisterEventMetric("events_with_fields",
                                        fields=[("source", str)])

        stats.STATS.RegisterGaugeMetric("str_gauge", str)
        stats.STATS.RegisterGaugeMetric("str_gauge_with_fields",
                                        str,
                                        fields=[("task", int)])

        # Check that there are no metadata for registered metrics.
        metadata = self.stats_store.ReadMetadata(process_id=self.process_id)
        self.assertFalse("counter" in metadata)
        self.assertFalse("counter_with_fields" in metadata)
        self.assertFalse("events" in metadata)
        self.assertFalse("events_with_fields" in metadata)
        self.assertFalse("str_gauge" in metadata)
        self.assertFalse("str_gauge_with_fields" in metadata)

        # Write stats to the data store. Metadata should be
        # written as well.
        self.stats_store.WriteStats(process_id=self.process_id,
                                    timestamp=42,
                                    sync=True)

        # Check that metadata were written into the store.
        metadata = self.stats_store.ReadMetadata(process_id=self.process_id)

        # Field definitions used in assertions below.
        source_field_def = rdfvalue.MetricFieldDefinition(
            field_name="source",
            field_type=rdfvalue.MetricFieldDefinition.FieldType.STR)
        task_field_def = rdfvalue.MetricFieldDefinition(
            field_name="task",
            field_type=rdfvalue.MetricFieldDefinition.FieldType.INT)

        self.assertTrue("counter" in metadata)
        self.assertEqual(metadata["counter"].varname, "counter")
        self.assertEqual(metadata["counter"].metric_type,
                         stats.MetricType.COUNTER)
        self.assertEqual(metadata["counter"].value_type,
                         rdfvalue.MetricMetadata.ValueType.INT)
        self.assertListEqual(list(metadata["counter"].fields_defs), [])

        self.assertTrue("counter_with_fields" in metadata)
        self.assertEqual(metadata["counter_with_fields"].varname,
                         "counter_with_fields")
        self.assertEqual(metadata["counter_with_fields"].metric_type,
                         stats.MetricType.COUNTER)
        self.assertEqual(metadata["counter_with_fields"].value_type,
                         rdfvalue.MetricMetadata.ValueType.INT)
        self.assertListEqual(list(metadata["counter_with_fields"].fields_defs),
                             [source_field_def])

        self.assertTrue("events" in metadata)
        self.assertEqual(metadata["events"].varname, "events")
        self.assertEqual(metadata["events"].metric_type,
                         stats.MetricType.EVENT)
        self.assertEqual(metadata["events"].value_type,
                         rdfvalue.MetricMetadata.ValueType.DISTRIBUTION)
        self.assertListEqual(list(metadata["events"].fields_defs), [])

        self.assertTrue("events_with_fields" in metadata)
        self.assertEqual(metadata["events_with_fields"].varname,
                         "events_with_fields")
        self.assertEqual(metadata["events_with_fields"].metric_type,
                         stats.MetricType.EVENT)
        self.assertEqual(metadata["events_with_fields"].value_type,
                         rdfvalue.MetricMetadata.ValueType.DISTRIBUTION)
        self.assertListEqual(list(metadata["events_with_fields"].fields_defs),
                             [source_field_def])

        self.assertTrue("str_gauge" in metadata)
        self.assertEqual(metadata["str_gauge"].varname, "str_gauge")
        self.assertEqual(metadata["str_gauge"].metric_type,
                         stats.MetricType.GAUGE)
        self.assertEqual(metadata["str_gauge"].value_type,
                         rdfvalue.MetricMetadata.ValueType.STR)
        self.assertListEqual(list(metadata["str_gauge"].fields_defs), [])

        self.assertTrue("str_gauge_with_fields" in metadata)
        self.assertEqual(metadata["str_gauge_with_fields"].varname,
                         "str_gauge_with_fields")
        self.assertEqual(metadata["str_gauge_with_fields"].metric_type,
                         stats.MetricType.GAUGE)
        self.assertEqual(metadata["str_gauge_with_fields"].value_type,
                         rdfvalue.MetricMetadata.ValueType.STR)
        self.assertListEqual(
            list(metadata["str_gauge_with_fields"].fields_defs),
            [task_field_def])