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

✨ Report a single log of evaluation steps #107

Merged
merged 1 commit into from
Sep 15, 2023
Merged
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
157 changes: 93 additions & 64 deletions lib/config_cat/rollout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,60 @@ defmodule ConfigCat.Rollout do
Config.settings()
) :: EvaluationDetails.t()
def evaluate(key, user, default_value, default_variation_id, settings) do
log_evaluating(key)

with {:ok, valid_user} <- validate_user(user),
{:ok, setting_descriptor} <- setting_descriptor(settings, key, default_value),
setting_variation <-
Map.get(setting_descriptor, Constants.variation_id(), default_variation_id),
rollout_rules <- Map.get(setting_descriptor, Constants.rollout_rules(), []),
percentage_rules <- Map.get(setting_descriptor, Constants.percentage_rules(), []),
{value, variation, rule, percentage_rule} <-
evaluate_rules(rollout_rules, percentage_rules, valid_user, key) do
variation = variation || setting_variation

value =
if value == :none do
base_value(setting_descriptor, default_value)
else
value
end

EvaluationDetails.new(
key: key,
matched_evaluation_rule: rule,
matched_evaluation_percentage_rule: percentage_rule,
user: user,
value: value,
variation_id: variation
)
else
{:error, :invalid_user} ->
log_invalid_user(key)
evaluate(key, nil, default_value, default_variation_id, settings)

{:error, message} ->
ConfigCatLogger.error(message, event_id: 1001)
{:ok, logs} = Agent.start(fn -> [] end)

try do
log_evaluating(logs, key, user)

with {:ok, valid_user} <- validate_user(user),
{:ok, setting_descriptor} <- setting_descriptor(settings, key, default_value),
setting_variation <-
Map.get(setting_descriptor, Constants.variation_id(), default_variation_id),
rollout_rules <- Map.get(setting_descriptor, Constants.rollout_rules(), []),
percentage_rules <- Map.get(setting_descriptor, Constants.percentage_rules(), []),
{value, variation, rule, percentage_rule} <-
evaluate_rules(rollout_rules, percentage_rules, valid_user, key, logs) do
variation = variation || setting_variation

value =
if value == :none do
base_value(setting_descriptor, default_value, logs)
else
value
end

EvaluationDetails.new(
default_value?: true,
error: message,
key: key,
matched_evaluation_rule: rule,
matched_evaluation_percentage_rule: percentage_rule,
user: user,
value: default_value,
variation_id: default_variation_id
value: value,
variation_id: variation
)
else
{:error, :invalid_user} ->
log_invalid_user(key)
evaluate(key, nil, default_value, default_variation_id, settings)

{:error, message} ->
ConfigCatLogger.error(message, event_id: 1001)

EvaluationDetails.new(
default_value?: true,
error: message,
key: key,
value: default_value,
variation_id: default_variation_id
)
end
after
logs
|> Agent.get(& &1)
|> Enum.reverse()
|> Enum.join("\n")
|> ConfigCatLogger.debug(event_id: 5000)

Agent.stop(logs)
end
end

Expand Down Expand Up @@ -87,17 +98,15 @@ defmodule ConfigCat.Rollout do
end
end

defp evaluate_rules([], [], _user, _key), do: {:none, nil, nil, nil}
defp evaluate_rules([], [], _user, _key, _logs), do: {:none, nil, nil, nil}

defp evaluate_rules(_rollout_rules, _percentage_rules, nil, key) do
defp evaluate_rules(_rollout_rules, _percentage_rules, nil, key, _logs) do
log_nil_user(key)
{:none, nil, nil, nil}
end

defp evaluate_rules(rollout_rules, percentage_rules, user, key) do
log_valid_user(user)

case evaluate_rollout_rules(rollout_rules, user, key) do
defp evaluate_rules(rollout_rules, percentage_rules, user, key, logs) do
case evaluate_rollout_rules(rollout_rules, user, key, logs) do
{:none, _, _} ->
{value, variation, rule} = evaluate_percentage_rules(percentage_rules, user, key)
{value, variation, nil, rule}
Expand All @@ -107,33 +116,42 @@ defmodule ConfigCat.Rollout do
end
end

