Skip to content

Commit

Permalink
mix format exs
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed May 27, 2020
1 parent a813362 commit 30c9996
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 125 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use Mix.Config

import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
12 changes: 6 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ defmodule Instruments.Mixfile do
name: "Instruments",
version: @version,
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
elixirc_paths: compile_paths(Mix.env),
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
elixirc_paths: compile_paths(Mix.env()),
deps: deps(),
docs: docs(),
package: package(),
Expand All @@ -27,8 +27,7 @@ defmodule Instruments.Mixfile do
"pages/Configuration.md",
"pages/Probes.md",
"pages/Performance.md"
],

]
]
end

Expand All @@ -42,6 +41,7 @@ defmodule Instruments.Mixfile do
def compile_paths(:test) do
default_compile_path() ++ ["test/support"]
end

def compile_paths(_), do: default_compile_path()

defp default_compile_path(), do: ["lib"]
Expand All @@ -50,7 +50,7 @@ defmodule Instruments.Mixfile do
[
{:ex_doc, "~> 0.19", only: :dev, runtime: false},
{:recon, "~> 2.3.1"},
{:statix, "~> 1.2.1"},
{:statix, "~> 1.2.1"}
]
end

Expand Down
39 changes: 20 additions & 19 deletions test/custom_functions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,58 @@ defmodule Instruments.CustomFunctionsTest do
end

describe "adding a prefix" do

test "to increment calls" do
Custom.increment("foo.bar.baz")
assert_metric_reported :increment, "custom.foo.bar.baz", 1
assert_metric_reported(:increment, "custom.foo.bar.baz", 1)

Custom.increment("foo.bar.baz", 3)
assert_metric_reported :increment, "custom.foo.bar.baz", 3
assert_metric_reported(:increment, "custom.foo.bar.baz", 3)

Custom.increment("foo.bar.baz", 4, tags: ["stinky"])
assert_metric_reported :increment, "custom.foo.bar.baz", 4, tags: ["stinky"]
assert_metric_reported(:increment, "custom.foo.bar.baz", 4, tags: ["stinky"])
end

test "to decrement calls" do
Custom.decrement("foo.bar.bax")
assert_metric_reported :decrement, "custom.foo.bar.bax", 1
assert_metric_reported(:decrement, "custom.foo.bar.bax", 1)

Custom.decrement("foo.bar.bax", 3)
assert_metric_reported :decrement, "custom.foo.bar.bax", 3
assert_metric_reported(:decrement, "custom.foo.bar.bax", 3)

Custom.decrement("foo.bar.baz", 4, tags: ["stinky"])
assert_metric_reported :decrement, "custom.foo.bar.baz", 4, tags: ["stinky"]
assert_metric_reported(:decrement, "custom.foo.bar.baz", 4, tags: ["stinky"])
end

test "to gauge calls" do
Custom.gauge("my.gauge", 384)
assert_metric_reported :gauge, "custom.my.gauge", 384
assert_metric_reported(:gauge, "custom.my.gauge", 384)

Custom.gauge("my.gauge", 946, tags: ["sweet_gauge"])
assert_metric_reported :gauge, "custom.my.gauge", 946, tags: ["sweet_gauge"]
assert_metric_reported(:gauge, "custom.my.gauge", 946, tags: ["sweet_gauge"])
end

test "to histogram calls" do
Custom.histogram("my.histogram", 900, sample_rate: 1.0)
assert_metric_reported :histogram, "custom.my.histogram", 900
assert_metric_reported(:histogram, "custom.my.histogram", 900)

Custom.histogram("my.histogram", 901, tags: ["cool_metric"], sample_rate: 1.0)
assert_metric_reported :histogram, "custom.my.histogram", 901, tags: ["cool_metric"]
assert_metric_reported(:histogram, "custom.my.histogram", 901, tags: ["cool_metric"])
end

test "to timing calls" do
Custom.timing("my.timing", 900, sample_rate: 1.0)
assert_metric_reported :timing, "custom.my.timing", 900
assert_metric_reported(:timing, "custom.my.timing", 900)

