test_core.py 6.7 KB

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