-
Notifications
You must be signed in to change notification settings - Fork 89
feat: Backends return Backends.QueryError struct for errors #3552
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
Draft
msmithstubbs
wants to merge
4
commits into
Logflare:main
Choose a base branch
from
msmithstubbs:refactor/backend-query-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ defmodule Logflare.Backends.Adaptor.BigQueryAdaptor do | |
| alias Logflare.Backends.Ecto.SqlUtils | ||
| alias Logflare.Backends.IngestEventQueue | ||
| alias Logflare.Backends.Adaptor.QueryResult | ||
| alias Logflare.Backends.QueryError | ||
| alias Logflare.BigQuery.SchemaTypes | ||
| alias Logflare.Billing | ||
| alias Logflare.BqRepo | ||
|
|
@@ -685,7 +686,7 @@ defmodule Logflare.Backends.Adaptor.BigQueryAdaptor do | |
| query_opts :: Keyword.t() | ||
| ) :: | ||
| {:ok, QueryResult.t()} | ||
| | {:error, any()} | ||
| | {:error, QueryError.t()} | ||
| defp execute_user_query(%User{} = user, project_id, query_string, bq_params, query_opts) | ||
| when is_non_empty_binary(query_string) and is_list(bq_params) and is_list(query_opts) do | ||
| case BqRepo.query_with_sql_and_params( | ||
|
|
@@ -704,20 +705,77 @@ defmodule Logflare.Backends.Adaptor.BigQueryAdaptor do | |
| bq_params: bq_params | ||
| })} | ||
|
|
||
| {:error, %{body: body}} -> | ||
| decoded = Jason.decode!(body)["error"] | ||
| error = GenUtils.process_bq_errors(decoded, user.id) | ||
| maybe_warn_reservation_error(decoded, user, project_id, query_opts) | ||
| {:error, error} | ||
| {:error, error} -> | ||
| query_error = | ||
| error | ||
| |> to_query_error(user.id) | ||
| |> QueryError.log( | ||
| user_id: user.id, | ||
| bigquery_project_id: project_id | ||
| ) | ||
|
|
||
| maybe_warn_reservation_error(query_error, user, project_id, query_opts) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be logged already by QueryError.log - do we need the specific wording of maybe_warn_reservation_error ? If not, we can remove it. |
||
|
|
||
| {:error, query_error} | ||
| end | ||
| end | ||
|
|
||
| {:error, err} when is_atom(err) -> | ||
| {:error, GenUtils.process_bq_errors(err, user.id)} | ||
| @spec to_query_error(Tesla.Env.t() | GenUtils.transport_error(), pos_integer()) :: | ||
| QueryError.t() | ||
| defp to_query_error(error, _user_id) when error in [:timeout, :closed, :emfile] do | ||
| %QueryError{ | ||
| kind: :connection_error, | ||
| raw_error: error, | ||
| backend: __MODULE__ | ||
| } | ||
| end | ||
|
|
||
| {:error, err} -> | ||
| {:error, err} | ||
| defp to_query_error(%{body: body}, user_id) when is_binary(body) do | ||
| with {:ok, %{"error" => raw_error}} <- Jason.decode(body), | ||
| %{"message" => _message} = processed_error <- | ||
| GenUtils.process_bq_errors(raw_error, user_id) do | ||
| processed_error | ||
| |> query_error_kind() | ||
| |> query_error(processed_error) | ||
| else | ||
| _error -> query_error(:backend_error, body) | ||
| end | ||
| end | ||
|
|
||
| defp to_query_error(%{body: body}, _user_id) do | ||
| query_error(:backend_error, body) | ||
| end | ||
|
|
||
| @spec query_error_kind(map()) :: QueryError.kind() | ||
| defp query_error_kind(%{"reason" => "billingTierLimitExceeded"}), do: :backend_error | ||
| defp query_error_kind(%{"reason" => "invalidQuery"}), do: :invalid_query | ||
|
|
||
| defp query_error_kind(%{"errors" => errors}) when is_list(errors) do | ||
| if Enum.any?(errors, &match?(%{"reason" => "invalidQuery"}, &1)) do | ||
| :invalid_query | ||
| else | ||
| :backend_error | ||
| end | ||
| end | ||
|
|
||
| defp query_error_kind(processed_error) do | ||
| with %{"message" => message} when is_binary(message) <- processed_error, | ||
| true <- String.starts_with?(message, ["Unrecognized name:", "Field name"]) do | ||
| :invalid_query | ||
| else | ||
| _ -> :backend_error | ||
| end | ||
| end | ||
|
|
||
| @spec query_error(QueryError.kind(), term()) :: QueryError.t() | ||
| defp query_error(kind, raw_error) do | ||
| %QueryError{ | ||
| kind: kind, | ||
| raw_error: raw_error, | ||
| backend: __MODULE__ | ||
| } | ||
| end | ||
|
|
||
| @spec pg_sql_to_bq_sql(sql :: String.t()) :: String.t() | ||
| defp pg_sql_to_bq_sql(sql) when is_non_empty_binary(sql) do | ||
| sql | ||
|
|
@@ -741,19 +799,25 @@ defmodule Logflare.Backends.Adaptor.BigQueryAdaptor do | |
| end | ||
|
|
||
| @spec maybe_warn_reservation_error( | ||
| decoded :: any(), | ||
| error :: QueryError.t(), | ||
| user :: User.t(), | ||
| project_id :: String.t(), | ||
| query_opts :: Keyword.t() | ||
| ) :: :ok | ||
| defp maybe_warn_reservation_error(decoded, %User{} = user, project_id, query_opts) do | ||
| if reservation_error?(decoded) and not caller_logs_own_errors?(query_opts) do | ||
| defp maybe_warn_reservation_error( | ||
| %QueryError{raw_error: error}, | ||
| %User{} = user, | ||
| project_id, | ||
| query_opts | ||
| ) do | ||
| with true <- reservation_error?(error), | ||
| false <- caller_logs_own_errors?(query_opts) do | ||
| Logger.warning("Possible BigQuery reservation error", | ||
| user_id: user.id, | ||
| project_id: project_id, | ||
| reservation: Keyword.get(query_opts, :reservation), | ||
| query_type: Keyword.get(query_opts, :query_type), | ||
| bq_error_message: decoded["message"] | ||
| bq_error_message: error["message"] | ||
| ) | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Logging now handled by backend adaptor, but note message ("Alert query execution failed with bad response") will have changed.