aboutsummaryrefslogtreecommitdiff
path: root/lib/ex_prometheus_io/query.ex
blob: 2bba7bd2429cce2774694aa30e22507ab952811b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
defmodule ExPrometheusIo.Query do

  def fetch_query(query_str, query_ref, owner) do
    "query=#{query_str}" <> "&time=#{:os.system_time(:seconds)}"
    |> fetch_json("query")
    |> Poison.decode
    |> send_results(query_ref, owner)
  end

  def fetch_range(query_str, start_ts, end_ts, step, query_ref, owner) do
    "query=#{query_str}"
    <> "&start=#{start_ts}"
    <> "&end=#{end_ts}"
    <> "&step=#{step}"
    |> fetch_json("query_range")
    |> Poison.decode
    |> send_results(query_ref, owner)
  end

  def fetch_series(matches, query_ref, owner) do
    matches
    |> Enum.map(fn(match) -> "match[]=#{match}" end)
    |> Enum.join("&")
    |> fetch_json("series")
    |> Poison.decode
    |> send_results(query_ref, owner)
  end

  defp fetch_json(query_str, query_type) do
    {:ok, {_, _, body}} = :httpc.request(
      "http://"
      <> prometheus_host
      <> "/api/v1/#{query_type}?"
      <> query_str
      |> String.to_char_list())
    body
  end

  defp send_results({:error, :invalid} = results, query_ref, owner) do
    send(owner, {:prometheus_results, query_ref, results})
  end

  defp send_results(
      {:ok, %{"status" => "success", "data" => results}}, query_ref, owner) do
    send(owner, {:prometheus_results, query_ref, results})
  end

  defp send_results({:ok, %{"error" => message}}, query_ref, owner) do
    send(owner, {:prometheus_results, query_ref, {:error, message}})
  end

  defp prometheus_host do
    Application.fetch_env!(:ex_prometheus_io, :hostname)
  end

end