Custom.timing("my.timing", 901, tags: ["speed:fast"], sample_rate: 1.0)
assert_metric_reported :timing, "custom.my.timing", 901, tags: ["speed:fast"]
assert_metric_reported(:timing, "custom.my.timing", 901, tags: ["speed:fast"])
end

test "to set calls" do
Custom.set("my.set", 900)
assert_metric_reported :set, "custom.my.set", 900
assert_metric_reported(:set, "custom.my.set", 900)

Custom.set("my.set", 901, tags: ["speed:fast"])
assert_metric_reported :set, "custom.my.set", 901, tags: ["speed:fast"]
assert_metric_reported(:set, "custom.my.set", 901, tags: ["speed:fast"])
end

test "to measure_calls" do
Expand All @@ -77,10 +76,12 @@ defmodule Instruments.CustomFunctionsTest do
end

assert :done == Custom.measure("my.measure", [sample_rate: 1.0], func)
assert_metric_reported :timing, "custom.my.measure", (10..12)
assert_metric_reported(:timing, "custom.my.measure", 10..12)

assert :done ==
Custom.measure("my.measure", [sample_rate: 1.0, tags: ["timing:short"]], func)

assert :done == Custom.measure("my.measure", [sample_rate: 1.0, tags: ["timing:short"]], func)
assert_metric_reported :timing, "custom.my.measure", (10..11), tags: ["timing:short"]
assert_metric_reported(:timing, "custom.my.measure", 10..11, tags: ["timing:short"])
end
end

Expand All @@ -90,6 +91,6 @@ defmodule Instruments.CustomFunctionsTest do
end

RuntimePrefix.increment("foo.bar", 3)
assert_metric_reported :increment, "foobar.foo.bar", 3
assert_metric_reported(:increment, "foobar.foo.bar", 3)
end
end
81 changes: 46 additions & 35 deletions test/instruments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,105 +11,115 @@ defmodule InstrumentsTest do

test "setting a gauge value" do
Instruments.gauge("foo.bar", 3)
assert_metric_reported :gauge, "foo.bar"
assert_metric_reported(:gauge, "foo.bar")

Instruments.gauge("foo.bar", 6, tags: ["my:tag"])
assert_metric_reported :gauge, "foo.bar", 6, tags: ["my:tag"]
assert_metric_reported(:gauge, "foo.bar", 6, tags: ["my:tag"])

Instruments.gauge("foo.bar", 6, tags: ["my:tag"], sample_rate: 1.0)
assert_metric_reported :gauge, "foo.bar", 6, tags: ["my:tag"], sample_rate: 1.0
assert_metric_reported(:gauge, "foo.bar", 6, tags: ["my:tag"], sample_rate: 1.0)

gauge_name = "fail_amount"
Instruments.gauge("foo.bar.#{gauge_name}", 284)
assert_metric_reported :gauge, "foo.bar.fail_amount", 284
assert_metric_reported(:gauge, "foo.bar.fail_amount", 284)
end

test "incrementing a counter" do
Instruments.increment("my.counter", 6)
assert_metric_reported :increment, "my.counter", 6
assert_metric_reported(:increment, "my.counter", 6)

Instruments.increment("my.counter", 6, tags: ["my:counter_tag"])
assert_metric_reported :increment, "my.counter", 6, tags: ["my:counter_tag"]
assert_metric_reported(:increment, "my.counter", 6, tags: ["my:counter_tag"])

Instruments.increment("my.counter", 6, tags: ["my:counter_tag"], sample_rate: 1.0)
assert_metric_reported :increment, "my.counter", 6, tags: ["my:counter_tag"], sample_rate: 1.0

assert_metric_reported(:increment, "my.counter", 6, tags: ["my:counter_tag"], sample_rate: 1.0)

counter_name = :stinky
Instruments.increment("my.counter.#{counter_name}", 6)
assert_metric_reported :increment, "my.counter.stinky", 6
assert_metric_reported(:increment, "my.counter.stinky", 6)
end

test "decrementing a counter" do
Instruments.decrement("my.counter", 6)
assert_metric_reported :decrement, "my.counter", 6
assert_metric_reported(:decrement, "my.counter", 6)

