Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ ch_transport_opts =
config :plausible, Plausible.ClickhouseRepo,
queue_target: 500,
queue_interval: 2000,
timeout: 15_000,
timeout: 1_000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err don't think we can afford that

url: ch_db_url,
transport_opts: ch_transport_opts,
settings: [
Expand All @@ -638,7 +638,7 @@ config :plausible, Plausible.ClickhouseRepo,
# NB! when :timeout is overridden to be over 20s,
# for it to have meaningful effect,
# this must be overridden as well
max_execution_time: 20
max_execution_time: 2
]

config :plausible, Plausible.IngestRepo,
Expand Down
37 changes: 34 additions & 3 deletions lib/plausible/stats/query_runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ defmodule Plausible.Stats.QueryRunner do
use Plausible
use Plausible.ClickhouseRepo

require Logger
require OpenTelemetry.Tracer, as: Tracer

alias Plausible.Stats.{
Comparisons,
Compare,
Expand Down Expand Up @@ -111,9 +114,37 @@ defmodule Plausible.Stats.QueryRunner do
end

defp execute_query(query, site) do
query
|> SQL.QueryBuilder.build(site)
|> ClickhouseRepo.all(query: query)
try do
query
|> SQL.QueryBuilder.build(site)
|> ClickhouseRepo.all(query: query)
rescue
e in [Ch.Error, DBConnection.ConnectionError, Mint.TransportError] ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have all that in log_comment?

ConnectionError AFAIU may be unrelated to the query itself, but a failing checkout for example caused by the query previously made - see plausible/ch#271

message = Exception.message(e)
Logger.error("ClickHouse query error: #{message}")

Tracer.set_status(:error, message)

Tracer.set_attributes([
{"plausible.query.error", message},
{"plausible.query.error.kind", inspect(e.__struct__)}
])

Sentry.Context.set_extra_context(%{
site_domain: site.domain,
site_id: site.id,
metrics: inspect(query.metrics),
dimensions: inspect(query.dimensions),
date_range: inspect(query.utc_time_range),
filters: inspect(query.filters)
})

Sentry.Context.set_tags_context(%{
source: "clickhouse_query_timeout"
})

reraise e, __STACKTRACE__
end
end

defp build_from_ch(ch_results, query) do
Expand Down
6 changes: 5 additions & 1 deletion lib/plausible_web/plugs/error_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ defmodule PlausibleWeb.Plugs.ErrorHandler do
use Plug.ErrorHandler

@impl Plug.ErrorHandler
def handle_errors(conn, %{kind: kind, reason: reason}) do
def handle_errors(conn, %{kind: :error, reason: reason, stack: stack}) do
OpenTelemetry.Tracer.set_status(:error, Exception.message(reason))
Sentry.capture_exception(reason, stacktrace: stack, handled: false)
json(conn, %{error: "internal server error"})
end

def handle_errors(conn, _), do: json(conn, %{error: "internal server error"})
end
end
end
Loading