-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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: record API usage for any type of query #26259
Conversation
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.
Why not. Left in a code nit as well.
posthog/tasks/usage_report.py
Outdated
@@ -580,7 +580,7 @@ def get_teams_with_hogql_metric( | |||
FROM clusterAllReplicas({CLICKHOUSE_CLUSTER}, system.query_log) | |||
WHERE (type = 'QueryFinish' OR type = 'ExceptionWhileProcessing') | |||
AND is_initial_query = 1 | |||
AND query_type IN (%(query_types)s) | |||
AND (%(query_types)s IS NULL OR query_type IN (%(query_types)s)) |
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.
Nit: This generates some strange code like:
AND (NULL IS NULL
OR query_type IN (NULL))
and
AND (['EventsQuery'] IS NULL
OR query_type IN (['EventsQuery']))
Since query_types
is a python variable, wouldn't it be better to rewrite the entire clause based on its contents?
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 let me rewrite that, thanks
query_app_bytes_read=all_data["teams_with_query_app_bytes_read"].get(team.id, 0), | ||
query_app_rows_read=all_data["teams_with_query_app_rows_read"].get(team.id, 0), | ||
query_app_duration_ms=all_data["teams_with_query_app_duration_ms"].get(team.id, 0), | ||
query_api_bytes_read=all_data["teams_with_query_api_bytes_read"].get(team.id, 0), |
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.
@patricio-posthog this will likely be your usage key for the API product - query_api_bytes_read
Problem
We want to start billing for API usage against the /query endpoint so we can lift the rate limits on it. First step: measure the usage we are already seeing to this endpoint. But, currently usage reports only capture hogql queries, but we want to charge for any type of query.
Changes
Records usage for any type of query hitting the API.
Renames the usage record key because it's not for hogql specifically any more, but for any type of query hitting /query.
👉 Stay up-to-date with PostHog coding conventions for a smoother review.
Does this work well for both Cloud and self-hosted?
How did you test this code?
Updated the tests and added a new one for api-specific queries.