test_core.py 6.4 KB

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