-
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.
Setup monitoring pipeline for metrics
- Loading branch information
Showing
7 changed files
with
101 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,57 @@ | ||
defmodule Uplink.Monitors do | ||
use Task | ||
|
||
alias Uplink.Cache | ||
alias Uplink.Pipelines | ||
alias Uplink.Clients.Instellar | ||
|
||
require Logger | ||
|
||
@pipeline_modules %{ | ||
metrics: Uplink.Metrics.Pipeline | ||
} | ||
|
||
def start_link(options) do | ||
Task.start_link(__MODULE__, :run, options) | ||
Task.start_link(__MODULE__, :run, [options]) | ||
end | ||
|
||
def run(options) do | ||
def run(_options) do | ||
Instellar.list_monitors() | ||
|> case do | ||
{:ok, %{body: monitors}} -> | ||
state = maybe_start_pipeline(monitors) | ||
{:ok, monitors} -> | ||
start_pipeline(monitors, :metrics) | ||
|
||
error -> | ||
{:error, error} | ||
end | ||
end | ||
|
||
defp maybe_start_pipeline(monitors) do | ||
Cache.transaction([keys: [:monitors]], fn -> | ||
started_monitors = Cache.get(:monitors) | ||
defp start_pipeline(monitors, context) do | ||
Logger.info("[Uplink.Monitors] Starting pipeline...") | ||
|
||
started_metrics_monitor_ids = | ||
Pipelines.get_monitors(context) | ||
|> Enum.map(fn monitor -> | ||
monitor["attributes"]["id"] | ||
end) | ||
|
||
not_started_monitors = | ||
Enum.filter(monitors, fn monitor -> | ||
monitor["attributes"]["id"] not in started_metrics_monitor_ids | ||
end) | ||
|
||
grouped_monitors = | ||
Enum.group_by(not_started_monitors, fn monitor -> | ||
monitor["attributes"]["type"] | ||
end) | ||
|
||
context_monitors = Map.get(grouped_monitors, "#{context}") | ||
|
||
if Enum.count(context_monitors) > 0 do | ||
Pipelines.append_monitors(context, context_monitors) | ||
end | ||
|
||
module = Map.fetch!(@pipeline_modules, context) | ||
|
||
not_started_monitors = | ||
Enum.filter(monitors, fn monitor -> | ||
monitor["attributes"]["id"] not in started_monitors | ||
end) | ||
end) | ||
Pipelines.start(module) | ||
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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
defmodule Uplink.Pipelines do | ||
use DynamicSupervisor | ||
defdelegate get_monitors(context), | ||
to: __MODULE__.Context, | ||
as: :get | ||
|
||
def start_link(options) do | ||
DynamicSupervisor.start_link(__MODULE__, options, name: __MODULE__) | ||
end | ||
defdelegate append_monitors(context, monitors), | ||
to: __MODULE__.Context, | ||
as: :append | ||
|
||
def start_metrics(monitors) do | ||
spec = {Uplink.Metrics.Pipeline, monitors: monitors} | ||
def start(module) do | ||
spec = %{ | ||
id: module, | ||
start: {module, :start_link, []} | ||
} | ||
|
||
DynamicSupervisor.start_child(__MODULE__, spec) | ||
Pogo.DynamicSupervisor.start_child(Uplink.PipelineSupervisor, spec) | ||
end | ||
|
||
def init(_options) do | ||
DynamicSupervisor.init(strategy: :one_for_one) | ||
def list do | ||
Pogo.DynamicSupervisor.which_children(Uplink.PipelineSupervisor, :global) | ||
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,32 @@ | ||
defmodule Uplink.Pipelines.Context do | ||
use Agent | ||
|
||
require Logger | ||
|
||
def start_link(options) do | ||
monitors = Keyword.get(options, :monitors, []) | ||
name = Keyword.fetch!(options, :name) | ||
|
||
Agent.start_link(fn -> monitors end, name: {:global, name}) | ||
|> case do | ||
{:ok, pid} -> | ||
Logger.info("[Uplink.Pipelines.Context] started #{inspect(name)}") | ||
|
||
{:ok, pid} | ||
|
||
{:error, {:already_started, pid}} -> | ||
Process.link(pid) | ||
{:ok, pid} | ||
end | ||
end | ||
|
||
def get(pid_or_name) do | ||
Agent.get({:global, pid_or_name}, fn monitors -> monitors end) | ||
end | ||
|
||
def append(pid_or_name, new_monitors) do | ||
Agent.get_and_update({:global, pid_or_name}, fn monitors -> | ||
{monitors, monitors ++ new_monitors} | ||
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
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