123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- from unittest.mock import patch
- from freezegun import freeze_time
- from minimetrics.core import CounterMetric, DistributionMetric, MiniMetricsClient, SetMetric
- from sentry.testutils.pytest.fixtures import django_db_all
- @django_db_all
- def test_envelope_forwarding():
- client = MiniMetricsClient()
- client.incr("button_clicked", 1.0)
- client.aggregator.stop()
- assert len(client.aggregator.buckets) == 0
- @freeze_time("2023-09-06 10:00:00")
- @patch("minimetrics.core.Aggregator._emit")
- def test_client_incr(_emit):
- tags = {
- "browser": "Chrome",
- "browser.version": "1.0",
- "user.orgs": ["sentry", "google", "apple"],
- "user.classes": ["1", "2", "3"],
- }
- client = MiniMetricsClient()
- client.incr("button_clicked", 1.0, tags=tags) # type:ignore
- client.aggregator.stop()
- assert len(client.aggregator.buckets) == 0
- extracted_metrics_arg = list(_emit.call_args.args[0])
- assert len(extracted_metrics_arg) == 1
- assert extracted_metrics_arg[0][0] == (
- 1693994400,
- "c",
- "button_clicked",
- "nanosecond",
- (
- ("browser", "Chrome"),
- ("browser.version", "1.0"),
- ("user.classes", "1"),
- ("user.classes", "2"),
- ("user.classes", "3"),
- ("user.orgs", "apple"),
- ("user.orgs", "google"),
- ("user.orgs", "sentry"),
- ),
- )
- assert isinstance(extracted_metrics_arg[0][1], CounterMetric)
- assert list(extracted_metrics_arg[0][1].serialize_value()) == [1]
- @freeze_time("2023-09-06 10:00:00")
- @patch("minimetrics.core.Aggregator._emit")
- def test_client_timing(_emit):
- tags = {
- "browser": "Chrome",
- "browser.version": "1.0",
- "user.orgs": ["sentry", "google", "apple"],
- "user.classes": ["1", "2", "3"],
- }
- client = MiniMetricsClient()
- client.timing("execution_time", 1.0, tags=tags) # type:ignore
- client.aggregator.stop()
- assert len(client.aggregator.buckets) == 0
- extracted_metrics_arg = list(_emit.call_args.args[0])
- assert len(extracted_metrics_arg) == 1
- assert extracted_metrics_arg[0][0] == (
- 1693994400,
- "d",
- "execution_time",
- "second",
- (
- ("browser", "Chrome"),
- ("browser.version", "1.0"),
- ("user.classes", "1"),
- ("user.classes", "2"),
- ("user.classes", "3"),
- ("user.orgs", "apple"),
- ("user.orgs", "google"),
- ("user.orgs", "sentry"),
- ),
- )
- assert isinstance(extracted_metrics_arg[0][1], DistributionMetric)
- assert list(extracted_metrics_arg[0][1].serialize_value()) == [1.0]
- assert len(client.aggregator.buckets) == 0
- @freeze_time("2023-09-06 10:00:00")
- @patch("minimetrics.core.Aggregator._emit")
- def test_client_set(_emit):
- tags = {
- "browser": "Chrome",
- "browser.version": "1.0",
- "user.orgs": ["sentry", "google", "apple"],
- "user.classes": ["1", "2", "3"],
- }
- client = MiniMetricsClient()
- client.set("user", "riccardo", tags=tags) # type:ignore
- client.aggregator.stop()
- assert len(client.aggregator.buckets) == 0
- extracted_metrics_arg = list(_emit.call_args.args[0])
- assert len(extracted_metrics_arg) == 1
- assert extracted_metrics_arg[0][0] == (
- 1693994400,
- "s",
- "user",
- "none",
- (
- ("browser", "Chrome"),
- ("browser.version", "1.0"),
- ("user.classes", "1"),
- ("user.classes", "2"),
- ("user.classes", "3"),
- ("user.orgs", "apple"),
- ("user.orgs", "google"),
- ("user.orgs", "sentry"),
- ),
- )
- assert isinstance(extracted_metrics_arg[0][1], SetMetric)
- assert list(extracted_metrics_arg[0][1].serialize_value()) == [3455635177]
- assert len(client.aggregator.buckets) == 0
- @freeze_time("2023-09-06 10:00:00")
- @patch("minimetrics.core.Aggregator._emit")
- def test_client_gauge_as_counter(_emit):
- tags = {
- "browser": "Chrome",
- "browser.version": "1.0",
- "user.orgs": ["sentry", "google", "apple"],
- "user.classes": ["1", "2", "3"],
- }
- client = MiniMetricsClient()
- client.gauge("frontend_time", 15.0, tags=tags) # type:ignore
- client.aggregator.stop()
- assert len(client.aggregator.buckets) == 0
- extracted_metrics_arg = list(_emit.call_args.args[0])
- assert len(extracted_metrics_arg) == 1
- assert extracted_metrics_arg[0][0] == (
- 1693994400,
- "c",
- "frontend_time",
- "second",
- (
- ("browser", "Chrome"),
- ("browser.version", "1.0"),
- ("user.classes", "1"),
- ("user.classes", "2"),
- ("user.classes", "3"),
- ("user.orgs", "apple"),
- ("user.orgs", "google"),
- ("user.orgs", "sentry"),
- ),
- )
- assert isinstance(extracted_metrics_arg[0][1], CounterMetric)
- assert list(extracted_metrics_arg[0][1].serialize_value()) == [15.0]
- assert len(client.aggregator.buckets) == 0
- # @freeze_time("2023-09-06 10:00:00")
- # @patch("minimetrics.core.Aggregator._emit")
- # def test_client_gauge(_emit):
- # tags = {
- # "browser": "Chrome",
- # "browser.version": "1.0",
- # "user.orgs": ["sentry", "google", "apple"],
- # "user.classes": ["1", "2", "3"],
- # }
- # client = MiniMetricsClient()
- # client.gauge("frontend_time", 15.0, tags=tags) # type:ignore
- # client.aggregator.stop()
- #
- # assert len(client.aggregator.buckets) == 0
- # extracted_metrics_arg = _emit.call_args.args[0]
- # assert len(extracted_metrics_arg) == 1
- # assert extracted_metrics_arg[0][0] == BucketKey(
- # timestamp=1693994400,
- # metric_type="g",
- # metric_name="frontend_time",
- # metric_unit="second",
- # metric_tags=(
- # ("browser", "Chrome"),
- # ("browser.version", "1.0"),
- # ("user.classes", "1"),
- # ("user.classes", "2"),
- # ("user.classes", "3"),
- # ("user.orgs", "apple"),
- # ("user.orgs", "google"),
- # ("user.orgs", "sentry"),
- # ),
- # )
- # assert isinstance(extracted_metrics_arg[0][1], GaugeMetric)
- # assert extracted_metrics_arg[0][1].serialize_value() == {
- # "last": 15.0,
- # "min": 15.0,
- # "max": 15.0,
- # "sum": 15.0,
- # "count": 1,
- # }
- # assert len(client.aggregator.buckets) == 0
|