Skip to content

Commit

Permalink
Adds configurable 'extra' options (pinterest#57)
Browse files Browse the repository at this point in the history
* adds extra options

* change extra to subscribe_options, add tests

* cleanup: more idiomatic test case
  • Loading branch information
ephe-meral authored and scohen committed Oct 24, 2016
1 parent 3ebedb9 commit 38940d9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}])
```
3 changes: 2 additions & 1 deletion lib/elixometer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/elixometer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 27 additions & 2 deletions test/support/test_reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,38 @@ 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

def value_for(metric_name, datapoint) 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

0 comments on commit 38940d9

Please sign in to comment.