diff --git a/README.md b/README.md index 4480432..b8c86a6 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ dogstatsd:gauge("users.active", UserCount, #{ shard => ShardId, version => Vsn } ### Elixir +For more details, see the example application in (examples/elixir)[examples/elixir] + 1. List dogstatsd dependency in your `mix.exs` file ```elixir @@ -63,7 +65,7 @@ dogstatsd:gauge("users.active", UserCount, #{ shard => ShardId, version => Vsn } 5. For custom metrics: ```elixir -Dogstatsd.gauge("users.active", user_count, %{ :shard => shard_id, :version => vsn }) +:dogstatsd.gauge("users.active", user_count, %{ :shard => shard_id, :version => vsn }) ``` ### VM Stats diff --git a/examples/elixir/.gitignore b/examples/elixir/.gitignore new file mode 100644 index 0000000..91b5343 --- /dev/null +++ b/examples/elixir/.gitignore @@ -0,0 +1,19 @@ +# The directory Mix will write compiled artifacts to. +/_build + +# If you run "mix test --cover", coverage assets end up here. +/cover + +# The directory Mix downloads your dependencies sources to. +/deps + +# Where 3rd-party dependencies like ExDoc output generated docs. +/doc + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez +elixir_example +mix.lock diff --git a/examples/elixir/README.md b/examples/elixir/README.md new file mode 100644 index 0000000..8c1f3f9 --- /dev/null +++ b/examples/elixir/README.md @@ -0,0 +1,23 @@ +# ElixirExample + +Example of some Elixir code using the Dogstatsd library + +## Mix.exs + +`:dogstatsd` is listed as an application dependency so it will start before your application. + +``` + def application do + [applications: [:logger, :dogstatsd]] + end +``` + +`:dogstatsd` is listed as a dependency so Mix will fetch it. (You will actually want to depend on `{:dogstatsd, "~> ", hex: :dogstatsde}`, not a relative path!) + +``` + defp deps do + [ + {:dogstatsd, path: "../../"} + ] + end +``` diff --git a/examples/elixir/config/config.exs b/examples/elixir/config/config.exs new file mode 100644 index 0000000..c842e32 --- /dev/null +++ b/examples/elixir/config/config.exs @@ -0,0 +1,30 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +use Mix.Config + +# This configuration is loaded before any dependency and is restricted +# to this project. If another project depends on this project, this +# file won't be loaded nor affect the parent project. For this reason, +# if you want to provide default values for your application for +# 3rd-party users, it should be done in your "mix.exs" file. + +# You can configure for your application as: +# +# config :elixir_example, key: :value +# +# And access this configuration in your application as: +# +# Application.get_env(:elixir_example, :key) +# +# Or configure a 3rd-party app: +# +# config :logger, level: :info +# + +# It is also possible to import configuration files, relative to this +# directory. For example, you can emulate configuration per environment +# by uncommenting the line below and defining dev.exs, test.exs and such. +# Configuration from the imported file will override the ones defined +# here (which is why it is important to import them last). +# +# import_config "#{Mix.env}.exs" diff --git a/examples/elixir/lib/elixir_example.ex b/examples/elixir/lib/elixir_example.ex new file mode 100644 index 0000000..0092174 --- /dev/null +++ b/examples/elixir/lib/elixir_example.ex @@ -0,0 +1,12 @@ +defmodule ElixirExample do + require Logger + + def main(_) do + Logger.info "Example dogstatsd calls" + :dogstatsd.event("Test", "Testing dogstatsd library", :info, :low) + :dogstatsd.gauge("connection.active", 5) + :dogstatsd.increment("requests.image", 1, %{:file_type => "png"}) + :dogstatsd.timer("connection.latency", 35, %{:path => "/hello/world.png"}) + :dogstatsd.set("user.recent_ids", 2054) + end +end diff --git a/examples/elixir/mix.exs b/examples/elixir/mix.exs new file mode 100644 index 0000000..9609a65 --- /dev/null +++ b/examples/elixir/mix.exs @@ -0,0 +1,35 @@ +defmodule Elixir.Mixfile do + use Mix.Project + + def project do + [app: :elixir_example, + version: "0.1.0", + elixir: "~> 1.3", + build_embedded: Mix.env == :prod, + start_permanent: Mix.env == :prod, + escript: [main_module: ElixirExample], + deps: deps()] + end + + # Configuration for the OTP application + # + # Type "mix help compile.app" for more information + def application do + [applications: [:logger, :dogstatsd]] + end + + # Dependencies can be Hex packages: + # + # {:mydep, "~> 0.3.0"} + # + # Or git/path repositories: + # + # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} + # + # Type "mix help deps" for more examples and options + defp deps do + [ + {:dogstatsd, path: "../../"} + ] + end +end diff --git a/examples/elixir/test/elixir_example_test.exs b/examples/elixir/test/elixir_example_test.exs new file mode 100644 index 0000000..02c0353 --- /dev/null +++ b/examples/elixir/test/elixir_example_test.exs @@ -0,0 +1,8 @@ +defmodule ElixirExampleTest do + use ExUnit.Case + doctest ElixirExample + + test "the truth" do + assert 1 + 1 == 2 + end +end diff --git a/examples/elixir/test/test_helper.exs b/examples/elixir/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/examples/elixir/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/mix.exs b/mix.exs deleted file mode 100644 index 9a06aa6..0000000 --- a/mix.exs +++ /dev/null @@ -1,73 +0,0 @@ -defmodule Dogstatsd do - [:gauge, :increment, :counter, :histogram, :timing, :timer, :set] |> (Enum.each fn name -> - def unquote(name)(metric_name, metric_value) do - apply(:dogstatsd, unquote(name), [metric_name, metric_value]) - end - def unquote(name)(metric_name, metric_value, rate_or_tags) do - apply(:dogstatsd, unquote(name), [metric_name, metric_value, rate_or_tags]) - end - def unquote(name)(metric_name, metric_value, metric_rate, metric_tags) do - apply(:dogstatsd, unquote(name), [metric_name, metric_value, metric_rate, metric_tags]) - end - end) - def start do - Application.ensure_all_started(:dogstatsd) - end - - defmodule Mixfile do - use Mix.Project - - def project do - [app: :dogstatsd, - version: version(), - name: "Dogstatsd", - elixir: "~> 1.3", - source_url: "https://github.com/WhoopInc/dogstatsde", - docs: [ - extras: ["README.md"], - main: "README.md", - ], - build_embedded: Mix.env == :prod, - start_permanent: Mix.env == :prod, - deps: deps] - end - - def application do - [ - applications: [:logger, :worker_pool, :stillir], - mod: {:dogstatsd_app, []} - ] - end - - defp deps do - [ - {:stillir, "~> 1.0.0"}, - {:worker_pool, "~> 2.1.0"}, - {:meck, "~> 0.8.4", only: :test} - ] - end - - def version do - # Fetch or fabricate a version number - {:ok, [{:application, :dogstatsd, appdata}]} = :file.consult("src/dogstatsd.app.src") - case appdata[:vsn] do - :git -> - # Fabricate a magic git version - last_vsn = case System.cmd("git", ["tag", "--sort=version:refname"]) do - {git_tags, 0} -> - git_tags |> String.trim |> String.split |> List.last - {_, 129} -> - {git_tags, 0} = System.cmd("git", ["tag"]) - git_tags |> String.trim |> String.split |> List.last - end - {git_hash,0} = System.cmd("git", ["rev-parse", "--short", "HEAD"]) - short_hash = git_hash |> String.trim - "#{last_vsn}+build-#{short_hash}" - real_vsn -> - # We get here when this is a downloaded Hex package - # In this case, the version comes in as an Erlang string and we need it to be an Elixir string - to_string(real_vsn) - end - end - end -end diff --git a/rebar.lock b/rebar.lock deleted file mode 100644 index 8859f54..0000000 --- a/rebar.lock +++ /dev/null @@ -1,2 +0,0 @@ -[{<<"stillir">>,{pkg,<<"stillir">>,<<"1.0.0">>},0}, - {<<"worker_pool">>,{pkg,<<"worker_pool">>,<<"2.1.0">>},0}]. diff --git a/scripts/install-deps.bash b/scripts/install-deps.bash index b7276af..65e3b56 100755 --- a/scripts/install-deps.bash +++ b/scripts/install-deps.bash @@ -13,7 +13,6 @@ elif [ $ELIXIR_VSN ]; then source $HOME/.kiex/elixirs/elixir-${ELIXIR_VSN}.env mix local.hex --force mix local.rebar --force - mix deps.get else echo Unknown rebar version requested: $REBAR_VSN exit 1 diff --git a/scripts/run-tests.bash b/scripts/run-tests.bash index 921d1a2..eecf4c9 100755 --- a/scripts/run-tests.bash +++ b/scripts/run-tests.bash @@ -12,8 +12,11 @@ elif [ $REBAR_VSN -eq 3 ]; then ./vendor/rebar3 eunit elif [ $ELIXIR_VSN ]; then source $HOME/.kiex/elixirs/elixir-${ELIXIR_VSN}.env + cd examples/elixir + mix deps.get mix compile - mix test + mix escript.build + ./elixir_example else echo Unknown rebar version requested: $REBAR_VSN exit 1 diff --git a/src/dogstatsd.app.src b/src/dogstatsd.app.src index 6088bf6..efc597f 100644 --- a/src/dogstatsd.app.src +++ b/src/dogstatsd.app.src @@ -17,5 +17,5 @@ {links, [{"GitHub", "https://github.com/WhoopInc/dogstatsde"}]}, {pkg_name, "dogstatsde"}, {include_files, ["mix.exs"]}, - {build_tools, [<<"rebar3">>, <<"mix">>]} + {build_tools, [<<"rebar3">>]} ]}. diff --git a/test/basic_test.exs b/test/basic_test.exs deleted file mode 100644 index 5f1cf62..0000000 --- a/test/basic_test.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule BasicTest do - use ExUnit.Case, async: false - - test "send a metric" do - assert :ok = Dogstatsd.gauge("test", 1) - - assert :meck.validate(:gen_udp) - assert :meck.called(:gen_udp, :send, :_) - end -end diff --git a/test/test_helper.exs b/test/test_helper.exs deleted file mode 100644 index fe022f2..0000000 --- a/test/test_helper.exs +++ /dev/null @@ -1,9 +0,0 @@ -ExUnit.start - -Application.ensure_all_started(:meck) - -:meck.new(:gen_udp, [:unstick]) -:meck.expect(:gen_udp, :send, fn (_Socket, _Address, _Port, _Packet) -> :ok end) -:meck.expect(:gen_udp, :open, fn (_Port) -> {:ok, :fake_socket} end) - -Dogstatsd.start