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 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 _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
def testStatsEntryId_IgnoreMetricValues(self): """Ensures metric values have no influence id generation.""" int_entry = stats_values.StatsStoreEntry( process_id="test_process", metric_name="test_metric", metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.INT, int_value=42), timestamp=_one_second_timestamp) float_entry = stats_values.StatsStoreEntry( process_id="test_process", metric_name="test_metric", metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.FLOAT, float_value=4.2), timestamp=_one_second_timestamp) # TODO: String gauges are deprecated. str_entry = stats_values.StatsStoreEntry( process_id="test_process", metric_name="test_metric", metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.DEPRECATED_STR, str_value="foo"), timestamp=_one_second_timestamp) distribution_entry = stats_values.StatsStoreEntry( process_id="test_process", metric_name="test_metric", metric_value=stats_values.StatsStoreValue( value_type=rdf_stats.MetricMetadata.ValueType.DISTRIBUTION, distribution_value=rdf_stats.Distribution()), timestamp=_one_second_timestamp) expected_id = ( b"\x8e\xf4\xe7\xdb\x03\x01}sB\x97\x98\x957\x18\x02U\xb0\xe6x\x9f" b"\x97Xfs/C\xedT\xd3\x89N\xe5") self.assertEqual(db_utils.GenerateStatsEntryId(int_entry), expected_id) self.assertEqual(db_utils.GenerateStatsEntryId(float_entry), expected_id) self.assertEqual(db_utils.GenerateStatsEntryId(str_entry), expected_id) self.assertEqual(db_utils.GenerateStatsEntryId(distribution_entry), expected_id)
def _GenerateStatsEntryForSingleDimensionalMetric(process_id, metric_name, metadata, timestamp): """Generates a StatsStoreEntry for the given single-dimensional metric. Args: process_id: Process identifier to use for for the generated entry. metric_name: Name of the single-dimensional metric. metadata: MetricMetadata for the metric. timestamp: Timestamp to use for the generated entry. Returns: A StatsStoreEntry containing the current value of the metric. """ raw_metric_value = stats_collector_instance.Get().GetMetricValue( metric_name) metric_value = stats_values.StatsStoreValue() metric_value.SetValue(raw_metric_value, metadata.value_type) return stats_values.StatsStoreEntry(process_id=process_id, metric_name=metric_name, metric_value=metric_value, timestamp=timestamp)
from grr_response_server import db from grr_response_server import stats_values _TEST_PROCESS_ID = "test_process" _SINGLE_DIM_COUNTER = "single_dim_counter" _MULTI_DIM_COUNTER = "multi_dim_counter" # RDF protobufs used for testing. # # TODO(user): Refactor rel-db tests so that component-specific test-mixin # classes do not have to worry about defining attributes/methods that conflict # with those of other mixin classes. _single_dim_entry1 = 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(1)) _single_dim_entry2 = 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(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,