def test_invalid_metric_names(self) -> None:
     for invalid_metric_name in TestData.INVALID_STRS:
         with pytest.raises(ValueError):
             statsd_format(
                 metric_name=invalid_metric_name,
                 value=TestData.VALUE,
                 typ="c",
             )
 def test_basic_counter(self) -> None:
     result = statsd_format(
         metric_name=TestData.METRIC_NAME,
         value=TestData.VALUE,
         typ="c",
     )
     assert result == "some_metric:2.0|c"
 def test_non_counter_with_sample_rate_doesnt_include_it(self) -> None:
     result = statsd_format(
         metric_name=TestData.METRIC_NAME,
         value=TestData.VALUE,
         sample_rate=TestData.SAMPLE_RATE,
         typ="g",
     )
     assert result == "some_metric:2.0|g"
 def test_counter_with_sample_rate(self) -> None:
     result = statsd_format(
         metric_name=TestData.METRIC_NAME,
         value=TestData.VALUE,
         sample_rate=TestData.SAMPLE_RATE,
         typ="c",
     )
     assert result == "some_metric:2.0|c|@0.5"
 def test_tags(self) -> None:
     result = statsd_format(
         metric_name=TestData.METRIC_NAME,
         value=TestData.VALUE,
         sample_rate=TestData.SAMPLE_RATE,
         typ="ms",
         tags=TestData.TAGS,
     )
     assert result == "some_metric:2.0|ms|#key:value,key2:value2"
Example #6
0
 def write_metric(
         self,
         metric_name: str,
         value: Union[int, float],
         typ: MetricType,
         sample_rate: float = DEFAULT_SAMPLE_RATE,
         tags: Sequence[TagPair] = (),
 ) -> None:
     now = self.utc_now()
     now_ts = self._format_time_for_cloudwatch(now)
     statsd = statsd_format(
         metric_name,
         value,
         typ,
         sample_rate,
         tags,
     )
     self.out.write(f"MONITORING|{self.service_name}|{now_ts}|{statsd}\n")