Skip to content

Commit

Permalink
make credo happy
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Sep 4, 2017
1 parent e46d627 commit 27ee8f5
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, false},
{Credo.Check.Readability.VariableNames},

{Credo.Check.Refactor.ABCSize},
Expand All @@ -41,7 +42,7 @@
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.Nesting, max_nesting: 3},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.IExPry},
Expand Down
2 changes: 0 additions & 2 deletions lib/prometheus.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ defmodule Prometheus do
- [Phoenix Instrumenter](https://hex.pm/packages/prometheus_phoenix);
- [RabbitMQ Exporter](https://github.com/deadtrickster/prometheus_rabbitmq_exporter).
## Erlang VM Collectors
- [Memory Collector](vm-memory-collector.html);
- [Statistics Collector](vm-statistics-collector.html);
Expand Down Expand Up @@ -103,7 +102,6 @@ defmodule Prometheus do
- `Prometheus.Model` - provides API for working with underlying Prometheus models.
You'll use that if you want to create custom collector.
"""

defmacro __using__(_opts) do
Expand Down
13 changes: 6 additions & 7 deletions lib/prometheus/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ defmodule Prometheus.Config do
end

defp config(key, option) do
try do
key
|> config()
|> Keyword.fetch!(option)
rescue
e in KeyError -> raise %KeyNotFoundError{key: key, option: option}
end
key
|> config()
|> Keyword.fetch!(option)
rescue
# credo:disable-for-next-line Credo.Check.Warning.RaiseInsideRescue
e in KeyError -> raise %KeyNotFoundError{key: key, option: option}
end

unquote do
Expand Down
4 changes: 2 additions & 2 deletions lib/prometheus/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule Prometheus.Erlang do
defp parse_metric_call_args(mf_or_spec, spec, arguments) do
case mf_or_spec do
## Erlang.metric_call({:prometheus_counter, :dinc}, spec, [value])
{_,_} -> {mf_or_spec, spec, arguments}
{_, _} -> {mf_or_spec, spec, arguments}
## Erlang.metric_call(:inc, spec, [value])
_ when is_atom(mf_or_spec) -> {mf_or_spec, spec, arguments}
_ ->
Expand Down Expand Up @@ -108,7 +108,7 @@ defmodule Prometheus.Erlang do
_ when is_list(mf) ->
{f, _arity} = __CALLER__.function
{Module.get_attribute(__CALLER__.module, :erlang_module), f}
{_,_} ->
{_, _} ->
mf
_ when is_atom(mf) ->
{Module.get_attribute(__CALLER__.module, :erlang_module), mf}
Expand Down
5 changes: 2 additions & 3 deletions lib/prometheus/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ end
defmodule Prometheus.UnknownMetricError do
defexception [:registry, :name]


def message(%{registry: registry, name: name}) do
"Unknown metric {registry: #{registry}, name: #{name}}."
end
Expand Down Expand Up @@ -155,8 +154,8 @@ end
defmodule Prometheus.Error do
@moduledoc false

@lint [{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.CyclomaticComplexity, false}]
# credo:disable-for-this-file Credo.Check.Refactor.ABCSize
# credo:disable-for-this-file Credo.Check.Refactor.CyclomaticComplexity
def normalize(erlang_error) do
case erlang_error do
%ErlangError{original: original} ->
Expand Down
42 changes: 24 additions & 18 deletions lib/prometheus/metric.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ defmodule Prometheus.Metric do
module_name = __CALLER__.module

quote do
# credo:disable-for-next-line Credo.Check.Readability.SpaceAfterCommas
alias Prometheus.Metric.{Counter,Gauge,Histogram,Summary,Boolean}
# credo:disable-for-next-line Credo.Check.Readability.SpaceAfterCommas
require Prometheus.Metric.{Counter,Gauge,Histogram,Summary,Boolean}
require Prometheus.Error

unquote_splicing(
for metric <- @metrics do
quote do
Module.register_attribute unquote(module_name), unquote(metric), accumulate: true
Module.register_attribute(unquote(module_name), unquote(metric),
accumulate: true)
end
end)

Expand Down Expand Up @@ -66,24 +69,27 @@ defmodule Prometheus.Metric do
end
end

unquote(
case get_on_load_attribute(env.module) do
nil ->
quote do
@on_load :__declare_prometheus_metrics__
end
on_load ->
Module.delete_attribute(env.module, :on_load)
Module.put_attribute(env.module, :on_load, :__prometheus_on_load_override__)
quote do
def __prometheus_on_load_override__() do
case unquote(on_load)() do
:ok -> __declare_prometheus_metrics__()
result -> result
end
end
unquote(gen_on_load(env))
end
end

defp gen_on_load(env) do
case get_on_load_attribute(env.module) do
nil ->
quote do
@on_load :__declare_prometheus_metrics__
end
on_load ->
Module.delete_attribute(env.module, :on_load)
Module.put_attribute(env.module, :on_load, :__prometheus_on_load_override__)
quote do
def __prometheus_on_load_override__() do
case unquote(on_load)() do
:ok -> __declare_prometheus_metrics__()
result -> result
end
end)
end
end
end
end

Expand Down
12 changes: 5 additions & 7 deletions test/contrib/mnesia_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ defmodule Prometheus.Contrib.MnesiaTest do
end

defp set_custom_mnesia_dir(dir) do
try do
:ets.lookup_element(:mnesia_gvar, :dir, 2)
:ets.update_element(:mnesia_gvar, :dir, dir)
rescue
ArgumentError ->
:application.set_env(:mnesia, :dir, dir)
end
:ets.lookup_element(:mnesia_gvar, :dir, 2)
:ets.update_element(:mnesia_gvar, :dir, dir)
rescue
ArgumentError ->
:application.set_env(:mnesia, :dir, dir)
end

end
1 change: 1 addition & 0 deletions test/format/protobuf_test.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# credo:disable-for-this-file Credo.Check.Readability.SpaceAfterCommas
defmodule Prometheus.Format.ProtobufTest do
use Prometheus.Case

Expand Down
2 changes: 1 addition & 1 deletion test/injector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ defmodule Prometheus.InjectorTest do
QweInjector.inject_ do
IO.puts("Doing dangerous work #{x}")
rescue
_-> IO.puts("Died")
_ -> IO.puts("Died")
after
IO.puts("Done anyway")
end
Expand Down
6 changes: 3 additions & 3 deletions test/metric/counter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ defmodule Prometheus.CounterTest do
Counter.count_no_exceptions [name: :no_exceptions_total] do
Counter.count_exceptions [name: :exceptions_total], ArithmeticError do
def sometimes_raise(arg) do
5/arg
5 / arg
end
end

def sometimes_raise1(arg) when is_list(arg) do
5/arg
5 / arg
end
end

Expand All @@ -262,7 +262,7 @@ defmodule Prometheus.CounterTest do

Counter.count_exceptions [name: :exceptions_total] do
def sometimes_raise_any(arg) do
5/arg
5 / arg
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/metric/gauge_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ defmodule Prometheus.GaugeTest do

Gauge.dec(spec)
Gauge.dec(spec, 3)
assert -4 == Gauge.value(spec)
assert (-4 == Gauge.value(spec))

Gauge.reset(spec)

Expand All @@ -319,7 +319,7 @@ defmodule Prometheus.GaugeTest do

## ddec is async. let's make sure gen_server processed our request
Process.sleep(10)
assert -4.5 == Gauge.value(spec)
assert (-4.5 == Gauge.value(spec))

Gauge.reset(spec)

Expand Down Expand Up @@ -364,7 +364,6 @@ defmodule Prometheus.GaugeTest do

assert 1 == Gauge.track_inprogress(spec, do: Gauge.value(spec))


assert_raise ErlangError, fn ->
Gauge.track_inprogress spec do
:erlang.error({:qwe})
Expand Down
6 changes: 4 additions & 2 deletions test/metric_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ defmodule Prometheus.MetricTest do
assert false == Summary.declare(name: :test_summary1, labels: [], help: "qwe")
assert false == Summary.declare(name: :test_summary2, labels: [:tag], help: "qwa")

assert false == Histogram.declare(name: :test_histogram1, labels: [], buckets: [1, 2], help: "qwe")
assert false == Histogram.declare(name: :test_histogram2, labels: [:tag], buckets: [1, 2], help: "qwa")
assert false ==
Histogram.declare(name: :test_histogram1, labels: [], buckets: [1, 2], help: "")
assert false ==
Histogram.declare(name: :test_histogram2, labels: [:tag], buckets: [1, 2], help: "")
end
end
8 changes: 4 additions & 4 deletions test/model_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ defmodule Prometheus.ModelTest do
end

test "create_mf" do
assert {:MetricFamily,<<"pool_size">>,<<"help">>,:UNTYPED,
[{:Metric,[],:undefined,:undefined,:undefined,
{:Untyped,365},
:undefined,:undefined}]} ==
assert {:MetricFamily, <<"pool_size">>, <<"help">>, :UNTYPED,
[{:Metric, [], :undefined, :undefined, :undefined,
{:Untyped, 365},
:undefined, :undefined}]} ==
Prometheus.Model.create_mf(
:pool_size, "help", :untyped, Prometheus.ModelTest, :undefined)
end
Expand Down

0 comments on commit 27ee8f5

Please sign in to comment.