continuousProfileProvider.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {createContext, useContext, useLayoutEffect, useMemo, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import * as qs from 'query-string';
  4. import type {Client} from 'sentry/api';
  5. import {ContinuousProfileHeader} from 'sentry/components/profiling/continuousProfileHeader';
  6. import type {RequestState} from 'sentry/types/core';
  7. import type {EventTransaction} from 'sentry/types/event';
  8. import type {Organization} from 'sentry/types/organization';
  9. import type {Project} from 'sentry/types/project';
  10. import {useSentryEvent} from 'sentry/utils/profiling/hooks/useSentryEvent';
  11. import useApi from 'sentry/utils/useApi';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import {useParams} from 'sentry/utils/useParams';
  14. import useProjects from 'sentry/utils/useProjects';
  15. interface ContinuousProfileQueryParams {
  16. end: string;
  17. profiler_id: string;
  18. start: string;
  19. }
  20. function fetchContinuousProfileFlamegraph(
  21. api: Client,
  22. query: ContinuousProfileQueryParams,
  23. projectSlug: Project['slug'],
  24. orgSlug: Organization['slug']
  25. ): Promise<Profiling.ProfileInput> {
  26. return api
  27. .requestPromise(`/organizations/${orgSlug}/profiling/chunks/`, {
  28. method: 'GET',
  29. query: {
  30. ...query,
  31. project: projectSlug,
  32. },
  33. includeAllArgs: true,
  34. })
  35. .then(([data]) => data.chunk);
  36. }
  37. type ContinuousProfileProviderValue = RequestState<Profiling.ProfileInput>;
  38. export const ContinuousProfileContext =
  39. createContext<ContinuousProfileProviderValue | null>(null);
  40. export function useContinuousProfile() {
  41. const context = useContext(ContinuousProfileContext);
  42. if (!context) {
  43. throw new Error('useContinuousProfile was called outside of ProfileProvider');
  44. }
  45. return context;
  46. }
  47. type ContinuousProfileSegmentProviderValue = RequestState<EventTransaction>;
  48. export const ContinuousProfileSegmentContext =
  49. createContext<ContinuousProfileSegmentProviderValue | null>(null);
  50. export function useContinuousProfileSegment() {
  51. const context = useContext(ContinuousProfileSegmentContext);
  52. if (!context) {
  53. throw new Error(
  54. 'useContinuousProfileSegment was called outside of ContinuousProfileSegmentProvider'
  55. );
  56. }
  57. return context;
  58. }
  59. function isValidDate(date: string): boolean {
  60. return !isNaN(Date.parse(date));
  61. }
  62. function getContinuousChunkQueryParams(
  63. query: string
  64. ): ContinuousProfileQueryParams | null {
  65. const queryString = new URLSearchParams(query);
  66. const start = queryString.get('start');
  67. const end = queryString.get('end');
  68. const profiler_id = queryString.get('profilerId');
  69. if (!start || !end || !profiler_id) {
  70. return null;
  71. }
  72. if (!isValidDate(start) || !isValidDate(end)) {
  73. return null;
  74. }
  75. return {
  76. start,
  77. end,
  78. profiler_id,
  79. };
  80. }
  81. interface ContinuousFlamegraphViewProps {
  82. children: React.ReactNode;
  83. }
  84. function ContinuousProfileProvider(
  85. props: ContinuousFlamegraphViewProps
  86. ): React.ReactElement {
  87. const api = useApi();
  88. const params = useParams();
  89. const organization = useOrganization();
  90. const projects = useProjects();
  91. const [profiles, setProfiles] = useState<RequestState<Profiling.ProfileInput>>({
  92. type: 'initial',
  93. });
  94. useLayoutEffect(() => {
  95. if (!params.projectId) {
  96. return undefined;
  97. }
  98. const chunkParams = getContinuousChunkQueryParams(window.location.search);
  99. const project = projects.projects.find(p => p.slug === params.projectId);
  100. if (!chunkParams) {
  101. Sentry.captureMessage(
  102. 'Failed to fetch continuous profile - invalid query parameters.'
  103. );
  104. return undefined;
  105. }
  106. if (!project) {
  107. Sentry.captureMessage('Failed to fetch continuous profile - project not found.');
  108. return undefined;
  109. }
  110. setProfiles({type: 'loading'});
  111. fetchContinuousProfileFlamegraph(api, chunkParams, project.id, organization.slug)
  112. .then(p => {
  113. setProfiles({type: 'resolved', data: p});
  114. })
  115. .catch(err => {
  116. setProfiles({type: 'errored', error: 'Failed to fetch profiles'});
  117. Sentry.captureException(err);
  118. });
  119. return () => api.clear();
  120. }, [api, organization.slug, projects.projects, params.projectId]);
  121. const eventPayload = useMemo(() => {
  122. const query = qs.parse(window.location.search);
  123. return {
  124. project: projects.projects.find(p => p.slug === params.projectId),
  125. eventId: query.eventId as string,
  126. };
  127. }, [projects, params.projectId]);
  128. const profileTransaction = useSentryEvent<EventTransaction>(
  129. organization.slug,
  130. eventPayload.project?.id ?? '',
  131. eventPayload.eventId
  132. );
  133. return (
  134. <ContinuousProfileContext.Provider value={profiles}>
  135. <ContinuousProfileSegmentContext.Provider value={profileTransaction}>
  136. <ContinuousProfileHeader />
  137. {props.children}
  138. </ContinuousProfileSegmentContext.Provider>
  139. </ContinuousProfileContext.Provider>
  140. );
  141. }
  142. export default ContinuousProfileProvider;