traceAnalytics.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import * as Sentry from '@sentry/react';
  2. import type {Organization} from 'sentry/types/organization';
  3. import type {Project} from 'sentry/types/project';
  4. import {trackAnalytics} from 'sentry/utils/analytics';
  5. import type {TraceTree} from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  6. import type {TraceType} from 'sentry/views/performance/newTraceDetails/traceType';
  7. const trackTraceMetadata = (
  8. tree: TraceTree,
  9. projects: Project[],
  10. organization: Organization
  11. ) => {
  12. Sentry.metrics.increment(`trace.trace_shape.${tree.shape}`);
  13. // space[1] represents the node duration (in milliseconds)
  14. const trace_duration_seconds = (tree.root.space?.[1] ?? 0) / 1000;
  15. const projectSlugs = [
  16. ...new Set(
  17. tree.list.map(node => node.metadata.project_slug).filter(slug => slug !== undefined)
  18. ),
  19. ];
  20. const projectPlatforms = projects
  21. .filter(p => projectSlugs.includes(p.slug))
  22. .map(project => project?.platform ?? '');
  23. trackAnalytics('trace.metadata', {
  24. shape: tree.shape,
  25. // round trace_duration_seconds to nearest two decimal places
  26. trace_duration_seconds: Math.round(trace_duration_seconds * 100) / 100,
  27. num_root_children: tree.root.children.length,
  28. num_nodes: tree.list.length,
  29. project_platforms: projectPlatforms,
  30. organization,
  31. });
  32. };
  33. const trackFailedToFetchTraceState = () =>
  34. Sentry.metrics.increment('trace.failed_to_fetch_trace');
  35. const trackEmptyTraceState = () => Sentry.metrics.increment('trace.empty_trace');
  36. const trackLayoutChange = (layout: string, organization: Organization) =>
  37. trackAnalytics('trace.trace_layout.change', {
  38. layout,
  39. organization,
  40. });
  41. const trackDrawerMinimize = (organization: Organization) =>
  42. trackAnalytics('trace.trace_layout.drawer_minimize', {
  43. organization,
  44. });
  45. const trackShowInView = (organization: Organization) =>
  46. trackAnalytics('trace.trace_layout.show_in_view', {
  47. organization,
  48. });
  49. const trackViewEventJSON = (organization: Organization) =>
  50. trackAnalytics('trace.trace_layout.view_event_json', {
  51. organization,
  52. });
  53. const trackTabPin = (organization: Organization) =>
  54. trackAnalytics('trace.trace_layout.tab_pin', {
  55. organization,
  56. });
  57. const trackTabView = (tab: string, organization: Organization) =>
  58. trackAnalytics('trace.trace_layout.tab_view', {
  59. organization,
  60. tab,
  61. });
  62. const trackSearchFocus = (organization: Organization) =>
  63. trackAnalytics('trace.trace_layout.search_focus', {
  64. organization,
  65. });
  66. const trackResetZoom = (organization: Organization) =>
  67. trackAnalytics('trace.trace_layout.reset_zoom', {
  68. organization,
  69. });
  70. const trackPerformanceSetupChecklistTriggered = (organization: Organization) =>
  71. trackAnalytics('trace.quality.performance_setup.checklist_triggered', {
  72. organization,
  73. });
  74. const trackPerformanceSetupBannerLoaded = (organization: Organization) =>
  75. trackAnalytics('trace.quality.performance_setup.banner_loaded', {
  76. organization,
  77. });
  78. const trackQuotaExceededIncreaseBudgetClicked = (
  79. organization: Organization,
  80. traceType: string
  81. ) =>
  82. trackAnalytics('trace.quality.quota_exceeded.increase_budget_clicked', {
  83. organization,
  84. traceType,
  85. });
  86. const trackQuotaExceededLearnMoreClicked = (
  87. organization: Organization,
  88. traceType: string
  89. ) =>
  90. trackAnalytics('trace.quality.quota_exceeded.learn_more_clicked', {
  91. organization,
  92. traceType,
  93. });
  94. const trackQuotaExceededBannerLoaded = (organization: Organization, traceType: string) =>
  95. trackAnalytics('trace.quality.quota_exceeded.banner_loaded', {
  96. organization,
  97. traceType,
  98. });
  99. const trackPerformanceSetupLearnMoreClicked = (organization: Organization) =>
  100. trackAnalytics('trace.quality.performance_setup.learn_more_clicked', {
  101. organization,
  102. });
  103. const trackViewShortcuts = (organization: Organization) =>
  104. trackAnalytics('trace.trace_layout.view_shortcuts', {
  105. organization,
  106. });
  107. const trackTraceWarningType = (type: TraceType, organization: Organization) =>
  108. trackAnalytics('trace.trace_warning_type', {
  109. organization,
  110. type,
  111. });
  112. const traceAnalytics = {
  113. // Trace shape
  114. trackTraceMetadata,
  115. trackEmptyTraceState,
  116. trackFailedToFetchTraceState,
  117. // Drawer actions
  118. trackShowInView,
  119. trackViewEventJSON,
  120. // Layout actions
  121. trackLayoutChange,
  122. trackDrawerMinimize,
  123. trackSearchFocus,
  124. trackTabPin,
  125. trackTabView,
  126. // Toolbar actions
  127. trackResetZoom,
  128. trackViewShortcuts,
  129. trackTraceWarningType,
  130. // Trace Quality Improvement
  131. trackPerformanceSetupChecklistTriggered,
  132. trackPerformanceSetupLearnMoreClicked,
  133. trackPerformanceSetupBannerLoaded,
  134. trackQuotaExceededIncreaseBudgetClicked,
  135. trackQuotaExceededLearnMoreClicked,
  136. trackQuotaExceededBannerLoaded,
  137. };
  138. export {traceAnalytics};