Skip to content

Commit

Permalink
Setup initial monitoring engine
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Oct 22, 2024
1 parent a65fdcf commit 2644229
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ uplink-*.tar

mnesia

.mnesia
.mnesia

.envrc
7 changes: 5 additions & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/uplink/clients/instellar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Uplink.Clients.Instellar do
Instance,
Register,
Component,
Monitor,
Variable,
Proxy,
Self
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/uplink/clients/instellar/monitor.ex
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions lib/uplink/monitors.ex
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions lib/uplink/monitors/boot.ex
Original file line number Diff line number Diff line change
@@ -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
Empty file added lib/uplink/monitors/observer.ex
Empty file.
21 changes: 21 additions & 0 deletions lib/uplink/monitors/router.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2644229

Please sign in to comment.