defmodule Fact.RunMetric do @moduledoc false use Instream.Series alias Fact.RunMetric import(Fact.Influx, only: [write: 2]) alias TimeSupport series do database(Application.get_env(:helen, Fact.Influx) |> Keyword.get(:database)) measurement("run_metric") tag(:module) tag(:application, default: "helen") tag(:metric) tag(:env, default: Application.get_env(:helen, :build_env, "dev")) tag(:device) field(:val) end def record(opts) when is_list(opts) do record = Keyword.get(opts, :record, true) if record do def_mtime = TimeSupport.unix_now(:second) f = %RunMetric{} f = set_tag(f, opts, :application) f = set_tag(f, opts, :module) f = set_tag(f, opts, :metric) f = set_tag(f, opts, :device) f = set_field(f, opts, :val) f = %{f | timestamp: Keyword.get(opts, :mtime, def_mtime)} write(f, precision: :second, async: true) else :did_not_record end end defp set_tag(map, opts, key) when is_map(map) and is_list(opts) and is_atom(key) do set_tag(map, key, Keyword.get(opts, key)) end defp set_tag(map, _key, nil), do: map defp set_tag(map, key, value) when is_map(map) and is_atom(key) do %{map | tags: %{map.tags | key => value}} end defp set_field(map, opts, key) when is_map(map) and is_list(opts) and is_atom(key) do set_field(map, key, Keyword.get(opts, key)) end defp set_field(map, _key, nil) when is_map(map), do: map defp set_field(map, key, value) when is_map(map) and is_atom(key) do %{map | fields: %{map.fields | key => value}} end end