-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathspanDescription.tsx
150 lines (130 loc) · 4.63 KB
/
spanDescription.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {Fragment, useMemo} from 'react';
import styled from '@emotion/styled';
import {CodeSnippet} from 'sentry/components/codeSnippet';
import LoadingIndicator from 'sentry/components/loadingIndicator';
import {space} from 'sentry/styles/space';
import {SQLishFormatter} from 'sentry/utils/sqlish/SQLishFormatter';
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import {useSpansIndexed} from 'sentry/views/insights/common/queries/useDiscover';
import {useFullSpanFromTrace} from 'sentry/views/insights/common/queries/useFullSpanFromTrace';
import {
MissingFrame,
StackTraceMiniFrame,
} from 'sentry/views/insights/database/components/stackTraceMiniFrame';
import {SupportedDatabaseSystem} from 'sentry/views/insights/database/utils/constants';
import {
isValidJson,
prettyPrintJsonString,
} from 'sentry/views/insights/database/utils/jsonUtils';
import type {SpanIndexedFieldTypes} from 'sentry/views/insights/types';
import {SpanIndexedField} from 'sentry/views/insights/types';
interface Props {
groupId: SpanIndexedFieldTypes[SpanIndexedField.SPAN_GROUP];
op: SpanIndexedFieldTypes[SpanIndexedField.SPAN_OP];
preliminaryDescription?: string;
}
export function SpanDescription(props: Props) {
const {op, preliminaryDescription} = props;
if (op.startsWith('db')) {
return <DatabaseSpanDescription {...props} />;
}
return <WordBreak>{preliminaryDescription ?? ''}</WordBreak>;
}
const formatter = new SQLishFormatter();
export function DatabaseSpanDescription({
groupId,
preliminaryDescription,
}: Omit<Props, 'op'>) {
const {data: indexedSpans, isFetching: areIndexedSpansLoading} = useSpansIndexed(
{
search: MutableSearch.fromQueryObject({'span.group': groupId}),
limit: 1,
fields: [
SpanIndexedField.PROJECT_ID,
SpanIndexedField.TRANSACTION_ID,
SpanIndexedField.SPAN_DESCRIPTION,
],
},
'api.starfish.span-description'
);
const indexedSpan = indexedSpans?.[0];
// NOTE: We only need this for `span.data`! If this info existed in indexed spans, we could skip it
const {data: rawSpan, isFetching: isRawSpanLoading} = useFullSpanFromTrace(
groupId,
[INDEXED_SPAN_SORT],
Boolean(indexedSpan)
);
const system = rawSpan?.data?.['db.system'];
const formattedDescription = useMemo(() => {
const rawDescription =
rawSpan?.description || indexedSpan?.['span.description'] || preliminaryDescription;
if (system === SupportedDatabaseSystem.MONGODB) {
let bestDescription = '';
if (
rawSpan?.sentry_tags?.description &&
isValidJson(rawSpan.sentry_tags.description)
) {
bestDescription = rawSpan.sentry_tags.description;
} else if (preliminaryDescription && isValidJson(preliminaryDescription)) {
bestDescription = preliminaryDescription;
} else if (
indexedSpan?.['span.description'] &&
isValidJson(indexedSpan?.['span.description'])
) {
bestDescription = indexedSpan?.['span.description'];
} else if (rawSpan?.description && isValidJson(rawSpan.description)) {
bestDescription = rawSpan?.description;
} else {
return rawDescription ?? 'N/A';
}
return prettyPrintJsonString(bestDescription);
}
return formatter.toString(rawDescription ?? '');
}, [preliminaryDescription, rawSpan, indexedSpan, system]);
return (
<Frame>
{areIndexedSpansLoading || isRawSpanLoading ? (
<WithPadding>
<LoadingIndicator mini />
</WithPadding>
) : (
<CodeSnippet language={system === 'mongodb' ? 'json' : 'sql'} isRounded={false}>
{formattedDescription ?? ''}
</CodeSnippet>
)}
{!areIndexedSpansLoading && !isRawSpanLoading && (
<Fragment>
{rawSpan?.data?.['code.filepath'] ? (
<StackTraceMiniFrame
projectId={indexedSpan?.project_id?.toString()}
eventId={indexedSpan?.['transaction.id']}
frame={{
filename: rawSpan?.data?.['code.filepath'],
lineNo: rawSpan?.data?.['code.lineno'],
function: rawSpan?.data?.['code.function'],
}}
/>
) : (
<MissingFrame system={system} />
)}
</Fragment>
)}
</Frame>
);
}
const INDEXED_SPAN_SORT = {
field: 'span.self_time',
kind: 'desc' as const,
};
export const Frame = styled('div')`
border: solid 1px ${p => p.theme.border};
border-radius: ${p => p.theme.borderRadius};
overflow: hidden;
`;
const WithPadding = styled('div')`
display: flex;
padding: ${space(1)} ${space(2)};
`;
const WordBreak = styled('div')`
word-break: break-word;
`;