From 26442294b41ad1e3a4100bbc0215b061ec090b4a Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Tue, 22 Oct 2024 11:33:18 +0700 Subject: [PATCH] Setup initial monitoring engine --- .gitignore | 4 +++- config/dev.exs | 7 +++++-- lib/uplink/clients/instellar.ex | 5 +++++ lib/uplink/clients/instellar/monitor.ex | 21 ++++++++++++++++++++ lib/uplink/monitors.ex | 26 +++++++++++++++++++++++++ lib/uplink/monitors/boot.ex | 21 ++++++++++++++++++++ lib/uplink/monitors/observer.ex | 0 lib/uplink/monitors/router.ex | 21 ++++++++++++++++++++ 8 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 lib/uplink/clients/instellar/monitor.ex create mode 100644 lib/uplink/monitors.ex create mode 100644 lib/uplink/monitors/boot.ex create mode 100644 lib/uplink/monitors/observer.ex create mode 100644 lib/uplink/monitors/router.ex diff --git a/.gitignore b/.gitignore index ffa6ed84..c31ab52f 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,6 @@ uplink-*.tar mnesia -.mnesia \ No newline at end of file +.mnesia + +.envrc diff --git a/config/dev.exs b/config/dev.exs index 25c4e064..619f4cba 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,10 +1,13 @@ import Config -config :uplink, Uplink.Secret, "secretsomethingsixteen" +config :uplink, + Uplink.Secret, + System.get_env("UPLINK_SECRET", "secretsomethingsixteen") config :uplink, Uplink.Data, mode: "lite" -config :uplink, Uplink.Clients.Instellar, endpoint: "http://localhost/uplink" +config :uplink, Uplink.Clients.Instellar, + endpoint: "http://localhost:4000/uplink" config :uplink, :environment, :dev diff --git a/lib/uplink/clients/instellar.ex b/lib/uplink/clients/instellar.ex index fa3a0613..e06409e7 100644 --- a/lib/uplink/clients/instellar.ex +++ b/lib/uplink/clients/instellar.ex @@ -9,6 +9,7 @@ defmodule Uplink.Clients.Instellar do Instance, Register, Component, + Monitor, Variable, Proxy, Self @@ -35,6 +36,10 @@ defmodule Uplink.Clients.Instellar do to: Proxy, as: :list + defdelegate list_monitors, + to: Monitor, + as: :list + defdelegate deployment_metadata(install), to: Installation, as: :metadata diff --git a/lib/uplink/clients/instellar/monitor.ex b/lib/uplink/clients/instellar/monitor.ex new file mode 100644 index 00000000..9703775c --- /dev/null +++ b/lib/uplink/clients/instellar/monitor.ex @@ -0,0 +1,21 @@ +defmodule Uplink.Clients.Instellar.Monitor do + alias Uplink.Clients.Instellar + + def list do + headers = Instellar.Self.headers() + + [Instellar.endpoint(), "self", "monitors"] + |> Path.join() + |> Req.get(headers: headers, max_retries: 1) + |> case do + {:ok, %{status: 200, body: %{"data" => monitors}}} -> + {:ok, monitors} + + {:ok, %{status: _, body: body}} -> + {:error, body} + + {:error, error} -> + {:error, error} + end + end +end diff --git a/lib/uplink/monitors.ex b/lib/uplink/monitors.ex new file mode 100644 index 00000000..6758fdd3 --- /dev/null +++ b/lib/uplink/monitors.ex @@ -0,0 +1,26 @@ +defmodule Uplink.Monitors do + alias Uplink.Clients.Instellar + + def index(type) do + %{"uplink" => %{"id" => uplink_id}} = Instellar.get_self() + + "metrics-system.#{type}-uplink-#{uplink_id}-*" + end + + def push(monitor, type, params) do + headers = headers(monitor) + index = index(type) + + [index, "_doc"] + |> Path.join() + |> Repo.post(headers: headers, json: params) + end + + defp headers(%{"attributes" => %{"uid" => uid, "token" => token}}) do + Base.encode64("#{uid}:#{token}") + + [ + {"authorization", "ApiKey #{token}"} + ] + end +end diff --git a/lib/uplink/monitors/boot.ex b/lib/uplink/monitors/boot.ex new file mode 100644 index 00000000..aa75a0fb --- /dev/null +++ b/lib/uplink/monitors/boot.ex @@ -0,0 +1,21 @@ +defmodule Uplink.Monitors.Boot do + use Task + + alias Uplink.Clients.Instellar + + require Logger + + def init(args) do + Task.start_link(__MODULE__, :run, [args]) + end + + def run(args) do + case Instellar.list_monitors() do + {:ok, monitors} -> + nil + + {:error, error} -> + Logger.error("Failed to find monitors: #{inspect(error)}") + end + end +end diff --git a/lib/uplink/monitors/observer.ex b/lib/uplink/monitors/observer.ex new file mode 100644 index 00000000..e69de29b diff --git a/lib/uplink/monitors/router.ex b/lib/uplink/monitors/router.ex new file mode 100644 index 00000000..2ef9dc0c --- /dev/null +++ b/lib/uplink/monitors/router.ex @@ -0,0 +1,21 @@ +defmodule Uplink.Monitors.Router do + use Plug.Router + use Uplink.Web + + alias Uplink.Secret + + plug :match + + plug Plug.Parsers, + parsers: [:urlencoded, :json], + body_reader: {Uplink.Web.CacheBodyReader, :read_body, []}, + json_decoder: Jason + + plug Secret.VerificationPlug + + plug :dispatch + + post "/:action" do + %{"actor" => actor_params, "instance" => instance_params} = conn.body_params + end +end