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

Lacking examples of error_sampler implementations #11021

Closed
mathiasose opened this issue Aug 7, 2024 · 2 comments · Fixed by #11314
Closed

Lacking examples of error_sampler implementations #11021

mathiasose opened this issue Aug 7, 2024 · 2 comments · Fixed by #11314
Assignees
Labels
Platform: Python Team: Web Backend SDKs team-web-sdk-backend Type: Content Issues about the contents of our docs

Comments

@mathiasose
Copy link

Core or SDK?

Platform/SDK

Which part? Which one?

Python SDK

Description

error_sampler looks like a good feature and a perfect match for what I need, but I find myself struggling to implement it for my use case based on the docs at https://docs.sentry.io/platforms/python/configuration/sampling/#dynamically-sampling-error-events. I haven't been able to find any other implementation examples by Googling around.

I have an event happening very often in production which causes a log event (not an exception raised). I want to downsample this event in particular, so I want to implement error_sampler, but it's hard to know how. If I need to trial-and-error my way to the correct solution I need to set up my dev environment both to connect to Sentry and produce the same condition that is happening in prod. If the docs were a bit more comprehensive or included some more examples it would help a lot.

  • What is the structure of event and hint? Could this page link to other docs pages?
  • The one code example uses only hint["exc_info"][0] to set a rate based on the exception class. In my use case I am using logger = logging.getLogger(__name__) + logger.error('Special message', extra={'some': 'metadata'}) and I want to set a lower rate if I see Special message, and maybe only if the logger name matches a specific file.

My not-working-properly attempt at filtering

I tried this and deployed it, but it didn't seem to decrease the number of events received in Sentry. Maybe I'm missing something obvious, or maybe it's a more subtle error, but regardless a few examples in the docs would help everyone.

def error_sampler(event: Event, hint: Hint) -> float:
    if event.get("message") == "Special message":
        # Only include 1% of these events
        return 0.01

    return 1.0

sentry_sdk.init(
    ...
    error_sampler=error_sampler,
)

Relevant:

CC @szokeasaurusrex

Suggested Solution

  • Show some more code examples of how to set a different rate based on the log message text, extra data, timestamps, logger name, etc. The current example with exception classes is good, we just need more.
  • More comprehensive explanation of what the event and hint parameters contain.
@mathiasose mathiasose added the Type: Content Issues about the contents of our docs label Aug 7, 2024
@getsantry
Copy link
Contributor

getsantry bot commented Aug 7, 2024

Assigning to @getsentry/support for routing ⏲️

@szokeasaurusrex
Copy link
Member

szokeasaurusrex commented Aug 7, 2024

@mathiasose, you are right, we could probably improve the docs here. We could certainly add some more examples, although describing exactly what is in the event and hint is difficult, since this could be different depending on the exception and how it was captured.

My recommendation when you are developing an error_sampler function and are unclear about what the event and hint contain would be to simply trigger the type of exception you wish to capture. Then, using a breakpoint in the error_sampler function, you can determine what the event and hint look like. Perhaps we could add this recommendation to the docs.

I was able to easily discover the solution in your case by using this debugger process. The reason why your current code snippet fails is because event["message"] is not set (i.e. KeyError) on events created from logging errors. You can instead find the log message in event["logentry"]["message"]. So, if you modify your error_sampler to the following, it should work as you expect:

def error_sampler(event: Event, hint: Hint) -> float:
    if event.get("logentry", {}).get("message") == "Special message":
        # Only include 1% of these events
        return 0.01

    return 1.0

@antonpirker antonpirker self-assigned this Sep 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Platform: Python Team: Web Backend SDKs team-web-sdk-backend Type: Content Issues about the contents of our docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants