continuousProfileProvider.tsx 3.6 KB

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