From 38940d924e1580779c7d3b84e1682f7a1192dd04 Mon Sep 17 00:00:00 2001 From: Johanna Appel Date: Mon, 24 Oct 2016 20:15:56 +0200 Subject: [PATCH] Adds configurable 'extra' options (#57) * adds extra options * change extra to subscribe_options, add tests * cleanup: more idiomatic test case --- README.md | 8 ++++++++ lib/elixometer.ex | 3 ++- test/elixometer_test.exs | 8 ++++++++ test/support/test_reporter.ex | 29 +++++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ec10e87..3e59fdc 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,11 @@ By default, Elixometer only includes the `exometer_core` package. However, some ] end ``` + +In case a reporter allows for extra configuration options on subscribe, you can configure them in your `elixometer` config like so: + +```elixir + config(:elixometer, + ... + subscribe_options: [{:tag, :value1}]) +``` diff --git a/lib/elixometer.ex b/lib/elixometer.ex index 1a19bca..d36049a 100644 --- a/lib/elixometer.ex +++ b/lib/elixometer.ex @@ -345,11 +345,12 @@ defmodule Elixometer do cfg = Application.get_all_env(:elixometer) reporter = cfg[:reporter] interval = cfg[:update_frequency] + subscribe_options = cfg[:subscribe_options] || true if reporter do :exometer.info(metric_name) |> Keyword.get(:datapoints) - |> Enum.map(&(:exometer_report.subscribe(reporter, metric_name, &1, interval))) + |> Enum.map(&(:exometer_report.subscribe(reporter, metric_name, &1, interval, subscribe_options))) end :ets.insert(@elixometer_table, {{:subscriptions, metric_name}, true}) end diff --git a/test/elixometer_test.exs b/test/elixometer_test.exs index 1a23f7b..df86f76 100644 --- a/test/elixometer_test.exs +++ b/test/elixometer_test.exs @@ -281,4 +281,12 @@ defmodule ElixometerTest do assert {:error, :not_found} == get_metric_value("elixometer.test.bad.bad") end + test "a subscription that has additional subscription options" do + Application.put_env(:elixometer, :subscribe_options, [some_option: 42]) + + update_counter("subscribe_options", 1) + + assert subscription_exists "elixometer.test.counters.subscribe_options" + assert [some_option: 42] = Reporter.options_for("elixometer.test.counters.subscribe_options") + end end diff --git a/test/support/test_reporter.ex b/test/support/test_reporter.ex index 85c7152..51d3d42 100644 --- a/test/support/test_reporter.ex +++ b/test/support/test_reporter.ex @@ -35,8 +35,7 @@ defmodule Elixometer.TestReporter do def value_for(metric_name, datapoint) when is_bitstring(metric_name) do metric_name - |> String.split(".") - |> Enum.map(&String.to_atom/1) + |> to_elixometer_name |> value_for(datapoint) end @@ -44,4 +43,30 @@ defmodule Elixometer.TestReporter do {:ok, value} = :exometer.get_value(metric_name, datapoint) value end + + def options_for(metric_name) when is_bitstring(metric_name) do + metric_name + |> to_elixometer_name + |> options_for + end + + def options_for(metric_name) do + Application.get_env(:elixometer, :reporter) + |> :exometer_report.list_subscriptions + |> Enum.filter_map( + fn {name, _, _, _} -> name == metric_name end, + fn {_, _, _, extra} -> extra end) + |> case do + [hd | _] -> hd + _ -> nil + end + end + + # Helpers + + defp to_elixometer_name(metric_name) do + metric_name + |> String.split(".") + |> Enum.map(&String.to_atom/1) + end end