Skip to content

expose Event type in package api #4122

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

Closed
fraser-langton opened this issue Mar 6, 2025 · 6 comments
Closed

expose Event type in package api #4122

fraser-langton opened this issue Mar 6, 2025 · 6 comments

Comments

@fraser-langton
Copy link

fraser-langton commented Mar 6, 2025

Problem Statement

Was wondering by the Event type was hidden in a private module _types, I am writing my before_send function and because I am a good pythonista I wanted to type it :)

The type was added in #2753 but maybe it didn't occur to them to add it then?

The same would probably apply to a lot of other types the user might need (hint etc...)

Here is how I currently do it, which is fine, but it doesn't encourage typing

import sentry_sdk
from sentry_sdk._types import Event, Hint


def before_send(event: Event, hint: Hint) -> Event:
    # do some stuff
    ...
    return event


sentry_sdk.init(
    dsn="***",
    before_send=before_send
)

Solution Brainstorm

Add to init? change submodule to public, typing or types?

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Mar 6, 2025
@alex-goswag
Copy link

Would like to add my support for this, we ended up using the Event from types but just as an easier way to browser the types.

Proper type hinting would have been nice at the time or for those unfamiliar with Sentry.

from sentry_sdk.types import Event

def _filter_transactions(event, hint) -> Optional[Event]:
    url_string = event["request"]["url"]
    parsed_url = urlparse(url_string)

    if parsed_url.path == "/health":
        # We don't send transactions for health checks, to avoid spamming Sentry
        return None

    return event

def sentry_initialization():
    sentry_sdk.init(
        dsn="***",    
        before_send_transaction=_filter_transactions,

@sentrivana
Copy link
Contributor

Hey folks, thanks for raising this.

Add to init? change submodule to public, typing or types?

Event is also accessible via the public sentry_sdk.types (see here). We can definitely think about exporting it under just sentry_sdk, too, to make it easier for folks to work with Events in the callbacks we provide. (Same goes for Hint.)

@fraser-langton
Copy link
Author

Ok, seems I just missed it then.

Perhaps more just a comment on how little the docs encourage usage of the types, I did look around the docs quite a lot and couldn't find examples.

(also it seems my IDE couldn't find the imports when recommending imports, but that's unrelated)

@sentrivana
Copy link
Contributor

sentrivana commented Mar 11, 2025

We can definitely make it more discoverable. I imagine if we moved the definition to types directly (instead of just re-exporting it there from _types), IDE completion would probably also work better. I'll see if we can improve this with the next major release. Created an issue to keep track.

A mention in the docs is also a great idea, we can do that for sure. Thanks for the great feedback!

@fraser-langton
Copy link
Author

I think just more mentions in the docs would suffice :)

@antonpirker
Copy link
Member

Hey @fraser-langton we updated our docs to include the types. Thanks again for raising this. We also have a PR merged that makes more types public which will be released soon: #4315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

4 participants