示例#1
0
    def Run(self):
        stats_collector = stats.StatsCollector()

        stats_collector.RegisterCounterMetric(
            "sample_counter", docstring="Sample counter metric.")

        stats_collector.RegisterGaugeMetric("sample_gauge_value",
                                            float,
                                            docstring="Sample gauge metric.")

        stats_collector.RegisterEventMetric("sample_event",
                                            docstring="Sample event metric.")

        with utils.Stubber(stats, "STATS", stats_collector):
            for i in range(10):
                with test_lib.FakeTime(42 + i * 60):
                    stats_collector.IncrementCounter("sample_counter")
                    stats_collector.SetGaugeValue("sample_gauge_value",
                                                  i * 0.5)
                    stats_collector.RecordEvent("sample_event", 0.42 + 0.5 * i)

                    with aff4.FACTORY.Create(None,
                                             aff4_stats_store.StatsStore,
                                             mode="w",
                                             token=self.token) as stats_store:
                        stats_store.WriteStats(process_id="worker_1")

        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_counter",
                       start=42000000,
                       end=3600000000))
        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_counter",
                       start=42000000,
                       end=3600000000,
                       rate="1m"))

        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_gauge_value",
                       start=42000000,
                       end=3600000000))

        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_event",
                       start=42000000,
                       end=3600000000))
        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_event",
                       start=42000000,
                       end=3600000000,
                       distribution_handling_mode="DH_COUNT"))
示例#2
0
  def Run(self):
    real_metric_metadata = list(
        itervalues(stats_collector_instance.Get().GetAllMetricsMetadata()))
    test_metadata = real_metric_metadata + [
        stats_utils.CreateCounterMetadata(
            _TEST_COUNTER, docstring="Sample counter metric."),
        stats_utils.CreateGaugeMetadata(
            _TEST_GAUGE_METRIC, float, docstring="Sample gauge metric."),
        stats_utils.CreateEventMetadata(
            _TEST_EVENT_METRIC, docstring="Sample event metric."),
    ]
    stats_collector = default_stats_collector.DefaultStatsCollector(
        test_metadata)
    with stats_test_utils.FakeStatsContext(stats_collector):
      for i in range(10):
        with test_lib.FakeTime(42 + i * 60):
          stats_collector.IncrementCounter(_TEST_COUNTER)
          stats_collector.SetGaugeValue(_TEST_GAUGE_METRIC, i * 0.5)
          stats_collector.RecordEvent(_TEST_EVENT_METRIC, 0.42 + 0.5 * i)

          with aff4.FACTORY.Create(
              None, aff4_stats_store.StatsStore, mode="w",
              token=self.token) as stats_store:
            stats_store.WriteStats(process_id="worker_1")

      range_start = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(42)
      range_end = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(3600)

      self.Check(
          "GetStatsStoreMetric",
          args=stats_plugin.ApiGetStatsStoreMetricArgs(
              component="WORKER",
              metric_name=_TEST_COUNTER,
              start=range_start,
              end=range_end))
      self.Check(
          "GetStatsStoreMetric",
          args=stats_plugin.ApiGetStatsStoreMetricArgs(
              component="WORKER",
              metric_name=_TEST_COUNTER,
              start=range_start,
              end=range_end,
              rate="1m"))

      self.Check(
          "GetStatsStoreMetric",
          args=stats_plugin.ApiGetStatsStoreMetricArgs(
              component="WORKER",
              metric_name=_TEST_GAUGE_METRIC,
              start=range_start,
              end=range_end))

      self.Check(
          "GetStatsStoreMetric",
          args=stats_plugin.ApiGetStatsStoreMetricArgs(
              component="WORKER",
              metric_name=_TEST_EVENT_METRIC,
              start=range_start,
              end=range_end))
      self.Check(
          "GetStatsStoreMetric",
          args=stats_plugin.ApiGetStatsStoreMetricArgs(
              component="WORKER",
              metric_name=_TEST_EVENT_METRIC,
              start=range_start,
              end=range_end,
              distribution_handling_mode="DH_COUNT"))