def testCountersWithFieldsAreWrittenToDataStore(self): stats.STATS.RegisterCounterMetric("counter", fields=[("source", str)]) stats.STATS.IncrementCounter("counter", fields=["http"]) stats.STATS.IncrementCounter("counter", delta=2, fields=["rpc"]) self.stats_store.WriteStats( process_id=self.process_id, timestamp=42, sync=True) row = data_store.DB.ResolvePrefix( "aff4:/stats_store/some_pid", "", token=self.token) # Check that no plain counter is written. values = [ stats_store.StatsStoreValue.FromSerializedString(x[1]) for x in row if x[0] == "aff4:stats_store/counter" ] self.assertEqual(len(values), 2) http_field_value = stats_store.StatsStoreFieldValue( field_type=stats.MetricFieldDefinition.FieldType.STR, str_value="http") rpc_field_value = stats_store.StatsStoreFieldValue( field_type=stats.MetricFieldDefinition.FieldType.STR, str_value="rpc") # Check that counter with source=http is written. http_counter = [x for x in values if x.fields_values == [http_field_value]] self.assertTrue(http_counter) self.assertEqual(http_counter[0].value_type, stats.MetricMetadata.ValueType.INT) self.assertEqual(http_counter[0].int_value, 1) # Check that counter with source=rpc is written. rpc_counter = [x for x in values if x.fields_values == [rpc_field_value]] self.assertTrue(rpc_counter) self.assertEqual(rpc_counter[0].value_type, stats.MetricMetadata.ValueType.INT) self.assertEqual(rpc_counter[0].int_value, 2)
def testEventMetricsWithFieldsAreWrittenToDataStore(self): stats.STATS.RegisterEventMetric("foo_event", fields=[("source", str)]) stats.STATS.RecordEvent("foo_event", 5, fields=["http"]) stats.STATS.RecordEvent("foo_event", 15, fields=["rpc"]) self.stats_store.WriteStats(process_id=self.process_id, timestamp=42, sync=True) row = data_store.DB.ResolveRegex("aff4:/stats_store/some_pid", ".*", token=self.token) values = [ stats_store.StatsStoreValue(x[1]) for x in row if x[0] == "aff4:stats_store/foo_event" ] self.assertEqual(len(values), 2) http_field_value = stats_store.StatsStoreFieldValue( field_type=stats.MetricFieldDefinition.FieldType.STR, str_value="http") rpc_field_value = stats_store.StatsStoreFieldValue( field_type=stats.MetricFieldDefinition.FieldType.STR, str_value="rpc") # Check that distribution with source=http is written. http_events = [ x for x in values if x.fields_values == [http_field_value] ] self.assertTrue(http_events) self.assertEqual(http_events[0].value_type, stats.MetricMetadata.ValueType.DISTRIBUTION) self.assertEqual(http_events[0].distribution_value.count, 1) self.assertEqual(http_events[0].distribution_value.sum, 5) # Check that distribution with source=rpc is written. rpc_events = [ x for x in values if x.fields_values == [rpc_field_value] ] self.assertTrue(rpc_events) self.assertEqual(rpc_events[0].value_type, stats.MetricMetadata.ValueType.DISTRIBUTION) self.assertEqual(rpc_events[0].distribution_value.count, 1) self.assertEqual(rpc_events[0].distribution_value.sum, 15)