diff --git a/config/runtime.exs b/config/runtime.exs index 0ecd2d04d558..def0f04d4aa9 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -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") diff --git a/lib/plausible/clickhouse_repo.ex b/lib/plausible/clickhouse_repo.ex index cf5c0dedf764..f6e493dcaa80 100644 --- a/lib/plausible/clickhouse_repo.ex +++ b/lib/plausible/clickhouse_repo.ex @@ -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) diff --git a/lib/plausible_web/controllers/api/system_controller.ex b/lib/plausible_web/controllers/api/system_controller.ex index 4e5c75648217..cbd3f5b63af8 100644 --- a/lib/plausible_web/controllers/api/system_controller.ex +++ b/lib/plausible_web/controllers/api/system_controller.ex @@ -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 =