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

feat: record API usage for any type of query #26259

Merged
merged 2 commits into from
Nov 22, 2024

Conversation

raquelmsmith
Copy link
Member

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.

Copy link
Collaborator

@mariusandra mariusandra left a 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.

@@ -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))
Copy link
Collaborator

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?

Copy link
Member Author

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

@raquelmsmith raquelmsmith enabled auto-merge (squash) November 22, 2024 17:48
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),
Copy link
Member Author

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

@raquelmsmith raquelmsmith merged commit 199e220 into master Nov 22, 2024
89 checks passed
@raquelmsmith raquelmsmith deleted the chore/update-hogql-billable-metrics branch November 22, 2024 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants