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 all commits
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,29 @@
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 formatSpanDescription(desc: string) {
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
}
}

let description = desc;

Check warning on line 25 in packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx#L15-L25

Added lines #L15 - L25 were not covered by tests
if (desc.match(/^(SELECT|INSERT|UPDATE|DELETE|TRUNCATE|ALTER) /i)) {
try {
return formatSQL(desc.replace(/([\s,(])(%[a-z])([\s,)])/gim, '$1?$3'));
description = formatSQL(desc.replace(/([\s,(])(%[a-z])([\s,)])/gim, '$1?$3'), { language: dbType || 'sql' });

Check warning on line 28 in packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx#L28

Added line #L28 was not covered by tests
} catch (err) {
console.error(err);
}
}
return desc;

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

Check warning on line 34 in packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx#L33-L34

Added lines #L33 - L34 were not covered by tests
}

function formatValue(name: string, value: unknown): ReactNode {
Expand All @@ -29,6 +42,36 @@
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>
);
}

Check warning on line 73 in packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx#L45-L73

Added lines #L45 - L73 were not covered by tests

export default function SpanDetails({
traceContext,
span,
Expand Down Expand Up @@ -102,28 +145,7 @@
</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} />

Check warning on line 148 in packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx

View check run for this annotation

Codecov / codecov/patch

packages/overlay/src/integrations/sentry/components/traces/spans/SpanDetails.tsx#L148

Added line #L148 was not covered by tests

<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