|
| 1 | +import {lazy, Suspense, useMemo} from 'react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | +import type {LocationDescriptor} from 'history'; |
| 4 | + |
| 5 | +import Link from 'sentry/components/links/link'; |
| 6 | +import {generateTraceTarget} from 'sentry/components/quickTrace/utils'; |
| 7 | +import type {Event} from 'sentry/types/event'; |
| 8 | +import type {Organization} from 'sentry/types/organization'; |
| 9 | +import {defined} from 'sentry/utils'; |
| 10 | +import {trackAnalytics} from 'sentry/utils/analytics'; |
| 11 | +import {useLocation} from 'sentry/utils/useLocation'; |
| 12 | +import {useIssuesTraceTree} from 'sentry/views/performance/newTraceDetails/traceApi/useIssuesTraceTree'; |
| 13 | +import {useTrace} from 'sentry/views/performance/newTraceDetails/traceApi/useTrace'; |
| 14 | +import {useTraceMeta} from 'sentry/views/performance/newTraceDetails/traceApi/useTraceMeta'; |
| 15 | +import {useTraceRootEvent} from 'sentry/views/performance/newTraceDetails/traceApi/useTraceRootEvent'; |
| 16 | +import {TraceViewSources} from 'sentry/views/performance/newTraceDetails/traceHeader/breadcrumbs'; |
| 17 | +import { |
| 18 | + loadTraceViewPreferences, |
| 19 | + type TracePreferencesState, |
| 20 | +} from 'sentry/views/performance/newTraceDetails/traceState/tracePreferences'; |
| 21 | +import {TraceStateProvider} from 'sentry/views/performance/newTraceDetails/traceState/traceStateProvider'; |
| 22 | +import {useTraceEventView} from 'sentry/views/performance/newTraceDetails/useTraceEventView'; |
| 23 | +import {useTraceQueryParams} from 'sentry/views/performance/newTraceDetails/useTraceQueryParams'; |
| 24 | + |
| 25 | +const LazyIssuesTraceWaterfall = lazy(() => |
| 26 | + import('sentry/views/performance/newTraceDetails/issuesTraceWaterfall').then( |
| 27 | + module => ({default: module.IssuesTraceWaterfall}) |
| 28 | + ) |
| 29 | +); |
| 30 | + |
| 31 | +function getHrefFromTraceTarget(traceTarget: LocationDescriptor) { |
| 32 | + if (typeof traceTarget === 'string') { |
| 33 | + return traceTarget; |
| 34 | + } |
| 35 | + |
| 36 | + const searchParams = new URLSearchParams(); |
| 37 | + for (const key in traceTarget.query) { |
| 38 | + if (defined(traceTarget.query[key])) { |
| 39 | + searchParams.append(key, traceTarget.query[key]); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return `${traceTarget.pathname}?${searchParams.toString()}`; |
| 44 | +} |
| 45 | + |
| 46 | +const DEFAULT_ISSUE_DETAILS_TRACE_VIEW_PREFERENCES: TracePreferencesState = { |
| 47 | + drawer: { |
| 48 | + minimized: true, |
| 49 | + sizes: { |
| 50 | + 'drawer left': 0.33, |
| 51 | + 'drawer right': 0.33, |
| 52 | + 'drawer bottom': 0.4, |
| 53 | + 'trace context height': 150, |
| 54 | + }, |
| 55 | + layoutOptions: [], |
| 56 | + }, |
| 57 | + missing_instrumentation: true, |
| 58 | + autogroup: { |
| 59 | + parent: true, |
| 60 | + sibling: true, |
| 61 | + }, |
| 62 | + layout: 'drawer bottom', |
| 63 | + list: { |
| 64 | + width: 0.5, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +interface SpanEvidenceTraceViewProps { |
| 69 | + event: Event; |
| 70 | + organization: Organization; |
| 71 | + traceId: string; |
| 72 | +} |
| 73 | + |
| 74 | +export function SpanEvidenceTraceView({ |
| 75 | + event, |
| 76 | + organization, |
| 77 | + traceId, |
| 78 | +}: SpanEvidenceTraceViewProps) { |
| 79 | + const timestamp = new Date(event.dateReceived).getTime() / 1e3; |
| 80 | + |
| 81 | + const location = useLocation(); |
| 82 | + const trace = useTrace({ |
| 83 | + timestamp, |
| 84 | + traceSlug: traceId, |
| 85 | + limit: 10000, |
| 86 | + }); |
| 87 | + const meta = useTraceMeta([{traceSlug: traceId, timestamp}]); |
| 88 | + const tree = useIssuesTraceTree({trace, meta, replay: null}); |
| 89 | + |
| 90 | + const shouldLoadTraceRoot = !trace.isPending && trace.data; |
| 91 | + |
| 92 | + const rootEvent = useTraceRootEvent(shouldLoadTraceRoot ? trace.data : null); |
| 93 | + const preferences = useMemo( |
| 94 | + () => |
| 95 | + loadTraceViewPreferences('issue-details-trace-view-preferences') || |
| 96 | + DEFAULT_ISSUE_DETAILS_TRACE_VIEW_PREFERENCES, |
| 97 | + [] |
| 98 | + ); |
| 99 | + |
| 100 | + const params = useTraceQueryParams({timestamp}); |
| 101 | + const traceEventView = useTraceEventView(traceId, params); |
| 102 | + |
| 103 | + if (!traceId) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + const traceTarget = generateTraceTarget( |
| 108 | + event, |
| 109 | + organization, |
| 110 | + { |
| 111 | + ...location, |
| 112 | + query: { |
| 113 | + ...location.query, |
| 114 | + groupId: event.groupID, |
| 115 | + }, |
| 116 | + }, |
| 117 | + TraceViewSources.ISSUE_DETAILS |
| 118 | + ); |
| 119 | + |
| 120 | + return ( |
| 121 | + <TraceStateProvider |
| 122 | + initialPreferences={preferences} |
| 123 | + preferencesStorageKey="issue-details-view-preferences" |
| 124 | + > |
| 125 | + <IssuesTraceContainer> |
| 126 | + <Suspense fallback={null}> |
| 127 | + <LazyIssuesTraceWaterfall |
| 128 | + tree={tree} |
| 129 | + trace={trace} |
| 130 | + traceSlug={traceId} |
| 131 | + rootEvent={rootEvent} |
| 132 | + organization={organization} |
| 133 | + traceEventView={traceEventView} |
| 134 | + meta={meta} |
| 135 | + source="issues" |
| 136 | + replay={null} |
| 137 | + event={event} |
| 138 | + /> |
| 139 | + </Suspense> |
| 140 | + <IssuesTraceOverlayContainer |
| 141 | + to={getHrefFromTraceTarget(traceTarget)} |
| 142 | + onClick={() => { |
| 143 | + trackAnalytics('issue_details.view_full_trace_waterfall_clicked', { |
| 144 | + organization, |
| 145 | + }); |
| 146 | + }} |
| 147 | + /> |
| 148 | + </IssuesTraceContainer> |
| 149 | + </TraceStateProvider> |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +const IssuesTraceContainer = styled('div')` |
| 154 | + position: relative; |
| 155 | +`; |
| 156 | + |
| 157 | +const IssuesTraceOverlayContainer = styled(Link)` |
| 158 | + position: absolute; |
| 159 | + inset: 0; |
| 160 | + z-index: 10; |
| 161 | +`; |
0 commit comments