Instruments.decrement("my.counter", 6, tags: ["my:counter_tag"])
assert_metric_reported :decrement, "my.counter", 6, tags: ["my:counter_tag"]
assert_metric_reported(:decrement, "my.counter", 6, tags: ["my:counter_tag"])

Instruments.decrement("my.counter", 6, tags: ["my:counter_tag"], sample_rate: 1.0)
assert_metric_reported :decrement, "my.counter", 6, tags: ["my:counter_tag"], sample_rate: 1.0

assert_metric_reported(:decrement, "my.counter", 6, tags: ["my:counter_tag"], sample_rate: 1.0)

counter_name = "decrementer"
Instruments.decrement("my.#{counter_name}.requests", 9)
assert_metric_reported :decrement, "my.decrementer.requests", 9
assert_metric_reported(:decrement, "my.decrementer.requests", 9)
end

test "sending a timing metric" do
Instruments.timing("my.timer", 3000, sample_rate: 1.0)
assert_metric_reported :timing, "my.timer", 3000, sample_rate: 1.0
assert_metric_reported(:timing, "my.timer", 3000, sample_rate: 1.0)

Instruments.timing("my.timer", 3000, tags: ["timing:slow"], sample_rate: 1.0)
assert_metric_reported :timing, "my.timer", 3000, tags: ["timing:slow"], sample_rate: 1.0
assert_metric_reported(:timing, "my.timer", 3000, tags: ["timing:slow"], sample_rate: 1.0)

Instruments.timing("my.timer", 3000, tags: ["timing:slow"], sample_rate: 1.0)
assert_metric_reported :timing, "my.timer", 3000, tags: ["timing:slow"], sample_rate: 1.0
assert_metric_reported(:timing, "my.timer", 3000, tags: ["timing:slow"], sample_rate: 1.0)

rpc_name = "get_user"
Instruments.timing("rpc.#{rpc_name}.response_time", 29, sample_rate: 1.0)
assert_metric_reported :timing, "rpc.get_user.response_time", 29
assert_metric_reported(:timing, "rpc.get_user.response_time", 29)
end

test "measuring a timed metric" do
pauser = fn ->
:timer.sleep(10)
end

Instruments.measure("my_timed_metric",[sample_rate: 1.0], pauser)
assert_metric_reported :timing, "my_timed_metric", (10..15)
Instruments.measure("my_timed_metric", [sample_rate: 1.0], pauser)
assert_metric_reported(:timing, "my_timed_metric", 10..15)

Instruments.measure("my_timed_metric", [sample_rate: 1.0, tags: ["my:pause"]], pauser)
assert_metric_reported :timing, "my_timed_metric", (10..15), tags: ["my:pause"]
assert_metric_reported(:timing, "my_timed_metric", 10..15, tags: ["my:pause"])

Instruments.measure("my_timed_metric", [tags: ["my:pause"], sample_rate: 1.0], pauser)
assert_metric_reported :timing, "my_timed_metric", (10..15), tags: ["my:pause"], sample_rate: 1.0

assert_metric_reported(:timing, "my_timed_metric", 10..15,
tags: ["my:pause"],
sample_rate: 1.0
)

rpc_name = "delete_user"
Instruments.measure("rpc.#{rpc_name}", [sample_rate: 1.0], pauser)
assert_metric_reported :timing, "rpc.delete_user", (10..15)
assert_metric_reported(:timing, "rpc.delete_user", 10..15)
end

test "setting a histogram" do
Instruments.histogram("my.histogram", 29, sample_rate: 1.0)
assert_metric_reported :histogram, "my.histogram", 29
assert_metric_reported(:histogram, "my.histogram", 29)

Instruments.histogram("my.histogram", 949, tags: ["rpc:call", "other:data"], sample_rate: 1.0)
assert_metric_reported :histogram, "my.histogram", 949, tags: ["rpc:call", "other:data"]
assert_metric_reported(:histogram, "my.histogram", 949, tags: ["rpc:call", "other:data"])

