-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(analytics): typesafe analytics.Event
and analytics.record
#94953
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(analytics): typesafe analytics.Event
and analytics.record
#94953
Conversation
…cs event definition and recording
analytics.Event
and analytics.record
Codecov ReportAttention: Patch coverage is ✅ All tests successful. No failed tests found.
Additional details and impacted files@@ Coverage Diff @@
## master #94953 +/- ##
==========================================
+ Coverage 87.87% 87.90% +0.02%
==========================================
Files 10456 10438 -18
Lines 604784 604154 -630
Branches 23617 23498 -119
==========================================
- Hits 531454 531064 -390
+ Misses 72969 72725 -244
- Partials 361 365 +4 |
Co-authored-by: Simon Hellmayr <[email protected]>
Co-authored-by: Vjeran Grozdanić <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - and great job designing this, it will make our analytics system so much more robust! 🥳
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Dataclass Construction Fails Across Styles
The from_instance
method has multiple issues:
- Old-style path: It ignores
kwargs
foruuid_
,datetime_
, and other dataclass fields, leading to incorrect event data. The directcls()
call is also fragile, potentially failing if old-style event subclasses introduce required fields without defaults. - New-style path: It fails to filter
init=False
fields when constructing the instance, which can cause aTypeError
. - General: The codebase mixes Pydantic's
@dataclass
with standard librarydataclasses
functions, risking subtle incompatibilities.
src/sentry/analytics/event.py#L102-L118
sentry/src/sentry/analytics/event.py
Lines 102 to 118 in bfe2996
# we can remove this. | |
if cls.attributes: | |
items = { | |
attr.name: kwargs.get(attr.name, getattr(instance, attr.name, None)) | |
for attr in cls.attributes | |
} | |
self = cls() | |
self.data = get_data(cls.attributes, items) | |
return self | |
return cls( | |
**{ | |
f.name: kwargs.get(f.name, getattr(instance, f.name, None)) | |
for f in fields(cls) | |
if f.name not in ("type", "uuid_", "datetime_") | |
} | |
) |
Was this report helpful? Give feedback by reacting with 👍 or 👎
Suspect IssuesThis pull request was deployed and Sentry observed the following issues:
Did you find this useful? React with a 👍 or 👎 |
…94953) Co-authored-by: Simon Hellmayr <[email protected]> Co-authored-by: Vjeran Grozdanić <[email protected]>
from pydantic import Field | ||
from pydantic.dataclasses import dataclass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be possible to write this without pydantic
we're permastuck on version 1 and I'm looking to factor it out because it is a liability to upgrading python -- adding new usage of it makes that a lot lot harder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah AFAIK @constantinius wants to refactor this without pydantic when the migration of analytics events over to the typed structure is done. Do you have a timeline in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not urgent -- still have to factor it out of hybrid cloud 😭
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright cool - I'll bring up that you want to get pydantic out of the codebase when he's back from vacation 👍
Closes: https://linear.app/getsentry/issue/TET-804/analytics-allow-both-old-style-attributes-as-well-as-new-style
Supersedes: #92943
Making analytics.record and analytics.Event typesafer
Deprecate stringly typed analytics.record() in favor of version that uses analytics.Event objects.
Migrating analytics.Event to dataclasses. For convenience introduce the analytics.eventclass decorator, which sets the type and applies the dataclass decorator with common parameters underneath.
Allows "old-style" definition of Events as well as to
record()
them until all event classes are migrated.