performanceAtScaleContext.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {createContext, useEffect, useState} from 'react';
  2. import {trackAnalytics} from 'sentry/utils/analytics';
  3. import {useMEPDataContext} from 'sentry/utils/performance/contexts/metricsEnhancedPerformanceDataContext';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. type TransactionListTableData =
  6. | {
  7. empty: boolean;
  8. query: string;
  9. }
  10. | undefined;
  11. export type PerformanceAtScaleContextProps = {
  12. metricsSeriesDataEmpty: boolean | undefined;
  13. setMetricsSeriesDataEmpty: (data: boolean | undefined) => void;
  14. setTransactionListTableData: (data: TransactionListTableData) => void;
  15. transactionListTableData: TransactionListTableData;
  16. };
  17. export const PerformanceAtScaleContext = createContext<
  18. PerformanceAtScaleContextProps | undefined
  19. >(undefined);
  20. type ProviderProps = {
  21. children: React.ReactNode;
  22. };
  23. export function PerformanceAtScaleContextProvider({children}: ProviderProps) {
  24. const [metricsSeriesDataEmpty, setMetricsSeriesDataEmpty] = useState<
  25. boolean | undefined
  26. >(false);
  27. const [transactionListTableData, setTransactionListTableData] =
  28. useState<TransactionListTableData>(undefined);
  29. const mepContext = useMEPDataContext();
  30. const organization = useOrganization();
  31. const query = transactionListTableData?.query ?? '';
  32. const transactionListTableEmpty = transactionListTableData?.empty;
  33. const isMetricsData = mepContext?.isMetricsData ?? false;
  34. useEffect(() => {
  35. // We only want to collect analytics events if we have metrics data
  36. // and if everything is dynamically samples
  37. if (!isMetricsData || !organization.isDynamicallySampled) {
  38. return;
  39. }
  40. // if the chart or the transaction list table are undefined,
  41. // some loading is probably still hapenning
  42. if (metricsSeriesDataEmpty === undefined || transactionListTableEmpty === undefined) {
  43. return;
  44. }
  45. // metricsSeriesDataEmpty comes from the series data response (events-stats request)
  46. // and if we don't have metrics data, we don't want to fire analytics events
  47. if (metricsSeriesDataEmpty) {
  48. return;
  49. }
  50. // If the transaction list table is empty, we want to fire the no_samples event
  51. // as it means that there is a gap in the dynamic sampling and we want to track that
  52. if (transactionListTableEmpty) {
  53. trackAnalytics('dynamic_sampling_transaction_summary.no_samples', {
  54. organization,
  55. query,
  56. });
  57. }
  58. // If the transaction list table is not empty and there are metrics, it means that
  59. // dynamic sampling is working properly and there is no gap
  60. trackAnalytics('dynamic_sampling_transaction_summary.baseline', {
  61. organization,
  62. query,
  63. });
  64. }, [
  65. metricsSeriesDataEmpty,
  66. isMetricsData,
  67. query,
  68. transactionListTableEmpty,
  69. organization,
  70. ]);
  71. return (
  72. <PerformanceAtScaleContext.Provider
  73. value={{
  74. metricsSeriesDataEmpty,
  75. setMetricsSeriesDataEmpty,
  76. setTransactionListTableData,
  77. transactionListTableData,
  78. }}
  79. >
  80. {children}
  81. </PerformanceAtScaleContext.Provider>
  82. );
  83. }