Skip to content

Commit

Permalink
feat: Add request and module details to context (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK and Shubhdeep12 authored Nov 5, 2024
1 parent ab5181c commit 2e4d90c
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 94 deletions.
6 changes: 6 additions & 0 deletions .changeset/khaki-snails-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@spotlightjs/spotlight': minor
'@spotlightjs/overlay': minor
---

Add request and module details to context with JSONViewer
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dist-ssr
*.sln
*.sw?

.astro
.yalc
yalc.lock
.vite-inspect
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/astro/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
1 change: 1 addition & 0 deletions packages/astro/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
71 changes: 71 additions & 0 deletions packages/overlay/src/components/JsonViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import ReactJson from 'react-json-view';

// Need this separately to fix Storybook 8 bundling
// See #419 and #420 for more context
const noop = () => {};

function shouldCollapse({ src, type }: { src: Array<unknown> | object; type: string }) {
if (type === 'object') return Object.keys(src).length > 10;
if (type === 'array') return (src as Array<unknown>).length > 10;
return false;
}

export default function JsonViewer({
data,
onUpdateData = noop,
editingEnabled = false,
clipboardEnabled = true,
displayDataTypes = false,
quotesOnKeys = false,
name = null,
collapseStringsAfterLength = 80,
}: {
data: object;
onUpdateData?: (value: unknown) => void;
editingEnabled?: boolean;
clipboardEnabled?: boolean;
displayDataTypes?: boolean;
quotesOnKeys?: boolean;
name?: string | null | false;
collapseStringsAfterLength?: number;
}) {
return (
<ReactJson
theme="bright"
displayDataTypes={displayDataTypes}
quotesOnKeys={quotesOnKeys}
shouldCollapse={shouldCollapse}
collapseStringsAfterLength={collapseStringsAfterLength}
name={name}
src={data}
enableClipboard={clipboardEnabled}
onEdit={
editingEnabled &&
(e => {
if (e.new_value === 'error') {
return false;
}
onUpdateData(e.updated_src);
})
}
onDelete={
editingEnabled &&
(e => {
if (e.new_value === 'error') {
return false;
}
onUpdateData(e.updated_src);
})
}
onAdd={
editingEnabled &&
(e => {
if (e.new_value === 'error') {
return false;
}
onUpdateData(e.updated_src);
})
}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Envelope } from '@sentry/types';
import { useState } from 'react';
import type { RawEventContext } from '~/integrations/integration';
import SidePanel, { SidePanelHeader } from '~/ui/SidePanel';
import JsonViewer from './JsonViewer';
import JsonViewer from '../../../../components/JsonViewer';

export default function EnvelopeDetails({ data }: { data: { envelope: Envelope; rawEnvelope: RawEventContext } }) {
const [showRawJSON, setShowRawJSON] = useState<boolean>(false);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SentryEvent } from '../../types';
import { Nullable } from 'vitest';
import JsonViewer from '../../../../components/JsonViewer';
import type { SentryEvent } from '../../types';
import Tags from '../Tags';

const EXAMPLE_CONTEXT = `Sentry.setContext("character", {
Expand All @@ -8,11 +10,21 @@ const EXAMPLE_CONTEXT = `Sentry.setContext("character", {
});`;

export default function EventContexts({ event }: { event: SentryEvent }) {
const contexts = { extra: event.extra, ...event.contexts };
const contexts: Record<string, Nullable<Record<string, unknown>>> = {
request: event.request,
...event.contexts,
};
if (event.extra) {
contexts.extra = event.extra;
}
if (event.modules) {
contexts.extra = Object.assign(contexts.extra || {}, { modules: event.modules });
}
const contextEntries = Object.entries(contexts).filter(entry => entry[1]) as [string, Record<string, unknown>][];

const tags = event.tags;
const { tags } = event;

if ((!contexts || !Object.values(contexts).some(v => v)) && !tags) {
if (contextEntries.length === 0 && !tags) {
return (
<div className="space-y-4 px-6">
<div className="text-primary-300">
Expand All @@ -31,29 +43,29 @@ export default function EventContexts({ event }: { event: SentryEvent }) {
</div>
)}
<div className="space-y-6">
{Object.entries(contexts).map(([ctxKey, ctxValues]) =>
ctxValues ? (
<div key={ctxKey}>
<h2 className="font-bold uppercase">{ctxKey}</h2>
<table className="w-full">
<tbody>
{Object.entries(ctxValues).map(([key, value]) => (
<tr key={key}>
<th className="text-primary-300 w-1/12 py-0.5 pr-4 text-left font-mono font-normal">
<div className="w-full truncate">{key}</div>
</th>
<td className="py-0.5">
<pre className="text-primary-300 whitespace-nowrap font-mono">
{JSON.stringify(value, undefined, 2)}
</pre>
</td>
</tr>
))}
</tbody>
</table>
</div>
) : null,
)}
{contextEntries.map(([ctxKey, ctxValues]) => (
<div key={ctxKey}>
<h2 className="font-bold uppercase">{ctxKey}</h2>
<table className="w-full">
<tbody>
{Object.entries(ctxValues).map(([key, value]) => (
<tr key={`${ctxKey}-${key}`}>
<th className="text-primary-300 w-1/12 py-0.5 pr-4 text-left font-mono font-normal">
<div className="w-full truncate">{key}</div>
</th>
<td className="py-0.5">
{typeof value !== 'object' || !value ? (
<pre className="text-primary-300 whitespace-nowrap font-mono">{JSON.stringify(value)}</pre>
) : (
<JsonViewer key={`${ctxKey}-${key}`} data={value} />
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
))}
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CardList from '../../../../components/CardList';
import TimeSince from '../../../../components/TimeSince';
import { useSentryEvents } from '../../data/useSentryEvents';
import { useSentryHelpers } from '../../data/useSentryHelpers';
import { SentryEvent } from '../../types';
import type { SentryEvent } from '../../types';
import HiddenItemsButton from '../HiddenItemsButton';
import PlatformIcon from '../PlatformIcon';
import { EventSummary } from './Event';
Expand Down
4 changes: 3 additions & 1 deletion packages/overlay/src/integrations/sentry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ type CommonEventAttrs = {
start_timestamp?: number;
contexts?: Contexts;
tags?: Tags;
extra?: { [key: string]: string | number };
extra?: Record<string, string | number>;
request?: Record<string, Record<string, string> | string>;
modules?: Record<string, string>;
sdk?: Sdk;
measurements?: Measurements;
};
Expand Down

0 comments on commit 2e4d90c

Please sign in to comment.