Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(span): Use JsonViewer for JSON-like queries such as MongoDB #563

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-foxes-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spotlightjs/overlay': minor
---

Add support for JSON-like queries (MongoDB) while improving span details page a bit
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Link } from 'react-router-dom';
import { ReactComponent as Sort } from '~/assets/sort.svg';
import { ReactComponent as SortDown } from '~/assets/sortDown.svg';
import classNames from '~/lib/classNames';
import { QUERIES_HEADERS, QUERIES_SORT_KEYS } from '../../constants';
import { DB_SPAN_REGEX, QUERIES_HEADERS, QUERIES_SORT_KEYS } from '../../constants';
import { useSentrySpans } from '../../data/useSentrySpans';
import type { Span } from '../../types';
import { getFormattedDuration } from '../../utils/duration';

const DB_SPAN_REGEX = /^db(\.[A-Za-z]+)?$/;

type QueryInfo = {
avgDuration: number;
timeSpent: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@ import { getDuration } from '../../../utils/duration';
import DateTime from '../../DateTime';
import { ErrorTitle } from '../../events/error/Error';
import SpanTree from './SpanTree';
import JsonViewer from '../../../../../components/JsonViewer';
import { DB_SPAN_REGEX } from '../../../constants';

function DBSpanDescription({ desc, dbType }: { desc: string; dbType?: string }) {
if (desc.startsWith('{') || dbType === 'mongodb') {
// looks like JSON?
try {
return <JsonViewer data={JSON.parse(desc)} />;
} catch (_err) {
// pass
}
}

function formatSpanDescription(desc: string) {
if (desc.match(/^(SELECT|INSERT|UPDATE|DELETE|TRUNCATE|ALTER) /i)) {
try {
return formatSQL(desc.replace(/([\s,(])(%[a-z])([\s,)])/gim, '$1?$3'));
desc = formatSQL(desc.replace(/([\s,(])(%[a-z])([\s,)])/gim, '$1?$3'), { language: dbType || 'sql' });
} catch (err) {
console.error(err);
}
}
return desc;

return <pre className="text-primary-300 whitespace-pre-wrap break-words font-mono text-sm">{desc}</pre>;
}

function formatValue(name: string, value: unknown): ReactNode {
Expand All @@ -29,6 +41,36 @@ function formatValue(name: string, value: unknown): ReactNode {
return `${value}` as ReactNode;
}

function SpanDescription({ span }: { span: Span }) {
let body = null;
let headerText = null;
if (span.op && DB_SPAN_REGEX.test(span.op) && span.description) {
headerText = 'Query';
body = <DBSpanDescription desc={span.description} dbType={span.data?.['db.system'] as string} />;
} else if (span.op === 'resource.img' && span.description?.indexOf('/') === 0) {
headerText = 'Preview';
body = (
<a
href={span.description}
className="border-primary-950 hover:border-primary-700 -m-2 inline-block max-w-sm cursor-pointer rounded border p-1"
>
<img src={span.description} alt="preview" style={{ maxHeight: 300 }} />
</a>
);
} else if (span.description) {
headerText = 'Description';
body = <pre className="text-primary-300 whitespace-pre-wrap break-words font-mono text-sm">{span.description}</pre>;
} else {
body = <div className="text-primary-300">No description recorded for this span.</div>;
}
return (
<div>
{headerText && <h2 className="mb-2 font-bold uppercase">{headerText}</h2>}
{body}
</div>
);
}

export default function SpanDetails({
traceContext,
span,
Expand Down Expand Up @@ -102,28 +144,7 @@ export default function SpanDetails({
</div>
)}

<div>
<h2 className="mb-2 font-bold uppercase">Description</h2>
{span.description ? (
<pre className="text-primary-300 whitespace-pre-wrap break-words font-mono text-sm">
{formatSpanDescription(span.description)}
</pre>
) : (
<div className="text-primary-300">No description recorded for this span.</div>
)}
</div>

{span.op === 'resource.img' && span.description?.indexOf('/') === 0 && (
<div>
<h2 className="mb-2 font-bold uppercase">Preview</h2>
<a
href={span.description}
className="border-primary-950 hover:border-primary-700 -m-2 inline-block max-w-sm cursor-pointer rounded border p-1"
>
<img src={span.description} alt="preview" style={{ maxHeight: 300 }} />
</a>
</div>
)}
<SpanDescription span={span} />

<div>
<h2 className="mb-2 font-bold uppercase">Tags</h2>
Expand Down
2 changes: 2 additions & 0 deletions packages/overlay/src/integrations/sentry/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const DB_SPAN_REGEX = /^db(\.[A-Za-z]+)?$/;

export const RESOURCES_SORT_KEYS = {
avgDuration: 'AVG_DURATION',
timeSpent: 'TIME_SPENT',
Expand Down
Loading