flamegraphProvider.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {createContext, useContext, useMemo} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {Flamegraph} from 'sentry/utils/profiling/flamegraph';
  4. import {useFlamegraphPreferences} from 'sentry/utils/profiling/flamegraph/hooks/useFlamegraphPreferences';
  5. import {useFlamegraphProfiles} from 'sentry/utils/profiling/flamegraph/hooks/useFlamegraphProfiles';
  6. import {useProfileGroup} from 'sentry/views/profiling/profileGroupProvider';
  7. const LOADING_OR_FALLBACK_FLAMEGRAPH = Flamegraph.Empty();
  8. const FlamegraphContext = createContext<Flamegraph | null>(null);
  9. export const useFlamegraph = () => {
  10. const context = useContext(FlamegraphContext);
  11. if (!context) {
  12. throw new Error('useFlamegraph was called outside of FlamegraphProvider');
  13. }
  14. return context;
  15. };
  16. interface FlamegraphProviderProps {
  17. children: React.ReactNode;
  18. }
  19. export function FlamegraphProvider(props: FlamegraphProviderProps) {
  20. const profileGroup = useProfileGroup();
  21. const {threadId} = useFlamegraphProfiles();
  22. const {sorting, view} = useFlamegraphPreferences();
  23. const flamegraph = useMemo(() => {
  24. if (typeof threadId !== 'number') {
  25. return LOADING_OR_FALLBACK_FLAMEGRAPH;
  26. }
  27. // This could happen if threadId was initialized from query string, but for some
  28. // reason the profile was removed from the list of profiles.
  29. const activeProfile = profileGroup.profiles.find(p => p.threadId === threadId);
  30. if (!activeProfile) {
  31. return LOADING_OR_FALLBACK_FLAMEGRAPH;
  32. }
  33. const transaction = Sentry.startTransaction({
  34. op: 'import',
  35. name: 'flamegraph.constructor',
  36. });
  37. transaction.setTag('sorting', sorting.split(' ').join('_'));
  38. transaction.setTag('view', view.split(' ').join('_'));
  39. const newFlamegraph = new Flamegraph(activeProfile, {
  40. inverted: view === 'bottom up',
  41. sort: sorting,
  42. configSpace: undefined,
  43. });
  44. transaction.finish();
  45. return newFlamegraph;
  46. }, [sorting, threadId, view, profileGroup]);
  47. return (
  48. <FlamegraphContext.Provider value={flamegraph}>
  49. {props.children}
  50. </FlamegraphContext.Provider>
  51. );
  52. }