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
3 changes: 2 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,8 @@ config :plausible, Plausible.IngestRepo,
max_buffer_size: ch_max_buffer_size,
pool_size: ingest_pool_size,
settings: [
materialized_views_ignore_errors: 1
materialized_views_ignore_errors: 1,
workload: "ingestion"
],
table_settings: [
storage_policy: get_var_from_path_or_env(config_dir, "CLICKHOUSE_DEFAULT_STORAGE_POLICY")
Expand Down
49 changes: 47 additions & 2 deletions lib/plausible/clickhouse_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,58 @@ defmodule Plausible.ClickhouseRepo do
log_comment = Jason.encode!(log_comment_data)

opts =
Keyword.update(opts, :settings, [log_comment: log_comment], fn settings ->
[{:log_comment, log_comment} | settings]
opts
|> Keyword.update(:settings, [log_comment: log_comment], fn current_settings ->
[{:log_comment, log_comment} | current_settings]
end)

opts =
if plausible_query do
opts
|> Keyword.update!(:settings, fn current_settings ->
current_settings |> Enum.concat(get_extra_connection_settings(log_comment_data))
end)
|> Keyword.update!(:settings, fn current_settings ->
should_use_workload? =
Map.get(plausible_query.debug_metadata, :phoenix_controller, nil) in [
PlausibleWeb.Api.ExternalStatsController |> to_string(),
PlausibleWeb.Api.ExternalQueryApiController |> to_string()
]

if should_use_workload? do
[{:workload, "external_api"} | current_settings]
else
current_settings
end
end)
else
opts
end

{query, opts}
end

defp get_extra_connection_settings(%{params: params}) do
keys =
params
|> Map.keys()
|> Enum.filter(fn k ->
case k do
"clickhouse_readonly" -> false
"clickhouse_" <> _k -> true
_ -> false
end
end)

Enum.map(keys, fn k ->
{k |> String.trim_leading("clickhouse_") |> String.to_atom(), params[k]}
end)
end

defp get_extra_connection_settings(_) do
[]
end

def get_config_without_ch_query_execution_timeout() do
{settings, config} = Plausible.ClickhouseRepo.config() |> Keyword.pop!(:settings)

Expand Down
2 changes: 1 addition & 1 deletion lib/plausible_web/controllers/api/system_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule PlausibleWeb.Api.SystemController do

clickhouse_health_task =
Task.async(fn ->
Ecto.Adapters.SQL.query(Plausible.ClickhouseRepo, "SELECT 1", [])
Plausible.ClickhouseRepo.query("SELECT 1", [], [settings: [workload: "ingestion"]])
end)

postgres_health =
Expand Down
Loading