Current section
21 Versions
Jump to
Current section
21 Versions
Compare versions
7
files changed
+95
additions
-160
deletions
| @@ -9,7 +9,7 @@ Chuck this into your project config: | |
| 9 9 | |
| 10 10 | ```elixir |
| 11 11 | defp deps do |
| 12 | - [{:riemann, " ~> 0.0.6"}, |
| 12 | + [{:riemann, " ~> 0.0.7"}, |
| 13 13 | {:exprotobuf, github: "koudelka/exprotobuf", branch: "injection-fix"}, |
| 14 14 | {:gpb, github: "tomas-abrahamsson/gpb", tag: "3.17.2", override: true}] |
| 15 15 | end |
| @@ -4,18 +4,19 @@ | |
| 4 4 | {<<"description">>,<<"A client for the Riemann event stream processor">>}. |
| 5 5 | {<<"elixir">>,<<"~> 1.0">>}. |
| 6 6 | {<<"files">>, |
| 7 | - [<<"lib/riemann.ex">>,<<"lib/riemann/helpers/attribute.ex">>, |
| 8 | - <<"lib/riemann/helpers/event.ex">>,<<"lib/riemann/helpers/msg.ex">>, |
| 9 | - <<"lib/riemann/proto.ex">>,<<"lib/riemann/riemann.proto">>, |
| 10 | - <<"lib/riemann/worker.ex">>,<<"mix.exs">>,<<"README.md">>,<<"LICENSE">>]}. |
| 7 | + [<<"lib/riemann.ex">>,<<"lib/riemann/connection.ex">>, |
| 8 | + <<"lib/riemann/helpers/attribute.ex">>,<<"lib/riemann/helpers/event.ex">>, |
| 9 | + <<"lib/riemann/helpers/msg.ex">>,<<"lib/riemann/proto.ex">>, |
| 10 | + <<"lib/riemann/riemann.proto">>,<<"lib/riemann/worker.ex">>,<<"mix.exs">>, |
| 11 | + <<"README.md">>,<<"LICENSE">>]}. |
| 11 12 | {<<"licenses">>,[<<"MIT">>]}. |
| 12 13 | {<<"links">>, |
| 13 14 | [{<<"GitHub">>,<<"https://github.com/koudelka/elixir-riemann">>}, |
| 14 15 | {<<"Riemann">>,<<"http://riemann.io">>}]}. |
| 15 16 | {<<"name">>,<<"riemann">>}. |
| 16 17 | {<<"requirements">>, |
| 17 | - [{<<"poolboy">>, |
| 18 | - [{<<"app">>,<<"poolboy">>}, |
| 18 | + [{<<"honeydew">>, |
| 19 | + [{<<"app">>,<<"honeydew">>}, |
| 19 20 | {<<"optional">>,nil}, |
| 20 | - {<<"requirement">>,<<"~> 1.5.1">>}]}]}. |
| 21 | - {<<"version">>,<<"0.0.6">>}. |
| 21 | + {<<"requirement">>,<<"~> 0.0.2">>}]}]}. |
| 22 | + {<<"version">>,<<"0.0.7">>}. |
| @@ -1,6 +1,5 @@ | |
| 1 1 | defmodule Riemann do |
| 2 2 | use Application |
| 3 | - alias Riemann.Worker |
| 4 3 | alias Riemann.Proto.Msg |
| 5 4 | alias Riemann.Proto.Event |
| 6 5 | alias Riemann.Proto.Query |
| @@ -13,13 +12,6 @@ defmodule Riemann do | |
| 13 12 | def start(_type, _args) do |
| 14 13 | import Supervisor.Spec, warn: false |
| 15 14 | |
| 16 | - pool_options = [ |
| 17 | - name: {:local, Worker.pool_name}, |
| 18 | - worker_module: Riemann.Worker, |
| 19 | - size: 6, |
| 20 | - max_overflow: 10 |
| 21 | - ] |
| 22 | - |
| 23 15 | address = Application.get_env(:riemann, :address) |
| 24 16 | worker_options = [ |
| 25 17 | host: address[:host] || "127.0.0.1", |
| @@ -27,7 +19,7 @@ defmodule Riemann do | |
| 27 19 | ] |
| 28 20 | |
| 29 21 | children = [ |
| 30 | - :poolboy.child_spec(Worker.pool_name, pool_options, worker_options) |
| 22 | + Honeydew.child_spec(:pool, Riemann.Worker, worker_options) |
| 31 23 | ] |
| 32 24 | |
| 33 25 | opts = [strategy: :one_for_one] |
| @@ -61,8 +53,7 @@ defmodule Riemann do | |
| 61 53 | end |
| 62 54 | |
| 63 55 | @doc false |
| 64 | - # queries are often goes to include double quotes, so |
| 65 | - # we'll support using char lists (single quotes) |
| 56 | + # queries are often going to include double quotes, so we'll support using char lists (single quotes) |
| 66 57 | # ex: 'service = "my service"' |
| 67 58 | def query(query_str) when is_list(query_str) do |
| 68 59 | query_str |
| @@ -0,0 +1,73 @@ | |
| 1 | + defmodule Riemann.Connection do |
| 2 | + use GenServer |
| 3 | + require Logger |
| 4 | + alias Riemann.Proto.Msg |
| 5 | + |
| 6 | + defmodule State do |
| 7 | + defstruct tcp: nil, from: nil |
| 8 | + end |
| 9 | + |
| 10 | + @ok_msg Msg.new(ok: true) |> Msg.encode |
| 11 | + def ok_msg, do: @ok_msg |
| 12 | + |
| 13 | + @error_msg Msg.new(ok: false) |> Msg.encode |
| 14 | + def error_msg, do: @error_msg |
| 15 | + |
| 16 | + def start_link(state) do |
| 17 | + GenServer.start_link(__MODULE__, state, []) |
| 18 | + end |
| 19 | + |
| 20 | + # used for testing |
| 21 | + def start(state) do |
| 22 | + GenServer.start(__MODULE__, state, []) |
| 23 | + end |
| 24 | + |
| 25 | + def init([host: host, port: port]) do |
| 26 | + # riemann message lengths are four bytes up front |
| 27 | + {:ok, tcp} = :gen_tcp.connect(:erlang.binary_to_list(host), port, [:binary, nodelay: true, packet: 4, active: true]) |
| 28 | + {:ok, %State{tcp: tcp}} |
| 29 | + end |
| 30 | + |
| 31 | + def handle_call({:send_msg, msg}, from, state) do |
| 32 | + :ok = :gen_tcp.send(state.tcp, Msg.encode(msg)) |
| 33 | + # we'll reply once we get an ok from the server |
| 34 | + {:noreply, %{state | from: from}} |
| 35 | + end |
| 36 | + def handle_call(_msg, _from, state), do: {:reply, :unknown_msg, state} |
| 37 | + |
| 38 | + # a query that errored |
| 39 | + def handle_info({:tcp, _port, << @error_msg, rest :: binary >> = msg}, %State{from: from} = state) when is_tuple(from) |
| 40 | + and bit_size(rest) > 0 do |
| 41 | + GenServer.reply(from, {:error, Msg.decode(msg)}) |
| 42 | + {:noreply, %{state | from: nil}} |
| 43 | + end |
| 44 | + |
| 45 | + # a successful query with a list of events |
| 46 | + def handle_info({:tcp, _port, << @ok_msg, rest :: binary >> = msg}, %State{from: from} = state) when is_tuple(from) |
| 47 | + and bit_size(rest) > 0 do |
| 48 | + GenServer.reply(from, {:ok, Msg.decode(msg)}) |
| 49 | + {:noreply, %{state | from: nil}} |
| 50 | + end |
| 51 | + |
| 52 | + # a successful event send, or empty query results |
| 53 | + def handle_info({:tcp, _port, @ok_msg}, %State{from: from} = state) when is_tuple(from) do |
| 54 | + GenServer.reply(from, :ok) |
| 55 | + {:noreply, %{state | from: nil}} |
| 56 | + end |
| 57 | + |
| 58 | + # unexpected message |
| 59 | + def handle_info({:tcp, _port, msg}, state) do |
| 60 | + Logger.warn("Unexpected message from Riemann server: #{inspect msg}") |
| 61 | + {:noreply, state} |
| 62 | + end |
| 63 | + |
| 64 | + # connection dropped |
| 65 | + def handle_info({:tcp_closed, _port}, state) do |
| 66 | + {:stop, :tcp_closed, %{state | tcp: nil}} |
| 67 | + end |
| 68 | + |
| 69 | + |
| 70 | + def code_change(_old_vsn, state, _extra) do |
| 71 | + {:ok, state} |
| 72 | + end |
| 73 | + end |
| @@ -3,12 +3,11 @@ defmodule Riemann.Helpers.Msg do | |
| 3 3 | quote do |
| 4 4 | |
| 5 5 | def send(msg, :sync) do |
| 6 | - :poolboy.transaction(Riemann.Worker.pool_name, &GenServer.call(&1, {:send_msg, msg}), :infinity) |
| 6 | + Riemann.Worker.call(:pool, {:send_msg, [msg]}) |
| 7 7 | end |
| 8 8 | |
| 9 9 | def send(msg, :async) do |
| 10 | - # since this is a "fire and forget" event send, we want to wait forever for an available worker |
| 11 | - :poolboy.transaction(Riemann.Worker.pool_name, &GenServer.call(&1, {:send_msg, msg}), :infinity) |
| 10 | + Riemann.Worker.cast(:pool, {:send_msg, [msg]}) |
| 12 11 | end |
| 13 12 | |
| 14 13 | end |
Loading more files…