-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix usage analytics to properly register and not count if disabled #11848
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
Conversation
f580147
to
fa81303
Compare
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! nice catch, can't believe i missed that one!
@hooks.on_infra_shutdown(should_load=lambda: not config.DISABLE_EVENTS) | ||
def aggregate_and_send(): | ||
""" | ||
Aggregates data from all registered usage trackers and immediately sends the aggregated result to the analytics service. | ||
""" | ||
if config.DISABLE_EVENTS: | ||
return |
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.
wow thanks for adding this, can't believe i missed this 😅
i personally probably would have kept the guard in the method and not use should_load
, simply because it makes it a bit easier to debug in case something's up. it also allows us to change config.DISABLE_EVENTS
after the runtime has initiated, though we really don't have a case for that now it would be a bit more consistent with being able to set certain config values at runtime via the config endpoint.
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.
I see, yes, I thought about this as well! Will update.
Only thing is that with the current logic to avoid "counting" if not enabled, even if we update at runtime, we will still avoid counting for now, or I should update the code to use the config variable directly. We can always update later for that part. 👍
fa81303
to
533d9dd
Compare
Motivation
While looking to implement the usage analytics for the API Gateway service, I've noticed three things:
This PR mainly does 2 things:
itertools.count
which is thread safe in CPythonI don't really like the boolean check for every call to
increment
and such, we could probably refactor our counters to be no-op classes if the analytics are disabled? But this at least prevents the memory leaks for users withDISABLE_EVENTS=1
as a quick fix before v4, while re-enabling sending the analytics over.As a sanity check, I've run some small benchmarks with
timeit
:Setting 1 000 000 values in the
state
:Performance:
append
Memory:
append
Changes
aggregate_and_send
with@hooks.on_infra_shutdown(should_load=lambda: not config.DISABLE_EVENTS)
DISABLE_EVENTS
is trueUsageSetCounter
count
aggregation methodmedian
calculation, as it would have been incorrect without sorting the list firstFollow-up
I'm following with #11854 which might contain more controversial changes, related to our counters. I believe the
UsageCounter
could be simplified into a really simple counter for less complex use case with no aggregation (likelambda:hotreload
), and rename the current counter into a somewhat "timing" related counter with aggregation for more complex use cases.Multithreading testing:
With this small test, we would always end up with the right amount of counting, just for sanity check.
\cc @cloutierMat thanks for the help and pairing on this! 🙏