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(logs): Add experimental user-callable logging methods #15442

Open
wants to merge 32 commits into
base: develop
Choose a base branch
from

Conversation

colin-sentry
Copy link
Member

@colin-sentry colin-sentry commented Feb 18, 2025

This PR adds logging APIs to the SDK. At the moment this is only exposed in @sentry/core and @sentry/browser.

Logging is gated by an experimental option, _experiments.enableLogs.

Sentry.init({
  _experiments: {
    enableLogs: true,
  },
});

These API are exposed in the Sentry._experiment_log namespace.

On the high level, there are functions for each of the logging severity levels fatal, error, warn, info, debug, trace. There is also a log function that is an alias to info.

Sentry._experiment_log.info('Adding item to cart', { item: 'sneakers' });

If you want to do structured logging, you'll need to use the fmt helper exposed in the Sentry._experiment_log namespace.

const { fmt } = Sentry._experiment_log;

Sentry._experiment_log.info(fmt`user ${username} just bought ${item}!`);

There is a buffer of logs that flushes every 5 seconds, or when you hit 25 logs, whichever comes first.

Frame 1 (5)

Copy link
Contributor

github-actions bot commented Feb 18, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.81 KB +2.92% +691 B 🔺
@sentry/browser - with treeshaking flags 23.62 KB +3.01% +706 B 🔺
@sentry/browser (incl. Tracing) 36.82 KB +1.71% +631 B 🔺
@sentry/browser (incl. Tracing, Replay) 73.98 KB +0.82% +612 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.41 KB +0.92% +629 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 78.62 KB +0.79% +630 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 91.16 KB +0.67% +618 B 🔺
@sentry/browser (incl. Feedback) 40.97 KB +1.68% +693 B 🔺
@sentry/browser (incl. sendFeedback) 28.45 KB +2.42% +687 B 🔺
@sentry/browser (incl. FeedbackAsync) 33.23 KB +2.06% +685 B 🔺
@sentry/react 25.6 KB +2.59% +661 B 🔺
@sentry/react (incl. Tracing) 38.72 KB +1.65% +643 B 🔺
@sentry/vue 28.02 KB +2.32% +650 B 🔺
@sentry/vue (incl. Tracing) 38.53 KB +1.69% +655 B 🔺
@sentry/svelte 23.85 KB +2.9% +688 B 🔺
CDN Bundle 24.35 KB - -
CDN Bundle (incl. Tracing) 36.25 KB - -
CDN Bundle (incl. Tracing, Replay) 71.26 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 76.43 KB - -
CDN Bundle - uncompressed 71.14 KB - -
CDN Bundle (incl. Tracing) - uncompressed 107.52 KB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 218.79 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 231.36 KB - -
@sentry/nextjs (client) 39.86 KB +1.54% +618 B 🔺
@sentry/sveltekit (client) 37.24 KB +1.69% +632 B 🔺
@sentry/node 142.81 KB +0.47% +683 B 🔺
@sentry/node - without tracing 96.24 KB +0.7% +685 B 🔺
@sentry/aws-serverless 120.57 KB +0.54% +663 B 🔺

View base workflow run

@@ -223,6 +223,7 @@ const ITEM_TYPE_TO_DATA_CATEGORY_MAP: Record<EnvelopeItemType, DataCategory> = {
feedback: 'feedback',
span: 'span',
raw_security: 'security',
otel_log: 'log_item',
Copy link
Contributor

Choose a reason for hiding this comment

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

Will there be any other type than otel_log? If not in the foreseeable future, I'd suggest we change it to

Suggested change
otel_log: 'log_item',
log: 'log',

Again singular, as in span, or replay

Copy link
Member

Choose a reason for hiding this comment

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

This is done because it matches the code in relay for data categories (rate limiting and client outcomes) https://github.com/getsentry/relay/blob/e36886a98c89af645e5c0d2109657deafa25d902/relay-server/src/envelope.rs#L182

@AbhiPrasad AbhiPrasad self-assigned this Feb 19, 2025
Copy link

codecov bot commented Feb 20, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
4648 1 4647 324
View the top 1 failed test(s) by shortest run time
should report ANR when event loop blocked worker can be stopped and restarted
Stack Traces | 15s run time
Error: thrown: "Exceeded timeout of 15000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
    at .../suites/anr/test.ts:204:3
    at _dispatchDescribe (.../jest-circus/build/index.js:98:26)
    at describe (.../jest-circus/build/index.js:60:5)
    at Object.<anonymous> (.../suites/anr/test.ts:109:1)
    at Runtime._execModule (.../jest-runtime/build/index.js:1646:24)
    at Runtime._loadModule (.../jest-runtime/build/index.js:1185:12)
    at Runtime.requireModule (.../jest-runtime/build/index.js:1009:12)
    at jestAdapter (.../build/legacy-code-todo-rewrite/jestAdapter.js:79:13)
    at processTicksAndRejections (node:internal/process/task_queues:105:5)
    at runTestInternal (.../jest-runner/build/runTest.js:389:16)

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@AbhiPrasad AbhiPrasad changed the title feat(ourlogs): Add experimental user-callable logging methods feat(logs): Add experimental user-callable logging methods Feb 20, 2025
Comment on lines 17 to 23
trace: 1,
log: 2,
debug: 5,
info: 9,
warn: 13,
error: 17,
fatal: 21,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
trace: 1,
log: 2,
debug: 5,
info: 9,
warn: 13,
error: 17,
fatal: 21,
trace: 1,
debug: 5,
info: 9,
log: 10,
warn: 13,
error: 17,
fatal: 21,

@timfish
Copy link
Collaborator

timfish commented Feb 25, 2025

It's strange how this impacts bundle size. I would have expected the additional code to tree-shake out when it's not used!

Why is the _experiments.enableLogs option required? If users don't log anything via the new experimental API, logs wont be sent.

@AbhiPrasad
Copy link
Member

It's strange how this impacts bundle size. I would have expected the additional code to tree-shake out when it's not used!

Yeah that feels weird to me too. I wonder if it's because of the log buffer side effect.

Why is the _experiments.enableLogs option required? If users don't log anything via the new experimental API, logs wont be sent.

Because we'll eventually also forward the console calls as logs, and that will be gated by _experiments.enableLogs. This experimental option will also graduate to be a top-level option, but we're bikeshedding naming because logs is super overloaded 😄

@timfish
Copy link
Collaborator

timfish commented Feb 28, 2025

I wonder if it's because of the log buffer side effect

It's all nicely decoupled so it has to be something like that.

* fatal(fmt`user ${username} just bought ${item}!`);
* ```
*/
fatal: sendLog('fatal'),
Copy link
Member

Choose a reason for hiding this comment

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

This is the side-effect y'all are probably looking for.

@k-fish
Copy link
Member

k-fish commented Mar 5, 2025

@AbhiPrasad we have to change the typing on the parametrize values to allow all the possible js types, do we want to change it or just make another function?

@k-fish
Copy link
Member

k-fish commented Mar 5, 2025

@AbhiPrasad this #15600 maybe?

@HofmannZ
Copy link

HofmannZ commented Mar 6, 2025

How does this integrate with loggers like pino?

@AbhiPrasad
Copy link
Member

How does this integrate with loggers like pino?

For now the focus is on frontend logging, but we'll have integrations for popular logging frameworks. You can see getsentry/sentry-docs#12920 for more details how SDKs will work with logging frameworks.

@joshdales
Copy link

joshdales commented Mar 7, 2025

Is there a reason that this couldn't be added to the Angular package as well?

Would be very nice if it could please 🥺

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.

8 participants