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 15 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. Just picked this for now, name is subject to change.

These API are exposed in the Sentry._experiment_log namespace.

On the high level, there are functions for each of the logging severity levels critical, fatal, error, warn, info, debug, trace. These functions are tagged template functions, so they use a special string template syntax that we use to parameterize functions accordingly.

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

If you want more custom usage, we also expose a captureLog method that allows you to pass custom attributes, but it's less easy to use than the tagged template functions.

Sentry._experiment_log.captureLog('error', 'Hello world!', { 'user.id': 123 });

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.64 KB +2.53% +597 B 🔺
@sentry/browser - with treeshaking flags 23.46 KB +2.74% +640 B 🔺
@sentry/browser (incl. Tracing) 36.66 KB +1.49% +549 B 🔺
@sentry/browser (incl. Tracing, Replay) 73.83 KB +0.75% +560 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.26 KB +0.84% +571 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 78.46 KB +0.73% +578 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 91.03 KB +0.62% +566 B 🔺
@sentry/browser (incl. Feedback) 40.78 KB +1.45% +596 B 🔺
@sentry/browser (incl. sendFeedback) 28.26 KB +2.11% +597 B 🔺
@sentry/browser (incl. FeedbackAsync) 33.06 KB +1.8% +597 B 🔺
@sentry/react 25.47 KB +2.4% +611 B 🔺
@sentry/react (incl. Tracing) 38.6 KB +1.56% +606 B 🔺
@sentry/vue 27.87 KB +2.11% +589 B 🔺
@sentry/vue (incl. Tracing) 38.38 KB +1.5% +579 B 🔺
@sentry/svelte 23.67 KB +2.53% +597 B 🔺
CDN Bundle 24.78 KB +2.17% +537 B 🔺
CDN Bundle (incl. Tracing) 36.67 KB +1.46% +539 B 🔺
CDN Bundle (incl. Tracing, Replay) 71.67 KB +0.74% +536 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 76.87 KB +0.72% +558 B 🔺
CDN Bundle - uncompressed 72.37 KB +2.07% +1.46 KB 🔺
CDN Bundle (incl. Tracing) - uncompressed 108.75 KB +1.36% +1.46 KB 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 220 KB +0.67% +1.46 KB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 232.58 KB +0.64% +1.47 KB 🔺
@sentry/nextjs (client) 39.77 KB +1.57% +626 B 🔺
@sentry/sveltekit (client) 37.09 KB +1.51% +564 B 🔺
@sentry/node 142.7 KB +0.43% +622 B 🔺
@sentry/node - without tracing 96.12 KB +0.63% +609 B 🔺
@sentry/aws-serverless 120.45 KB +0.5% +609 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

const SEVERITY_TEXT_TO_SEVERITY_NUMBER: Partial<Record<LogSeverityLevel | 'log', number>> = {
trace: 1,
log: 2,
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 wouldn't expect sentry.log to be lower priority than sentry.debug - is this actually what otel says?

Copy link
Member

Choose a reason for hiding this comment

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

I put this in the wrong place 🤦

just saw it while I saw debugging, more reason to add tests for this

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.

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.

5 participants