test_core.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. emit_args = list(_emit.call_args.args[0])
  25. assert len(emit_args) == 1
  26. assert emit_args[0][0] == 1693994400
  27. keys = list(emit_args[0][1].keys())
  28. assert keys == [
  29. (
  30. "c",
  31. "button_clicked",
  32. "nanosecond",
  33. (
  34. ("browser", "Chrome"),
  35. ("browser.version", "1.0"),
  36. ("user.classes", "1"),
  37. ("user.classes", "2"),
  38. ("user.classes", "3"),
  39. ("user.orgs", "apple"),
  40. ("user.orgs", "google"),
  41. ("user.orgs", "sentry"),
  42. ),
  43. )
  44. ]
  45. values = list(emit_args[0][1].values())
  46. assert isinstance(values[0], CounterMetric)
  47. assert list(values[0].serialize_value()) == [1]
  48. @freeze_time("2023-09-06 10:00:00")
  49. @patch("minimetrics.core.Aggregator._emit")
  50. def test_client_timing(_emit):
  51. tags = {
  52. "browser": "Chrome",
  53. "browser.version": "1.0",
  54. "user.orgs": ["sentry", "google", "apple"],
  55. "user.classes": ["1", "2", "3"],
  56. }
  57. client = MiniMetricsClient()
  58. client.timing("execution_time", 1.0, tags=tags) # type:ignore
  59. client.aggregator.stop()
  60. assert len(client.aggregator.buckets) == 0
  61. emit_args = list(_emit.call_args.args[0])
  62. assert len(emit_args) == 1
  63. assert emit_args[0][0] == 1693994400
  64. keys = list(emit_args[0][1].keys())
  65. assert keys == [
  66. (
  67. "d",
  68. "execution_time",
  69. "second",
  70. (
  71. ("browser", "Chrome"),
  72. ("browser.version", "1.0"),
  73. ("user.classes", "1"),
  74. ("user.classes", "2"),
  75. ("user.classes", "3"),
  76. ("user.orgs", "apple"),
  77. ("user.orgs", "google"),
  78. ("user.orgs", "sentry"),
  79. ),
  80. )
  81. ]
  82. values = list(emit_args[0][1].values())
  83. assert isinstance(values[0], DistributionMetric)
  84. assert list(values[0].serialize_value()) == [1.0]
  85. @freeze_time("2023-09-06 10:00:00")
  86. @patch("minimetrics.core.Aggregator._emit")
  87. def test_client_set(_emit):
  88. tags = {
  89. "browser": "Chrome",
  90. "browser.version": "1.0",
  91. "user.orgs": ["sentry", "google", "apple"],
  92. "user.classes": ["1", "2", "3"],
  93. }
  94. client = MiniMetricsClient()
  95. client.set("user", "riccardo", tags=tags) # type:ignore
  96. client.aggregator.stop()
  97. assert len(client.aggregator.buckets) == 0
  98. emit_args = list(_emit.call_args.args[0])
  99. assert len(emit_args) == 1
  100. assert emit_args[0][0] == 1693994400
  101. keys = list(emit_args[0][1].keys())
  102. assert keys == [
  103. (
  104. "s",
  105. "user",
  106. "none",
  107. (
  108. ("browser", "Chrome"),
  109. ("browser.version", "1.0"),
  110. ("user.classes", "1"),
  111. ("user.classes", "2"),
  112. ("user.classes", "3"),
  113. ("user.orgs", "apple"),
  114. ("user.orgs", "google"),
  115. ("user.orgs", "sentry"),
  116. ),
  117. )
  118. ]
  119. values = list(emit_args[0][1].values())
  120. assert isinstance(values[0], SetMetric)
  121. assert list(values[0].serialize_value()) == [3455635177]
  122. @freeze_time("2023-09-06 10:00:00")
  123. @patch("minimetrics.core.Aggregator._emit")
  124. def test_client_gauge_as_counter(_emit):
  125. tags = {
  126. "browser": "Chrome",
  127. "browser.version": "1.0",
  128. "user.orgs": ["sentry", "google", "apple"],
  129. "user.classes": ["1", "2", "3"],
  130. }
  131. client = MiniMetricsClient()
  132. client.gauge("frontend_time", 15.0, tags=tags) # type:ignore
  133. client.aggregator.stop()
  134. assert len(client.aggregator.buckets) == 0
  135. emit_args = list(_emit.call_args.args[0])
  136. assert len(emit_args) == 1
  137. assert emit_args[0][0] == 1693994400
  138. keys = list(emit_args[0][1].keys())
  139. assert keys == [
  140. (
  141. "c",
  142. "frontend_time",
  143. "second",
  144. (
  145. ("browser", "Chrome"),
  146. ("browser.version", "1.0"),
  147. ("user.classes", "1"),
  148. ("user.classes", "2"),
  149. ("user.classes", "3"),
  150. ("user.orgs", "apple"),
  151. ("user.orgs", "google"),
  152. ("user.orgs", "sentry"),
  153. ),
  154. )
  155. ]
  156. values = list(emit_args[0][1].values())
  157. assert isinstance(values[0], CounterMetric)
  158. assert list(values[0].serialize_value()) == [15.0]