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): Make the logging integration send Sentry logs #4143

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

colin-sentry
Copy link
Member

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

We have integrations that make the python logger create breadcrumbs and issues. This adds a third handler which creates Sentry logs on logger.log statements.

Enable the logger with:

sentry_sdk.init(
    ...
    _experiments={
        "enable_sentry_logs": True
    }
)

some_logger = logging.Logger("some-logger")
some_logger.info('Finished sending answer! #chunks=%s', chunks)

Screenshot 2025-03-17 at 4 12 27 PM

Refs #4150

Copy link

codecov bot commented Mar 17, 2025

Codecov Report

Attention: Patch coverage is 74.28571% with 18 lines in your changes missing coverage. Please review.

Project coverage is 79.49%. Comparing base (12b3ca3) to head (88f390c).
Report is 4 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
sentry_sdk/integrations/logging.py 72.00% 2 Missing and 12 partials ⚠️
sentry_sdk/client.py 69.23% 0 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4143      +/-   ##
==========================================
- Coverage   79.56%   79.49%   -0.08%     
==========================================
  Files         141      141              
  Lines       15736    15784      +48     
  Branches     2675     2694      +19     
==========================================
+ Hits        12520    12547      +27     
- Misses       2369     2377       +8     
- Partials      847      860      +13     
Files with missing lines Coverage Δ
sentry_sdk/_experimental_logger.py 100.00% <100.00%> (ø)
sentry_sdk/consts.py 92.85% <ø> (ø)
sentry_sdk/client.py 78.88% <69.23%> (-0.30%) ⬇️
sentry_sdk/integrations/logging.py 83.33% <72.00%> (-5.78%) ⬇️

... and 6 files with indirect coverage changes

@antonpirker antonpirker changed the title feat(logs): Make the python logger go to ourlogs feat(logs): Make the logging integration send Sentry logs Mar 18, 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.

All in all looks good. I have some questions

} # type: Log
propagation_context = scope.get_active_propagation_context()
if propagation_context is not None:
headers["trace_id"] = propagation_context.trace_id
Copy link
Member

Choose a reason for hiding this comment

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

I think the trace_id should go into headers["trace"]["trace_id"] (the trace envelope header contains the DynamicSamplingContext of an event. See: https://github.com/getsentry/relay/blob/master/relay-server/src/envelope.rs#L1085)

Copy link
Member

Choose a reason for hiding this comment

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

Also see here for the Dynamic Sampling Context (aka DSC) spec: https://develop.sentry.dev/sdk/telemetry/traces/dynamic-sampling-context/#dsc-specification


if level is not None:
self._breadcrumb_handler = BreadcrumbHandler(level=level)
self._sentry_logs_handler = SentryLogsHandler(level=level)
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a separate sentry_logs_level config option to specify what levels will be emitted as Sentry logs?


if level is not None:
self._breadcrumb_handler = BreadcrumbHandler(level=level)
self._sentry_logs_handler = SentryLogsHandler(level=level)
Copy link
Member

Choose a reason for hiding this comment

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

Lets just attach this handler if Sentry logs is enabled. This safes us some CPU cycles every time a log statement is called:

Suggested change
self._sentry_logs_handler = SentryLogsHandler(level=level)
client = sentry_sdk.get_client()
if client.options["_experiments"].get("enable_sentry_logs", False):
self._sentry_logs_handler = SentryLogsHandler(level=level)

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.

2 participants