-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
212 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
defmodule Uplink.Monitors do | ||
alias Uplink.Clients.Instellar | ||
|
||
def index(type) do | ||
%{"uplink" => %{"id" => uplink_id}} = Instellar.get_self() | ||
defdelegate get_instances_metrics, | ||
to: __MODULE__.Instance, | ||
as: :metrics | ||
|
||
"metrics-system.#{type}-uplink-#{uplink_id}" | ||
end | ||
|
||
def push(monitor, type, params) do | ||
def push(%{"attributes" => attributes} = monitor, type, document) do | ||
headers = headers(monitor) | ||
index = index(type) | ||
endpoint = Map.fetch!(attributes, "endpoint") | ||
|
||
[index, "_doc"] | ||
[endpoint, index, "_doc"] | ||
|> Path.join() | ||
|> Repo.post(headers: headers, json: params) | ||
|> Req.post(headers: headers, json: document) | ||
end | ||
|
||
defp index(type) do | ||
%{"uplink" => %{"id" => uplink_id}} = Instellar.get_self() | ||
|
||
"metrics-system.#{type}-uplink-#{uplink_id}" | ||
end | ||
|
||
defp headers(%{"attributes" => %{"uid" => uid, "token" => token}}) do | ||
Base.encode64("#{uid}:#{token}") | ||
encoded_token = Base.encode64("#{uid}:#{token}") | ||
|
||
[ | ||
{"authorization", "ApiKey #{token}"} | ||
] | ||
[{"authorization", "ApiKey #{encoded_token}"}] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Uplink.Monitors.Instance do | ||
alias Uplink.Clients.LXD | ||
|
||
defstruct [:name, :data] | ||
|
||
def metrics do | ||
LXD.list_instances(recursion: 2) | ||
|
||
# members | ||
# |> Enum.map(fn member -> | ||
# Lexdee.show_resources(client, member.server_name) | ||
# |> case do | ||
# {:ok, %{body: metric}} -> | ||
# %__MODULE__{node: member.server_name, data: metric} | ||
|
||
# {:error, error} -> | ||
# error | ||
# end | ||
# end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defimpl Uplink.Monitors.Metric, for: Uplink.Monitors.Instance do | ||
alias Uplink.Monitors.Instance | ||
|
||
def memory(%Instance{name: node_name, data: data}) do | ||
%{ | ||
"@timestamp" => DateTime.utc_now(), | ||
"host" => %{ | ||
"name" => node_name, | ||
"containerized" => false | ||
} | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defprotocol Uplink.Monitors.Metric do | ||
@spec cpu(struct) :: {:ok, map()} | {:error, String.t()} | ||
def cpu(data) | ||
|
||
@spec memory(struct) :: {:ok, map()} | {:error, String.t()} | ||
def memory(data) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Uplink.Monitors.Observer do | ||
use GenStage | ||
|
||
def start_link do | ||
GenServer.start_link(__MODULE__, nil, name: __MODULE__) | ||
end | ||
|
||
def init(_options) do | ||
schedule_initial_job() | ||
|
||
{:ok, %{}} | ||
end | ||
|
||
def handle_info(:perform, state) do | ||
|
||
|
||
defp schedule_initial_job() do | ||
# In 5 seconds | ||
Process.send_after(self(), :perform, 5_000) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Uplink.Monitors.Pipeline do | ||
use Broadway | ||
|
||
alias Broadway.Message | ||
|
||
def start_link(_opts) do | ||
Broadway.start_link(__MODULE__, | ||
name: __MODULE__, | ||
producer: [ | ||
module: {Uplink.Monitors.Producer, [poll_interval: :timer.seconds(15)]}, | ||
cncurrency: 1 | ||
], | ||
processors: [ | ||
default: [ | ||
concurrency: 1 | ||
] | ||
] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule Uplink.Monitors.Producer do | ||
use GenStage | ||
@behaviour Broadway.Producer | ||
|
||
alias Uplink.Metrics | ||
|
||
@doc false | ||
def start_link(opts) do | ||
GenStage.start_link(__MODULE__, opts) | ||
end | ||
|
||
@impl true | ||
def init(opts) do | ||
state = %{ | ||
client: LXD.client(), | ||
demand: 0, | ||
poll_interval: Keyword.get(opts, :poll_interval, 15_000) | ||
} | ||
|
||
{:producer, state} | ||
end | ||
|
||
@impl true | ||
def handle_demand(demand, state) do | ||
{messages, state} = load_metrics(demand, state) | ||
{:noreply, messages, state} | ||
end | ||
|
||
def handle_info(:poll, state) do | ||
{messages, state} = load_metrics(0, state) | ||
Process.send_after(self(), :poll, state.poll_interval) | ||
end | ||
|
||
defp load_metrics(demand, state) when demand <= 0, do: {[], state} | ||
|
||
defp load_metrics(demand, state) do | ||
messages = | ||
Metrics.get_instances_metrics() | ||
|> transform_metrics() | ||
|
||
{messages, %{state | demand: demand - Enum.count(messages)}} | ||
end | ||
|
||
defp transform_metrics(metrics) do | ||
Enum.map(metrics, &transform_message/1) | ||
end | ||
|
||
defp transform_message(message) do | ||
%Broadway.Message{ | ||
data: message, | ||
acknowledger: Broadway.NoopAcknowledger.init() | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.