defp evaluate_rollout_rules(rules, user, _key) do
Enum.reduce_while(rules, {:none, nil, nil}, &evaluate_rollout_rule(&1, &2, user))
defp evaluate_rollout_rules(rules, user, _key, logs) do
Enum.reduce_while(rules, {:none, nil, nil}, &evaluate_rollout_rule(&1, &2, user, logs))
end

defp evaluate_rollout_rule(rule, default, user) do
defp evaluate_rollout_rule(rule, default, user, logs) do
with comparison_attribute <- Map.get(rule, Constants.comparison_attribute()),
comparison_value <- Map.get(rule, Constants.comparison_value()),
comparator <- Map.get(rule, Constants.comparator()),
value <- Map.get(rule, Constants.value()),
variation <- Map.get(rule, Constants.variation_id()) do
case User.get_attribute(user, comparison_attribute) do
nil ->
log_no_match(comparison_attribute, nil, comparator, comparison_value)
log_no_match(logs, comparison_attribute, nil, comparator, comparison_value)
{:cont, default}

user_value ->
case Comparator.compare(comparator, to_string(user_value), to_string(comparison_value)) do
{:ok, true} ->
log_match(comparison_attribute, user_value, comparator, comparison_value, value)
log_match(
logs,
comparison_attribute,
user_value,
comparator,
comparison_value,
value
)

{:halt, {value, variation, rule}}

{:ok, false} ->
log_no_match(comparison_attribute, user_value, comparator, comparison_value)
log_no_match(logs, comparison_attribute, user_value, comparator, comparison_value)
{:cont, default}

{:error, error} ->
log_validation_error(
logs,
comparison_attribute,
user_value,
comparator,
Expand Down Expand Up @@ -188,37 +206,44 @@ defmodule ConfigCat.Rollout do
rem(hash_value, 100)
end

defp base_value(setting_descriptor, default_value) do
defp base_value(setting_descriptor, default_value, logs) do
result = Map.get(setting_descriptor, Constants.value(), default_value)
ConfigCatLogger.debug("Returning #{result}")
log(logs, "Returning #{result}")

result
end

defp log_evaluating(key) do
ConfigCatLogger.debug("Evaluating get_value('#{key}').")
defp log_evaluating(logs, key, user) do
log(logs, "Evaluating get_value('#{key}). User object:\n#{inspect(user)}")
end

defp log_match(comparison_attribute, user_value, comparator, comparison_value, value) do
ConfigCatLogger.debug(
defp log_match(logs, comparison_attribute, user_value, comparator, comparison_value, value) do
log(
logs,
"Evaluating rule: [#{comparison_attribute}:#{user_value}] [#{Comparator.description(comparator)}] [#{comparison_value}] => match, returning: #{value}"
)
end

defp log_no_match(comparison_attribute, user_value, comparator, comparison_value) do
ConfigCatLogger.debug(
defp log_no_match(logs, comparison_attribute, user_value, comparator, comparison_value) do
log(
logs,
"Evaluating rule: [#{comparison_attribute}:#{user_value}] [#{Comparator.description(comparator)}] [#{comparison_value}] => no match"
)
end

defp log_validation_error(comparison_attribute, user_value, comparator, comparison_value, error) do
ConfigCatLogger.warn(
defp log_validation_error(
logs,
comparison_attribute,
user_value,
comparator,
comparison_value,
error
) do
message =
"Evaluating rule: [#{comparison_attribute}:#{user_value}] [#{Comparator.description(comparator)}] [#{comparison_value}] => SKIP rule. Validation error: #{inspect(error)}"
)
end

defp log_valid_user(user) do
ConfigCatLogger.debug("User object: #{inspect(user)}")
ConfigCatLogger.warn(message)
log(logs, message)
end

defp log_nil_user(key) do
Expand All @@ -236,4 +261,8 @@ defmodule ConfigCat.Rollout do
event_id: 4001
)
end

defp log(logs, message) do
Agent.update(logs, &[message | &1])
end
end