Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {screen, waitFor} from 'sentry-test/reactTestingLibrary';
import {OrganizationFixture} from 'sentry-fixture/organization';

import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';

import {SeerMarkdown} from 'sentry/components/seer/markdown';

import {renderEmbed, renderEmbedMarkdown} from './resourceEmbedTestUtils';
import {TraceWaterfall} from './traceWaterfall';
Expand Down Expand Up @@ -66,6 +70,37 @@ describe('traceWaterfall embed', () => {
});
});

it('requests web vitals from trace metrics when flagged', async () => {
mockTraceRequests();
const metricsRequest = MockApiClient.addMockResponse({
url: '/organizations/org-slug/events/',
match: [MockApiClient.matchQuery({dataset: 'tracemetrics'})],
body: {data: []},
});

render(
<SeerMarkdown
raw={`{% traceWaterfall %}${JSON.stringify({traceId, timestamp})}{% /traceWaterfall %}`}
/>,
{
organization: OrganizationFixture({
features: ['dashboards-web-vitals-trace-metrics'],
}),
}
);

await waitFor(() => expect(metricsRequest).toHaveBeenCalled());

expect(metricsRequest).toHaveBeenCalledWith(
'/organizations/org-slug/events/',
expect.objectContaining({
query: expect.objectContaining({
query: expect.stringContaining(`trace:${traceId}`),
}),
})
);
});

it('does not let the host page query string steer the trace fetch', async () => {
// `useTrace`/`useTraceMeta` parse `location.search` themselves, so opting the waterfall out of
// URL sync is not enough — the fetches need their own opt-out or the host page's event and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {useTrace} from 'sentry/views/performance/traceDetails/traceApi/useTrace'
import {useTraceMeta} from 'sentry/views/performance/traceDetails/traceApi/useTraceMeta';
import {useTraceRootEvent} from 'sentry/views/performance/traceDetails/traceApi/useTraceRootEvent';
import {useTraceTree} from 'sentry/views/performance/traceDetails/traceApi/useTraceTree';
import {useTraceWebVitalMetrics} from 'sentry/views/performance/traceDetails/traceApi/useTraceWebVitalMetrics';
import {
DEFAULT_TRACE_VIEW_PREFERENCES,
type TracePreferencesState,
Expand Down Expand Up @@ -80,7 +81,12 @@ function TraceWaterfallEmbed({
{traceSlug: traceId, timestamp: timestampSeconds},
{disableUrlSync: true}
);
const tree = useTraceTree({trace, replay: null});
const webVitalMetrics = useTraceWebVitalMetrics({
enabled: organization.features.includes('dashboards-web-vitals-trace-metrics'),
queryParams,
traceSlug: traceId,
});
const tree = useTraceTree({trace, replay: null, webVitalMetrics});
const rootEventResults = useTraceRootEvent({
tree,
logs: undefined,
Expand Down
27 changes: 27 additions & 0 deletions static/app/views/explore/replays/detail/trace/trace.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,31 @@ describe('NewTraceView', () => {
})
);
});

it('requests web vitals from trace metrics when flagged', async () => {
MockApiClient.addMockResponse({
url: '/organizations/org-slug/trace/trace1/',
body: [],
});
const metricsRequest = MockApiClient.addMockResponse({
url: '/organizations/org-slug/events/',
match: [MockApiClient.matchQuery({dataset: 'tracemetrics'})],
body: {data: []},
});

render(<NewTraceView replay={replayRecord} />, {
organization: OrganizationFixture({
features: ['dashboards-web-vitals-trace-metrics'],
}),
});

await waitFor(() => expect(metricsRequest).toHaveBeenCalled());

expect(metricsRequest).toHaveBeenCalledWith(
'/organizations/org-slug/events/',
expect.objectContaining({
query: expect.objectContaining({query: expect.stringContaining('trace:trace1')}),
})
);
});
});
15 changes: 14 additions & 1 deletion static/app/views/explore/replays/detail/trace/trace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {useReplayTraceMeta} from 'sentry/views/performance/traceDetails/traceApi
import {useTrace} from 'sentry/views/performance/traceDetails/traceApi/useTrace';
import {useTraceRootEvent} from 'sentry/views/performance/traceDetails/traceApi/useTraceRootEvent';
import {useTraceTree} from 'sentry/views/performance/traceDetails/traceApi/useTraceTree';
import {useTraceWebVitalMetrics} from 'sentry/views/performance/traceDetails/traceApi/useTraceWebVitalMetrics';
import type {TracePreferencesState} from 'sentry/views/performance/traceDetails/traceState/tracePreferences';
import {getInitialTracePreferences} from 'sentry/views/performance/traceDetails/traceState/tracePreferences';
import {TraceStateProvider} from 'sentry/views/performance/traceDetails/traceState/traceStateProvider';
Expand Down Expand Up @@ -90,7 +91,19 @@ function NewTraceViewImpl({replay}: {replay: undefined | HydratedReplayRecord})
additionalAttributes: REPLAY_TRACE_ADDITIONAL_ATTRIBUTES,
});
const meta = useReplayTraceMeta(replay);
const tree = useTraceTree({trace, replay: replay ?? null});
const webVitalMetrics = useTraceWebVitalMetrics({
enabled:
!!firstTrace &&
organization.features.includes('dashboards-web-vitals-trace-metrics'),
queryParams: {
start: undefined,
end: undefined,
statsPeriod: undefined,
timestamp: firstTrace?.timestamp,
},
traceSlug: firstTrace?.traceSlug ?? '',
});
const tree = useTraceTree({trace, replay: replay ?? null, webVitalMetrics});