Instruments.histogram("my.histogram", 949, tags: ["rpc:call", "other:data"], sample_rate: 1.0)
assert_metric_reported :histogram, "my.histogram", 949, tags: ["rpc:call", "other:data"], sample_rate: 1.0

assert_metric_reported(:histogram, "my.histogram", 949,
tags: ["rpc:call", "other:data"],
sample_rate: 1.0
)

histogram_name = "friend_count"
Instruments.histogram("discord.users.#{histogram_name}", 29, sample_rate: 1.0)
assert_metric_reported :histogram, "discord.users.friend_count", 29
assert_metric_reported(:histogram, "discord.users.friend_count", 29)
end

test "setting a set value" do
Instruments.set("my.set", 629)
assert_metric_reported :set, "my.set", 629
assert_metric_reported(:set, "my.set", 629)

set_name = "custom_set"
Instruments.set("discord.#{set_name}", 830)
assert_metric_reported :set, "discord.custom_set", 830
assert_metric_reported(:set, "discord.custom_set", 830)
end

test "sending events" do
Expand All @@ -119,7 +129,7 @@ defmodule InstrumentsTest do

set_name = "dirty"
Instruments.send_event("my_stuff.#{set_name}", "clothes")
assert_metric_reported :event, "my_stuff.dirty", "clothes"
assert_metric_reported(:event, "my_stuff.dirty", "clothes")
end

test "sending events with tags" do
Expand All @@ -128,13 +138,14 @@ defmodule InstrumentsTest do
end

test "sending events with a title that's a variable blows up" do
quoted = quote do
use Instruments

val = "43"
interp = "this is my title #{val}"
Instruments.send_event(interp, "my_text")
end
quoted =
quote do
use Instruments

val = "43"
interp = "this is my title #{val}"
Instruments.send_event(interp, "my_text")
end

assert_raise CompileError, ~r/Metric keys must be defined statically/, fn ->
Code.eval_quoted(quoted)
Expand Down
17 changes: 11 additions & 6 deletions test/macro_helpers.test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule Instruments.MacroHelpersTest do
assert ["foo.", ^metric_var, ".bar"] = to_iolist(quote do: "foo.#{metric_name}.bar")
end


test "should work with an interpolated string at the beginning" do
metric_var = Macro.var(:metric_1, __MODULE__)
assert [^metric_var, ".second"] = to_iolist(quote do: "#{metric_1}.second")
Expand All @@ -26,7 +25,8 @@ defmodule Instruments.MacroHelpersTest do
prefix_var = Macro.var(:prefix, __MODULE__)
suffix_var = Macro.var(:suffix, __MODULE__)

assert [^prefix_var, ".something.", ^suffix_var] = to_iolist(quote do: "#{prefix}.something.#{suffix}")
assert [^prefix_var, ".something.", ^suffix_var] =
to_iolist(quote do: "#{prefix}.something.#{suffix}")
end

test "it should work with string concatenation" do
Expand All @@ -39,9 +39,14 @@ defmodule Instruments.MacroHelpersTest do
metric_var = Macro.var(:metric_name, __MODULE__)

assert ["foo", ".", "bar", ".", "baz"] == to_iolist(quote do: ["foo", ".", "bar", ".", "baz"])
assert ["foo", ".", "bar", ".", ^metric_var] = to_iolist(quote do: ["foo", ".", "bar", ".", metric_name])
assert ["foo", ".", ^metric_var, ".", "baz"] = to_iolist(quote do: ["foo", ".", metric_name, ".", "baz"])
assert [^metric_var, ".", "bar", ".", "baz"] = to_iolist(quote do: [metric_name, ".", "bar", ".", "baz"])
end

assert ["foo", ".", "bar", ".", ^metric_var] =
to_iolist(quote do: ["foo", ".", "bar", ".", metric_name])

assert ["foo", ".", ^metric_var, ".", "baz"] =
to_iolist(quote do: ["foo", ".", metric_name, ".", "baz"])

assert [^metric_var, ".", "bar", ".", "baz"] =
to_iolist(quote do: [metric_name, ".", "bar", ".", "baz"])
end
end
Loading

0 comments on commit 30c9996

Please sign in to comment.