gapSpanDetails.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import {useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import emptyStateImg from 'sentry-images/spot/profiling-empty-state.svg';
  4. import {Button} from 'sentry/components/button';
  5. import {SectionHeading} from 'sentry/components/charts/styles';
  6. import ExternalLink from 'sentry/components/links/externalLink';
  7. import {FlamegraphPreview} from 'sentry/components/profiling/flamegraph/flamegraphPreview';
  8. import QuestionTooltip from 'sentry/components/questionTooltip';
  9. import {t, tct} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {Organization} from 'sentry/types';
  12. import {EventTransaction} from 'sentry/types/event';
  13. import {defined} from 'sentry/utils';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import {CanvasView} from 'sentry/utils/profiling/canvasView';
  16. import {colorComponentsToRGBA} from 'sentry/utils/profiling/colors/utils';
  17. import {Flamegraph as FlamegraphModel} from 'sentry/utils/profiling/flamegraph';
  18. import {FlamegraphThemeProvider} from 'sentry/utils/profiling/flamegraph/flamegraphThemeProvider';
  19. import {useFlamegraphTheme} from 'sentry/utils/profiling/flamegraph/useFlamegraphTheme';
  20. import {getProfilingDocsForPlatform} from 'sentry/utils/profiling/platforms';
  21. import {generateProfileFlamechartRouteWithQuery} from 'sentry/utils/profiling/routes';
  22. import {Rect} from 'sentry/utils/profiling/speedscope';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import {useProfileGroup} from 'sentry/views/profiling/profileGroupProvider';
  26. import InlineDocs from './inlineDocs';
  27. import {GapSpanType} from './types';
  28. interface GapSpanDetailsProps {
  29. event: Readonly<EventTransaction>;
  30. resetCellMeasureCache: () => void;
  31. span: Readonly<GapSpanType>;
  32. }
  33. export function GapSpanDetails({
  34. event,
  35. resetCellMeasureCache,
  36. span,
  37. }: GapSpanDetailsProps) {
  38. const {projects} = useProjects();
  39. const project = useMemo(
  40. () => projects.find(p => p.id === event.projectID),
  41. [projects, event]
  42. );
  43. const organization = useOrganization();
  44. const [canvasView, setCanvasView] = useState<CanvasView<FlamegraphModel> | null>(null);
  45. const profileGroup = useProfileGroup();
  46. const profile = useMemo(() => {
  47. const threadId = profileGroup.profiles[profileGroup.activeProfileIndex]?.threadId;
  48. if (!defined(threadId)) {
  49. return null;
  50. }
  51. return profileGroup.profiles.find(p => p.threadId === threadId) ?? null;
  52. }, [profileGroup.profiles, profileGroup.activeProfileIndex]);
  53. const transactionHasProfile = defined(profile);
  54. const flamegraph = useMemo(() => {
  55. if (!transactionHasProfile) {
  56. return FlamegraphModel.Example();
  57. }
  58. return new FlamegraphModel(profile, {});
  59. }, [transactionHasProfile, profile]);
  60. // The most recent profile formats should contain a timestamp indicating
  61. // the beginning of the profile. This timestamp can be after the start
  62. // timestamp on the transaction, so we need to account for the gap and
  63. // make sure the relative start timestamps we compute for the span is
  64. // relative to the start of the profile.
  65. //
  66. // If the profile does not contain a timestamp, we fall back to using the
  67. // start timestamp on the transaction. This won't be as accurate but it's
  68. // the next best thing.
  69. const startTimestamp = profile?.timestamp ?? event.startTimestamp;
  70. const relativeStartTimestamp = transactionHasProfile
  71. ? span.start_timestamp - startTimestamp
  72. : 0;
  73. const relativeStopTimestamp = transactionHasProfile
  74. ? span.timestamp - startTimestamp
  75. : flamegraph.configSpace.width;
  76. // Found the profile, render the preview
  77. if (transactionHasProfile) {
  78. return (
  79. <FlamegraphThemeProvider>
  80. <div>
  81. <ProfilePreviewHeader
  82. canvasView={canvasView}
  83. event={event}
  84. organization={organization}
  85. />
  86. <ProfilePreviewLegend />
  87. <FlamegraphContainer>
  88. <FlamegraphPreview
  89. flamegraph={flamegraph}
  90. relativeStartTimestamp={relativeStartTimestamp}
  91. relativeStopTimestamp={relativeStopTimestamp}
  92. updateFlamegraphView={setCanvasView}
  93. />
  94. </FlamegraphContainer>
  95. <ManualInstrumentationInstruction />
  96. </div>
  97. </FlamegraphThemeProvider>
  98. );
  99. }
  100. // The event's platform is more accurate than the project
  101. // so use that first and fall back to the project's platform
  102. const docsLink =
  103. getProfilingDocsForPlatform(event.platform) ??
  104. (project && getProfilingDocsForPlatform(project.platform));
  105. // This project has received a profile before so they've already
  106. // set up profiling. No point showing the profiling setup again.
  107. if (!docsLink || project?.hasProfiles) {
  108. return (
  109. <InlineDocs
  110. orgSlug={organization.slug}
  111. platform={event.sdk?.name || ''}
  112. projectSlug={event?.projectSlug ?? project?.slug ?? ''}
  113. resetCellMeasureCache={resetCellMeasureCache}
  114. />
  115. );
  116. }
  117. // At this point we must have a project on a supported
  118. // platform that has not setup profiling yet
  119. return (
  120. <Container>
  121. <Image src={emptyStateImg} />
  122. <InstructionsContainer>
  123. <h5>{t('With Profiling, we could paint a better picture')}</h5>
  124. <p>
  125. {t(
  126. 'Profiles can give you additional context on which functions are sampled at the same time of these spans.'
  127. )}
  128. </p>
  129. <Button size="sm" priority="primary" href={docsLink} external>
  130. {t('Set Up Profiling')}
  131. </Button>
  132. <ManualInstrumentationInstruction />
  133. </InstructionsContainer>
  134. </Container>
  135. );
  136. }
  137. function ManualInstrumentationInstruction() {
  138. return (
  139. <SubText>
  140. {tct(
  141. `You can also [docLink:manually instrument] certain regions of your code to see span details for future transactions.`,
  142. {
  143. docLink: (
  144. <ExternalLink href="https://docs.sentry.io/product/performance/getting-started/" />
  145. ),
  146. }
  147. )}
  148. </SubText>
  149. );
  150. }
  151. interface ProfilePreviewProps {
  152. canvasView: CanvasView<FlamegraphModel> | null;
  153. event: Readonly<EventTransaction>;
  154. organization: Organization;
  155. }
  156. function ProfilePreviewHeader({canvasView, event, organization}: ProfilePreviewProps) {
  157. const profileId = event.contexts.profile?.profile_id || '';
  158. // we want to try to go straight to the same config view as the preview
  159. const query = canvasView?.configView
  160. ? {
  161. // TODO: this assumes that profile start timestamp == transaction timestamp
  162. fov: Rect.encode(canvasView.configView),
  163. // the flamechart persists some preferences to local storage,
  164. // force these settings so the view is the same as the preview
  165. view: 'top down',
  166. type: 'flamechart',
  167. }
  168. : undefined;
  169. const target = generateProfileFlamechartRouteWithQuery({
  170. orgSlug: organization.slug,
  171. projectSlug: event?.projectSlug ?? '',
  172. profileId,
  173. query,
  174. });
  175. function handleGoToProfile() {
  176. trackAnalytics('profiling_views.go_to_flamegraph', {
  177. organization,
  178. source: 'performance.missing_instrumentation',
  179. });
  180. }
  181. return (
  182. <HeaderContainer>
  183. <HeaderContainer>
  184. <SectionHeading>{t('Related Profile')}</SectionHeading>
  185. <QuestionTooltip
  186. position="top"
  187. size="sm"
  188. containerDisplayMode="block"
  189. title={t(
  190. 'This profile was collected concurrently with the transaction. It displays the relevant stacks and functions for the duration of this span.'
  191. )}
  192. />
  193. </HeaderContainer>
  194. <Button size="xs" onClick={handleGoToProfile} to={target}>
  195. {t('View Profile')}
  196. </Button>
  197. </HeaderContainer>
  198. );
  199. }
  200. function ProfilePreviewLegend() {
  201. const theme = useFlamegraphTheme();
  202. const applicationFrameColor = colorComponentsToRGBA(
  203. theme.COLORS.FRAME_APPLICATION_COLOR
  204. );
  205. const systemFrameColor = colorComponentsToRGBA(theme.COLORS.FRAME_SYSTEM_COLOR);
  206. return (
  207. <LegendContainer>
  208. <LegendItem>
  209. <LegendMarker color={applicationFrameColor} />
  210. {t('Application Function')}
  211. </LegendItem>
  212. <LegendItem>
  213. <LegendMarker color={systemFrameColor} />
  214. {t('System Function')}
  215. </LegendItem>
  216. </LegendContainer>
  217. );
  218. }
  219. const Container = styled('div')`
  220. display: flex;
  221. gap: ${space(2)};
  222. justify-content: space-between;
  223. flex-direction: column;
  224. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  225. flex-direction: row-reverse;
  226. }
  227. `;
  228. const InstructionsContainer = styled('div')`
  229. > p {
  230. margin: 0;
  231. }
  232. display: flex;
  233. gap: ${space(3)};
  234. flex-direction: column;
  235. align-items: start;
  236. `;
  237. const Image = styled('img')`
  238. user-select: none;
  239. width: 420px;
  240. align-self: center;
  241. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  242. width: 300px;
  243. }
  244. @media (min-width: ${p => p.theme.breakpoints.large}) {
  245. width: 380px;
  246. }
  247. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  248. width: 420px;
  249. }
  250. `;
  251. const FlamegraphContainer = styled('div')`
  252. height: 300px;
  253. margin-top: ${space(1)};
  254. margin-bottom: ${space(1)};
  255. `;
  256. const LegendContainer = styled('div')`
  257. display: flex;
  258. flex-direction: row;
  259. gap: ${space(1.5)};
  260. `;
  261. const LegendItem = styled('span')`
  262. display: flex;
  263. flex-direction: row;
  264. align-items: center;
  265. gap: ${space(0.5)};
  266. color: ${p => p.theme.subText};
  267. font-size: ${p => p.theme.fontSizeSmall};
  268. `;
  269. const LegendMarker = styled('span')<{color: string}>`
  270. display: inline-block;
  271. width: 12px;
  272. height: 12px;
  273. border-radius: 1px;
  274. background-color: ${p => p.color};
  275. `;
  276. const HeaderContainer = styled('div')`
  277. display: flex;
  278. flex-direction: row;
  279. align-items: center;
  280. justify-content: space-between;
  281. gap: ${space(1)};
  282. `;
  283. const SubText = styled('p')`
  284. color: ${p => p.theme.subText};
  285. `;