test_core.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from unittest.mock import patch
  2. from freezegun import freeze_time
  3. from minimetrics import MiniMetricsClient
  4. from minimetrics.core import BucketKey, CounterMetric, DistributionMetric, GaugeMetric, SetMetric
  5. def test_simple():
  6. client = MiniMetricsClient()
  7. client.incr("button_clicked", 1.0)
  8. client.aggregator.stop()
  9. assert len(client.aggregator.buckets) == 0
  10. @freeze_time("2023-09-06 10:00:00")
  11. @patch("minimetrics.core.Aggregator._emit")
  12. def test_client_incr(_emit):
  13. tags = {
  14. "browser": "Chrome",
  15. "browser.version": "1.0",
  16. "user.orgs": ["sentry", "google", "apple"],
  17. "user.classes": ["1", "2", "3"],
  18. }
  19. client = MiniMetricsClient()
  20. client.incr("button_clicked", 1.0, tags=tags) # type:ignore
  21. client.aggregator.stop()
  22. assert len(client.aggregator.buckets) == 0
  23. extracted_metrics_arg = _emit.call_args.args[0]
  24. assert len(extracted_metrics_arg) == 1
  25. assert extracted_metrics_arg[0][0] == BucketKey(
  26. timestamp=1693994400,
  27. metric_type="c",
  28. metric_key="button_clicked",
  29. metric_unit="nanosecond",
  30. metric_tags=(
  31. ("browser", "Chrome"),
  32. ("browser.version", "1.0"),
  33. ("user.classes", "1"),
  34. ("user.classes", "2"),
  35. ("user.classes", "3"),
  36. ("user.orgs", "apple"),
  37. ("user.orgs", "google"),
  38. ("user.orgs", "sentry"),
  39. ),
  40. )
  41. assert isinstance(extracted_metrics_arg[0][1], CounterMetric)
  42. assert extracted_metrics_arg[0][1].serialize_value() == 1
  43. @freeze_time("2023-09-06 10:00:00")
  44. @patch("minimetrics.core.Aggregator._emit")
  45. def test_client_timing(_emit):
  46. tags = {
  47. "browser": "Chrome",
  48. "browser.version": "1.0",
  49. "user.orgs": ["sentry", "google", "apple"],
  50. "user.classes": ["1", "2", "3"],
  51. }
  52. client = MiniMetricsClient()
  53. client.timing("execution_time", 1.0, tags=tags) # type:ignore
  54. client.aggregator.stop()
  55. assert len(client.aggregator.buckets) == 0
  56. extracted_metrics_arg = _emit.call_args.args[0]
  57. assert len(extracted_metrics_arg) == 1
  58. assert extracted_metrics_arg[0][0] == BucketKey(
  59. timestamp=1693994400,
  60. metric_type="d",
  61. metric_key="execution_time",
  62. metric_unit="second",
  63. metric_tags=(
  64. ("browser", "Chrome"),
  65. ("browser.version", "1.0"),
  66. ("user.classes", "1"),
  67. ("user.classes", "2"),
  68. ("user.classes", "3"),
  69. ("user.orgs", "apple"),
  70. ("user.orgs", "google"),
  71. ("user.orgs", "sentry"),
  72. ),
  73. )
  74. assert isinstance(extracted_metrics_arg[0][1], DistributionMetric)
  75. assert extracted_metrics_arg[0][1].serialize_value() == [1.0]
  76. assert len(client.aggregator.buckets) == 0
  77. @freeze_time("2023-09-06 10:00:00")
  78. @patch("minimetrics.core.Aggregator._emit")
  79. def test_client_set(_emit):
  80. tags = {
  81. "browser": "Chrome",
  82. "browser.version": "1.0",
  83. "user.orgs": ["sentry", "google", "apple"],
  84. "user.classes": ["1", "2", "3"],
  85. }
  86. client = MiniMetricsClient()
  87. client.set("user", "riccardo", tags=tags) # type:ignore
  88. client.aggregator.stop()
  89. assert len(client.aggregator.buckets) == 0
  90. extracted_metrics_arg = _emit.call_args.args[0]
  91. assert len(extracted_metrics_arg) == 1
  92. assert extracted_metrics_arg[0][0] == BucketKey(
  93. timestamp=1693994400,
  94. metric_type="s",
  95. metric_key="user",
  96. metric_unit=None,
  97. metric_tags=(
  98. ("browser", "Chrome"),
  99. ("browser.version", "1.0"),
  100. ("user.classes", "1"),
  101. ("user.classes", "2"),
  102. ("user.classes", "3"),
  103. ("user.orgs", "apple"),
  104. ("user.orgs", "google"),
  105. ("user.orgs", "sentry"),
  106. ),
  107. )
  108. assert isinstance(extracted_metrics_arg[0][1], SetMetric)
  109. assert extracted_metrics_arg[0][1].serialize_value() == [3455635177]
  110. assert len(client.aggregator.buckets) == 0
  111. @freeze_time("2023-09-06 10:00:00")
  112. @patch("minimetrics.core.Aggregator._emit")
  113. def test_client_gauge(_emit):
  114. tags = {
  115. "browser": "Chrome",
  116. "browser.version": "1.0",
  117. "user.orgs": ["sentry", "google", "apple"],
  118. "user.classes": ["1", "2", "3"],
  119. }
  120. client = MiniMetricsClient()
  121. client.gauge("frontend_time", 15.0, tags=tags) # type:ignore
  122. client.aggregator.stop()
  123. assert len(client.aggregator.buckets) == 0
  124. extracted_metrics_arg = _emit.call_args.args[0]
  125. assert len(extracted_metrics_arg) == 1
  126. assert extracted_metrics_arg[0][0] == BucketKey(
  127. timestamp=1693994400,
  128. metric_type="g",
  129. metric_key="frontend_time",
  130. metric_unit="second",
  131. metric_tags=(
  132. ("browser", "Chrome"),
  133. ("browser.version", "1.0"),
  134. ("user.classes", "1"),
  135. ("user.classes", "2"),
  136. ("user.classes", "3"),
  137. ("user.orgs", "apple"),
  138. ("user.orgs", "google"),
  139. ("user.orgs", "sentry"),
  140. ),
  141. )
  142. assert isinstance(extracted_metrics_arg[0][1], GaugeMetric)
  143. assert extracted_metrics_arg[0][1].serialize_value() == {
  144. "last": 15.0,
  145. "min": 15.0,
  146. "max": 15.0,
  147. "sum": 15.0,
  148. "count": 1,
  149. }
  150. assert len(client.aggregator.buckets) == 0