Skip to content

feat(metrics): Add timings method to metrics #12226

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

Merged
merged 6 commits into from
May 28, 2024
Merged

feat(metrics): Add timings method to metrics #12226

merged 6 commits into from
May 28, 2024

Conversation

mydea
Copy link
Member

@mydea mydea commented May 27, 2024

This introduces a new method, metrics.timing(), which can be used in two ways:

  1. With a numeric value, to simplify creating a distribution metric. This will default to second as unit:
Sentry.metrics.timing('myMetric', 100);
  1. With a callback, which will wrap the duration of the callback. This can accept a sync or async callback. It will create an inactive span around the callback and at the end emit a metric with the duration of the span in seconds:
const returnValue = Sentry.metrics.timing('myMetric', measureThisFunction);

Closes #12215

@mydea mydea self-assigned this May 27, 2024
Copy link
Contributor

github-actions bot commented May 27, 2024

size-limit report 📦

Path Size
@sentry/browser 21.78 KB (-0.02% 🔽)
@sentry/browser (incl. Tracing) 32.8 KB (-0.01% 🔽)
@sentry/browser (incl. Tracing, Replay) 68.27 KB (0%)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.7 KB (0%)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.33 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback) 84.38 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 86.2 KB (+0.41% 🔺)
@sentry/browser (incl. metrics) 25.95 KB (+12.01% 🔺)
@sentry/browser (incl. Feedback) 37.8 KB (-0.02% 🔽)
@sentry/browser (incl. sendFeedback) 26.35 KB (-0.02% 🔽)
@sentry/browser (incl. FeedbackAsync) 30.78 KB (-0.02% 🔽)
@sentry/react 24.49 KB (-0.02% 🔽)
@sentry/react (incl. Tracing) 35.82 KB (-0.01% 🔽)
@sentry/vue 25.75 KB (-0.02% 🔽)
@sentry/vue (incl. Tracing) 34.61 KB (-0.01% 🔽)
@sentry/svelte 21.91 KB (-0.03% 🔽)
CDN Bundle 23.09 KB (+0.36% 🔺)
CDN Bundle (incl. Tracing) 34.51 KB (+0.65% 🔺)
CDN Bundle (incl. Tracing, Replay) 68.35 KB (+0.38% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) 73.33 KB (+0.31% 🔺)
CDN Bundle - uncompressed 68.08 KB (+0.3% 🔺)
CDN Bundle (incl. Tracing) - uncompressed 102.18 KB (+0.44% 🔺)
CDN Bundle (incl. Tracing, Replay) - uncompressed 212.08 KB (+0.21% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 224.46 KB (+0.2% 🔺)
@sentry/nextjs (client) 35.15 KB (-0.01% 🔽)
@sentry/sveltekit (client) 33.4 KB (-0.01% 🔽)
@sentry/node 114.69 KB (-0.01% 🔽)
@sentry/aws-serverless 103.38 KB (+0.01% 🔺)

@mydea mydea force-pushed the fn/metrics-timing branch from 4c8c2a0 to ea43d42 Compare May 27, 2024 09:17
@mydea mydea force-pushed the fn/metrics-timing branch from ea43d42 to 3cf3ff1 Compare May 27, 2024 09:36
mydea added a commit that referenced this pull request May 27, 2024
Metrics are only included when performance is included, reducing the
base bundle size.

We always expose a shim so there is no API breakage, it just does
nothing.

I noticed this while working on
#12226.
@mydea mydea force-pushed the fn/metrics-timing branch from d8b0eb3 to abc9295 Compare May 28, 2024 07:06
@mydea mydea marked this pull request as ready for review May 28, 2024 07:06
@mydea mydea force-pushed the fn/metrics-timing branch from abc9295 to 19e28cc Compare May 28, 2024 07:10
() => {
const endTime = timestampInSeconds();
const timeDiff = endTime - startTime;
distribution(aggregator, name, timeDiff, { ...data, unit: 'second' });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about if we should allow to convert this into other units, but for now decided against this - this would just increase bundle size, and not sure what the value would be in doing that? This means that for now this disregards the unit passed in when used in callback form.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is totally fine being in seconds.

() => {
const endTime = timestampInSeconds();
const timeDiff = endTime - startTime;
distribution(aggregator, name, timeDiff, { ...data, unit: 'second' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is totally fine being in seconds.

Comment on lines +112 to +113
timing(name: string, value: number, unit?: DurationUnit, data?: Omit<MetricData, 'unit'>): void;
timing<T>(name: string, callback: () => T, unit?: DurationUnit, data?: Omit<MetricData, 'unit'>): T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check whether this JSDoc shows up properly in VS Code. I believe the JS Doc here might not show up when you use the callback variant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked, seems fine for me for both variants:

Screenshot 2024-05-28 at 10 15 09 Screenshot 2024-05-28 at 10 15 13

@mydea mydea force-pushed the fn/metrics-timing branch from efb1575 to 7bff900 Compare May 28, 2024 09:35
'_frozenDsc',
// This keeps metrics summary on spans
'_metrics_summary',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops turns out metrics summaries never worked for minified CDN bundles 😬 we had no tests covering this, now we have!

@AbhiPrasad
Copy link
Member

create an inactive span

Why doesn't this create an active span?

@mydea
Copy link
Member Author

mydea commented May 28, 2024

create an inactive span

Why doesn't this create an active span?

I wasn't quite sure what the expected/intended outcome is there 🤔 since this is a bit implicit, not sure what people would want there. but we can make it an active span too, do you think that's better?

@AbhiPrasad
Copy link
Member

but we can make it an active span too, do you think that's better

Because it's a callback it feels more intuitive to me to make it active (matches startSpan behavior more closely).

@mydea mydea force-pushed the fn/metrics-timing branch from 7bff900 to d51e787 Compare May 28, 2024 13:40
@mydea
Copy link
Member Author

mydea commented May 28, 2024

I updated it to make it an active span!

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @krystofwoldrich and @timfish to expose this in React Native and Electron!

@mydea mydea merged commit d63d7c6 into develop May 28, 2024
106 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement metrics.timing()
3 participants