useTraceStateAnalytics({
trace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ export function withoutWebVitals(
}

/**
* Adds the web vitals read from trace metrics to the nodes they were measured
* on. The first row per node and vital wins.
* Adds the web vitals read from trace metrics to the spans they were measured
* on, or to the trace when a metric has no span in it. The first row per node
* and vital wins.
*/
export function collectWebVitalMetrics(
tree: TraceTree,
Expand All @@ -215,14 +216,14 @@ export function collectWebVitalMetrics(
// Relay writes the span id to `sentry.span_id`; `span_id` is empty.
const spanId =
row[TraceMetricKnownFieldKey.OLD_SPAN_ID] ?? row[TraceMetricKnownFieldKey.SPAN_ID];
const node = spanId ? nodes.get(spanId) : undefined;
const node = (spanId && nodes.get(spanId)) || traceNode;
const vital = ORDER.find(
v => webVitalMetricName(v) === row[TraceMetricKnownFieldKey.METRIC_NAME]
);
if (!node || !vital || tree.vitals.get(node)?.some(v => v.key === vital)) {
const nodeMeasurements = measurements.get(node) ?? {};
if (!vital || nodeMeasurements[vital]) {
continue;
}
const nodeMeasurements = measurements.get(node) ?? {};
nodeMeasurements[vital] = {value: Number(row[TraceMetricKnownFieldKey.METRIC_VALUE])};
const ratio = row[webVitalScoreRatioField(vital)];
if (typeof ratio === 'number') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,47 @@ describe('TraceTree', () => {
);
});

it('anchors web vital metrics without a span in the trace to the trace', () => {
const tree = TraceTree.FromTrace(
makeEAPTrace([
makeEAPSpan({
event_id: 'eap-span-1',
start_timestamp: start,
end_timestamp: start + 2,
is_transaction: true,
children: [],
}),
]),
{
...traceOptions,
webVitalMetrics: [
{
span_id: null,
'metric.name': 'browser.web_vital.lcp',
value: 200,
'tags[score.ratio.lcp,number]': 0.9,
},
{span_id: 'missing', 'metric.name': 'browser.web_vital.cls', value: 0.1},
{span_id: null, 'metric.name': 'browser.web_vital.lcp', value: 300},
],
}
);

const traceNode = tree.root.children[0]!;
expect(tree.vitals.get(traceNode)).toEqual([
expect.objectContaining({
key: 'lcp',
measurement: {value: 200},
score: 90,
timestamp: start * 1e3 + 200,
}),
expect.objectContaining({key: 'cls', measurement: {value: 0.1}}),
]);
expect(tree.indicators).toEqual([
expect.objectContaining({type: 'lcp', start: start * 1e3 + 200, score: 90}),
]);
});

it('ignores span web vitals when trace metrics are provided', () => {
const tree = TraceTree.FromTrace(
makeEAPTrace([
Expand Down
Loading