Skip to content

Commit

Permalink
feat(tracing): Send component name on interaction spans (#9948)
Browse files Browse the repository at this point in the history
One of the PRs scoped from
#9855

Sends component names on the databag of interaction spans
  • Loading branch information
0Calories authored Dec 21, 2023
1 parent de6d41a commit f433a55
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ const delay = e => {
};

document.querySelector('[data-test-id=interaction-button]').addEventListener('click', delay);
document.querySelector('[data-test-id=annotated-button]').addEventListener('click', delay);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<body>
<div>Rendered Before Long Task</div>
<button data-test-id="interaction-button">Click Me</button>
<button data-test-id="annotated-button" data-sentry-component="AnnotatedButton">Click Me</button>
<script src="https://example.com/path/to/script.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,35 @@ sentryTest(
}
},
);

sentryTest(
'should use the component name for a clicked element when it is available',
async ({ browserName, getLocalTestPath, page }) => {
const supportedBrowsers = ['chromium', 'firefox'];

if (shouldSkipTracingTest() || !supportedBrowsers.includes(browserName)) {
sentryTest.skip();
}

await page.route('**/path/to/script.js', (route: Route) =>
route.fulfill({ path: `${__dirname}/assets/script.js` }),
);

const url = await getLocalTestPath({ testDir: __dirname });

await page.goto(url);
await getFirstSentryEnvelopeRequest<Event>(page);

await page.locator('[data-test-id=annotated-button]').click();

const envelopes = await getMultipleSentryEnvelopeRequests<TransactionJSON>(page, 1);
expect(envelopes).toHaveLength(1);
const eventData = envelopes[0];

expect(eventData.spans).toHaveLength(1);

const interactionSpan = eventData.spans![0];
expect(interactionSpan.op).toBe('ui.interaction.click');
expect(interactionSpan.description).toBe('body > AnnotatedButton');
},
);
15 changes: 11 additions & 4 deletions packages/tracing-internal/src/browser/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable max-lines */
import type { IdleTransaction, Transaction } from '@sentry/core';
import { getActiveTransaction } from '@sentry/core';
import type { Measurements } from '@sentry/types';
import { browserPerformanceTimeOrigin, htmlTreeAsString, logger } from '@sentry/utils';
import type { Measurements, SpanContext } from '@sentry/types';
import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger } from '@sentry/utils';

import { DEBUG_BUILD } from '../../common/debug-build';
import {
Expand Down Expand Up @@ -102,13 +102,20 @@ export function startTrackingInteractions(): void {
const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime);
const duration = msToSec(entry.duration);

transaction.startChild({
const span: SpanContext = {
description: htmlTreeAsString(entry.target),
op: `ui.interaction.${entry.name}`,
origin: 'auto.ui.browser.metrics',
startTimestamp: startTime,
endTimestamp: startTime + duration,
});
};

const componentName = getComponentName(entry.target);
if (componentName) {
span.data = { 'ui.component_name': componentName };
}

transaction.startChild(span);
}
}
});
Expand Down

0 comments on commit f433a55

Please sign in to comment.