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 alpha version of Sentry logs #4126

Merged
merged 31 commits into from
Mar 17, 2025
Merged

Conversation

colin-sentry
Copy link
Member

@colin-sentry colin-sentry commented Mar 10, 2025

Logs are coming to sentry!

This commit:

  • Adds sentry_sdk._experimental_logger.{trace, debug, info, warn, error, fatal} methods
  • Adds _experimental options for before_emit_log and enable_sentry_logs

There are no tests (yet), and this still uses the otel_log schema.

Example usage:

def _before_emit_log(record, hint):
    if record["severity_text"] in ["fatal", "error"]:
        return None
    return record

sentry_sdk.init(
    dsn=...,
    _experiments={
        "enable_sentry_logs": True, 
        "before_emit_log": _before_emit_log,
    },
)

from sentry_sdk import _experimental_logger as sentry_logger
sentry_logger.info('Finished sending answer! #chunks={num_chunks}', num_chunks=10)
Screenshot 2025-03-10 at 4 11 19 PM

Refs #4149

Copy link

codecov bot commented Mar 10, 2025

Codecov Report

Attention: Patch coverage is 81.57895% with 14 lines in your changes missing coverage. Please review.

Project coverage is 79.55%. Comparing base (380e32f) to head (1906ab4).
Report is 1 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/client.py 79.66% 5 Missing and 7 partials ⚠️
sentry_sdk/envelope.py 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4126      +/-   ##
==========================================
+ Coverage   79.52%   79.55%   +0.02%     
==========================================
  Files         140      141       +1     
  Lines       15636    15711      +75     
  Branches     2652     2671      +19     
==========================================
+ Hits        12435    12499      +64     
  Misses       2369     2369              
- Partials      832      843      +11     
Files with missing lines Coverage Δ
sentry_sdk/__init__.py 100.00% <ø> (ø)
sentry_sdk/_experimental_logger.py 100.00% <100.00%> (ø)
sentry_sdk/envelope.py 80.09% <50.00%> (-0.60%) ⬇️
sentry_sdk/client.py 79.24% <79.66%> (+0.23%) ⬆️

... and 6 files with indirect coverage changes

@cleptric
Copy link
Member

Any reason we are not taking the same route as JS with this?
As in, making this a clearly experimental API etc.

@colin-sentry
Copy link
Member Author

Any reason we are not taking the same route as JS with this? As in, making this a clearly experimental API etc.

the import is from _experimental_logger.py for now, similar UX

@k-fish k-fish requested review from antonpirker, sentrivana and a team March 12, 2025 17:55
@colin-sentry colin-sentry changed the title WIP: feat(ourlogs): Add alpha version of logger feat(ourlogs): Add alpha version of logger Mar 12, 2025
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

This looks ok for a first implementation. I added some unit tests to know how it feels to use this new API.

I have added some TODO comments with topics I think we should discuss.

@antonpirker
Copy link
Member

I we want to parse variables from f-strings here is how other libs do this:

@antonpirker antonpirker changed the title feat(ourlogs): Add alpha version of logger feat(ourlogs): Add alpha version of Sentry logs Mar 14, 2025
@antonpirker antonpirker changed the title feat(ourlogs): Add alpha version of Sentry logs feat(logs): Add alpha version of Sentry logs Mar 14, 2025
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

After adding some tests and adding the configuration options also under the _experimental flag, this is good enough for an alpha version!

@antonpirker antonpirker merged commit 486d733 into master Mar 17, 2025
140 checks passed
@antonpirker antonpirker deleted the logging_integration branch March 17, 2025 08:43
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