Skip to content

feat(logs): Add alpha version of Sentry logs#4126

Merged
antonpirker merged 31 commits into
masterfrom
logging_integration
Mar 17, 2025
Merged

feat(logs): Add alpha version of Sentry logs#4126
antonpirker merged 31 commits into
masterfrom
logging_integration

Conversation

@colin-sentry

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

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Mar 10, 2025

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Contributor 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 a team, antonpirker and sentrivana 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

@antonpirker antonpirker left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread sentry_sdk/client.py
@antonpirker

Copy link
Copy Markdown
Contributor

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

Comment thread sentry_sdk/consts.py Outdated
Comment thread sentry_sdk/_experimental_logger.py Outdated
@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

@antonpirker antonpirker left a comment

Copy link
Copy Markdown
Contributor

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!

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