def testStatsEntryId_VaryIntDimensions(self): """Ensures StatsEntries with different int dimensions get different ids.""" entry1 = stats_values.StatsStoreEntry( process_id="test_process", metric_name="test_metric", metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.INT, fields_values=[ stats_values.StatsStoreFieldValue( field_type=rdf_stats.MetricFieldDefinition.FieldType. INT, int_value=11), stats_values.StatsStoreFieldValue( field_type=rdf_stats.MetricFieldDefinition.FieldType. INT, int_value=12) ], int_value=42), timestamp=_one_second_timestamp) # Clone entry1 and change a single dimension value. entry2 = stats_values.StatsStoreEntry() entry2.FromDict(entry1.ToPrimitiveDict()) entry2.metric_value.fields_values[0].int_value = 13 self.assertEqual( db_utils.GenerateStatsEntryId(entry1), b"pl\xc31\x1a\xf8\xd5\xfe\xe1\xc6\x10gR\x10f\x0c\xb8\xd3\x96_\xa3`\x19" b"\xf2\x15\xc5\xa0\x8d\xbbu\xf1&") self.assertEqual( db_utils.GenerateStatsEntryId(entry2), b"L \x1d+\xd5<g\\\xa8\xa4\x97\xdd\xe8^\x88\xac\xc7\xbej\xae\xff\xd5S" b"\x10\xce\xec\x82a\xe5_\xe1\x1c")
def testStatsEntryId_VaryStringDimensions(self): """Ensures StatsEntries with different str dimensions get different ids.""" entry1 = stats_values.StatsStoreEntry( process_id="test_process", metric_name="test_metric", metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.INT, fields_values=[ stats_values.StatsStoreFieldValue( field_type=rdf_stats.MetricFieldDefinition.FieldType. STR, str_value="dim1"), stats_values.StatsStoreFieldValue( field_type=rdf_stats.MetricFieldDefinition.FieldType. STR, str_value="dim2") ], int_value=42), timestamp=_one_second_timestamp) # Clone entry1 and change a single dimension value. entry2 = stats_values.StatsStoreEntry() entry2.FromDict(entry1.ToPrimitiveDict()) entry2.metric_value.fields_values[0].str_value = "dim3" self.assertEqual( db_utils.GenerateStatsEntryId(entry1), b"\xb8'\x1cv\xf9`\x90\xd1\x9d#{\x8a'y\xd8E\x0bx\x1b6f\xe6\x8d\x16\xb95" b"\x0011uy\xf9") self.assertEqual( db_utils.GenerateStatsEntryId(entry2), b"\xa4\xcb\x95*\xe4\x8aclM&@\xde\xba\x17\xec\x02\xc6i\xea\xc0\x1a{bQ" b"\xabr~w}Z\xb9\x99")
def _GenerateStatsEntriesForMultiDimensionalMetric(process_id, metric_name, metadata, timestamp): """Generates StatsStoreEntries for the given multi-dimensional metric. Args: process_id: Process identifier to use for for the generated entries. metric_name: Name of the multi-dimensional metric. metadata: MetricMetadata for the metric. timestamp: Timestamp to use for the generated entries. Returns: A list of StatsStoreEntries containing current values for the metric's dimensions. """ stats_entries = [] for raw_field_values in stats_collector_instance.Get().GetMetricFields( metric_name): _ValidateFieldValues(raw_field_values, metadata) metric_value = stats_values.StatsStoreValue() for i, raw_field_value in enumerate(raw_field_values): field_value = stats_values.StatsStoreFieldValue() field_value.SetValue(raw_field_value, metadata.fields_defs[i].field_type) metric_value.fields_values.Append(field_value) raw_metric_value = stats_collector_instance.Get().GetMetricValue( metric_name, fields=raw_field_values) metric_value.SetValue(raw_metric_value, metadata.value_type) stats_entries.append( stats_values.StatsStoreEntry(process_id=process_id, metric_name=metric_name, metric_value=metric_value, timestamp=timestamp)) return stats_entries
value_type=rdf_stats.MetricMetadata.ValueType.INT, int_value=42), timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(2)) _single_dim_entry3 = stats_values.StatsStoreEntry( process_id=_TEST_PROCESS_ID, metric_name=_SINGLE_DIM_COUNTER, metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.INT, int_value=42), timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(3)) _multi_dim_entry = stats_values.StatsStoreEntry( process_id=_TEST_PROCESS_ID, metric_name=_MULTI_DIM_COUNTER, metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.INT, fields_values=[ stats_values.StatsStoreFieldValue( field_type=rdf_stats.MetricFieldDefinition.FieldType.STR, str_value="dim1"), stats_values.StatsStoreFieldValue( field_type=rdf_stats.MetricFieldDefinition.FieldType.STR, str_value="dim2") ], int_value=42), timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(2)) class DatabaseTestStatsMixin(object): # TODO(user): Refactor rel-db tests so it becomes possible/natural to # have setUp() functions in component-specific mixins like this one. def _SetUpStatsTest(self): """Does set-up operations prior to running tests."""