def testGaugeWithCallback(self): with self.SetUpStatsCollector(self._CreateStatsCollector()): int_gauge = metrics.Gauge("testGaugeWithCallback_int_gauge", int) float_gauge = metrics.Gauge("testGaugeWithCallback_float_gauge", float) self.assertEqual(0, int_gauge.GetValue()) self.assertEqual(0.0, float_gauge.GetValue()) int_gauge.SetCallback(lambda: 42) float_gauge.SetCallback(lambda: 42.3) self.assertEqual(42, int_gauge.GetValue()) self.assertAlmostEqual(42.3, float_gauge.GetValue())
def testGetAllMetricsMetadataWorksCorrectlyOnSimpleMetrics(self): counter_name = "testGAMM_SimpleMetrics_counter" int_gauge_name = "testGAMM_SimpleMetrics_int_gauge" event_metric_name = "testGAMM_SimpleMetrics_event_metric" with self.SetUpStatsCollector(self._CreateStatsCollector): metrics.Counter(counter_name) metrics.Gauge(int_gauge_name, int, fields=[("dimension", str)]) metrics.Event(event_metric_name) metadatas = self.collector.GetAllMetricsMetadata() self.assertEqual(metadatas[counter_name].metric_type, rdf_stats.MetricMetadata.MetricType.COUNTER) self.assertFalse(metadatas[counter_name].fields_defs) self.assertEqual(metadatas[int_gauge_name].metric_type, rdf_stats.MetricMetadata.MetricType.GAUGE) self.assertEqual(metadatas[int_gauge_name].fields_defs, [ rdf_stats.MetricFieldDefinition( field_name="dimension", field_type=rdf_stats.MetricFieldDefinition.FieldType.STR) ]) self.assertEqual(metadatas[event_metric_name].metric_type, rdf_stats.MetricMetadata.MetricType.EVENT) self.assertFalse(metadatas[event_metric_name].fields_defs)
def testRaisesOnImproperFieldsUsage2(self): with self.SetUpStatsCollector(self._CreateStatsCollector): counter = metrics.Counter( "testRaisesOnImproperFieldsUsage2_counter", fields=[("dimension", str)]) int_gauge = metrics.Gauge( "testRaisesOnImproperFieldsUsage2_int_gauge", int, fields=[("dimension", str)]) event_metric = metrics.Event( "testRaisesOnImproperFieldsUsage2_event_metric", fields=[("dimension", str)]) # Check for counters with self.assertRaises(ValueError): counter.GetValue() with self.assertRaises(ValueError): counter.GetValue(fields=["a", "b"]) # Check for gauges with self.assertRaises(ValueError): int_gauge.GetValue() with self.assertRaises(ValueError): int_gauge.GetValue(fields=["a", "b"]) # Check for event metrics with self.assertRaises(ValueError): event_metric.GetValue() with self.assertRaises(ValueError): event_metric.GetValue(fields=["a", "b"])
def testSimpleGauge(self): with self.SetUpStatsCollector(self._CreateStatsCollector): int_gauge = metrics.Gauge("testSimpleGauge_int_gauge", int) float_gauge = metrics.Gauge("testSimpleGauge_float_gauge", float) self.assertEqual(0, int_gauge.GetValue()) self.assertEqual(0.0, float_gauge.GetValue()) int_gauge.SetValue(42) float_gauge.SetValue(42.3) self.assertEqual(42, int_gauge.GetValue()) self.assertAlmostEqual(42.3, float_gauge.GetValue()) # At least default Python type checking is enforced in gauges: # we can't assign string to int with self.assertRaises(ValueError): int_gauge.SetValue("some")
def testGaugeWithFields(self): with self.SetUpStatsCollector(self._CreateStatsCollector()): int_gauge = metrics.Gauge( "testGaugeWithFields_int_gauge", int, fields=[("dimension", str)]) self.assertEqual(0, int_gauge.GetValue(fields=["dimension_value_1"])) self.assertEqual(0, int_gauge.GetValue(fields=["dimesnioN_value_2"])) int_gauge.SetValue(1, fields=["dimension_value_1"]) int_gauge.SetValue(2, fields=["dimension_value_2"]) self.assertEqual(1, int_gauge.GetValue(fields=["dimension_value_1"])) self.assertEqual(2, int_gauge.GetValue(fields=["dimension_value_2"]))
def testRaisesOnImproperFieldsUsage1(self): with self.SetUpStatsCollector(self._CreateStatsCollector()): counter = metrics.Counter("testRaisesOnImproperFieldsUsage1_counter") int_gauge = metrics.Gauge("testRaisesOnImproperFieldsUsage1_int_gauge", int) event_metric = metrics.Event( "testRaisesOnImproperFieldsUsage1_event_metric") # Check for counters with self.assertRaises(ValueError): counter.GetValue(fields=["a"]) # Check for gauges with self.assertRaises(ValueError): int_gauge.GetValue(fields=["a"]) # Check for event metrics with self.assertRaises(ValueError): event_metric.GetValue(fields=["a", "b"])
def testGetMetricFieldsWorksCorrectly(self): with self.SetUpStatsCollector(self._CreateStatsCollector): counter = metrics.Counter( "testGetMetricFieldsWorksCorrectly_counter", fields=[("dimension1", str), ("dimension2", str)]) int_gauge = metrics.Gauge( "testGetMetricFieldsWorksCorrectly_int_gauge", int, fields=[("dimension", str)]) event_metric = metrics.Event( "testGetMetricFieldsWorksCorrectly_event_metric", fields=[("dimension", str)]) counter.Increment(fields=["b", "b"]) counter.Increment(fields=["a", "c"]) self.assertCountEqual([("a", "c"), ("b", "b")], counter.GetFields()) int_gauge.SetValue(20, fields=["a"]) int_gauge.SetValue(30, fields=["b"]) self.assertCountEqual([("a", ), ("b", )], int_gauge.GetFields()) event_metric.RecordEvent(0.1, fields=["a"]) event_metric.RecordEvent(0.1, fields=["b"]) self.assertCountEqual([("a", ), ("b", )], event_metric.GetFields())
from grr_response_core.stats import metrics from grr_response_server import access_control from grr_response_server import communicator from grr_response_server import data_store from grr_response_server import events from grr_response_server import message_handlers from grr_response_server import worker_lib from grr_response_server.databases import db from grr_response_server.flows.general import transfer from grr_response_server.rdfvalues import flow_objects as rdf_flow_objects from grr_response_server.rdfvalues import objects as rdf_objects CLIENT_PINGS_BY_LABEL = metrics.Counter("client_pings_by_label", fields=[("label", str)]) FRONTEND_ACTIVE_COUNT = metrics.Gauge("frontend_active_count", int, fields=[("source", str)]) FRONTEND_MAX_ACTIVE_COUNT = metrics.Gauge("frontend_max_active_count", int) FRONTEND_HTTP_REQUESTS = metrics.Counter("frontend_http_requests", fields=[("action", str), ("protocol", str)]) FRONTEND_IN_BYTES = metrics.Counter("frontend_in_bytes", fields=[("source", str)]) FRONTEND_OUT_BYTES = metrics.Counter("frontend_out_bytes", fields=[("source", str)]) FRONTEND_REQUEST_COUNT = metrics.Counter("frontend_request_count", fields=[("source", str)]) FRONTEND_INACTIVE_REQUEST_COUNT = metrics.Counter( "frontend_inactive_request_count", fields=[("source", str)]) FRONTEND_REQUEST_LATENCY = metrics.Event("frontend_request_latency", fields=[("source", str)])
def testSetGaugeValue(self): with self.SetUpStatsCollector( default_stats_collector.DefaultStatsCollector()): gauge = metrics.Gauge("gfoo", int, fields=[("bar", str)]) with self.assertStatsCounterDelta(42, gauge, fields=["baz"]): gauge.SetValue(42, fields=["baz"])
def testGaugeRegistration(self): with self.SetUpStatsCollector( default_stats_collector.DefaultStatsCollector()): metrics.Gauge("gfoo", int) self.assertIsNotNone(self.collector.GetMetricMetadata("gfoo"))
import logging import queue import threading import time import psutil from grr_response_core.lib import utils from grr_response_core.lib.util import collection from grr_response_core.lib.util import random from grr_response_core.stats import metrics STOP_MESSAGE = "Stop message" THREADPOOL_OUTSTANDING_TASKS = metrics.Gauge("threadpool_outstanding_tasks", int, fields=[("pool_name", str)]) THREADPOOL_THREADS = metrics.Gauge("threadpool_threads", int, fields=[("pool_name", str)]) THREADPOOL_CPU_USE = metrics.Gauge("threadpool_cpu_use", float, fields=[("pool_name", str)]) THREADPOOL_TASK_EXCEPTIONS = metrics.Counter("threadpool_task_exceptions", fields=[("pool_name", str)]) THREADPOOL_WORKING_TIME = metrics.Event("threadpool_working_time", fields=[("pool_name", str)]) THREADPOOL_QUEUEING_TIME = metrics.Event("threadpool_queueing_time", fields=[("pool_name", str)])