-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Samuel <samuel@uniris.io>
- Loading branch information
1 parent
da1a29c
commit 75800a0
Showing
17 changed files
with
654 additions
and
578 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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
defmodule ArchEthic.Metrics.Aggregator do | ||
@moduledoc """ | ||
Provide aggregation methods for the metrics collected | ||
""" | ||
|
||
@doc """ | ||
Add the TPS based on the transaction validation durations | ||
## Examples | ||
iex> [ | ||
...> %{ "archethic_mining_full_transaction_validation_duration" => %{count: 10, sum: 1.5}}, | ||
...> %{ "archethic_mining_full_transaction_validation_duration" => %{count: 10, sum: 1.5 }}, | ||
...> %{ "archethic_mining_full_transaction_validation_duration" => %{count: 10, sum: 1.5 }}, | ||
...> ] |> Aggregator.inject_tps() | ||
[ | ||
%{ "tps" => 6.666666666666667 }, | ||
%{ "archethic_mining_full_transaction_validation_duration" => %{count: 10, sum: 1.5}}, | ||
%{ "archethic_mining_full_transaction_validation_duration" => %{count: 10, sum: 1.5}}, | ||
%{ "archethic_mining_full_transaction_validation_duration" => %{count: 10, sum: 1.5}} | ||
] | ||
""" | ||
@spec inject_tps(list(map())) :: list(map()) | ||
def inject_tps(metrics) do | ||
%{count: count, sum: sum} = | ||
Enum.reduce(metrics, %{count: 0, sum: 0.0}, fn | ||
%{ | ||
"archethic_mining_full_transaction_validation_duration" => %{count: count, sum: sum} | ||
}, | ||
acc -> | ||
acc | ||
|> Map.update!(:count, &(&1 + count)) | ||
|> Map.update!(:sum, &(&1 + sum)) | ||
|
||
_, acc -> | ||
acc | ||
end) | ||
|
||
if count > 0.0 do | ||
tps = count / sum | ||
[%{"tps" => tps} | metrics] | ||
else | ||
metrics | ||
end | ||
end | ||
|
||
@doc """ | ||
Reduce through aggregation metrics values | ||
## Examples | ||
iex> [ | ||
...> %{"archethic_p2p_send_message_duration" => %{count: 100, sum: 10}}, | ||
...> %{"archethic_p2p_send_message_duration" => %{count: 200, sum: 20}}, | ||
...> %{"archethic_p2p_send_message_duration" => %{count: 300, sum: 30}} | ||
...> ]|> Aggregator.reduce_values() | ||
%{ | ||
"archethic_p2p_send_message_duration" => %{count: 600, sum: 60} | ||
} | ||
""" | ||
@spec reduce_values(list(map())) :: map() | ||
def reduce_values(list_of_metrics) do | ||
list_of_metrics | ||
|> Enum.map(&Map.to_list/1) | ||
|> Enum.reduce(%{}, fn | ||
[{"tps", tps}], acc -> | ||
Map.put(acc, "tps", tps) | ||
|
||
[{metric_name, %{count: count, sum: sum}}], acc -> | ||
update_histogram_acc(metric_name, count, sum, acc) | ||
|
||
[{metric_name, value}], acc -> | ||
update_guage_acc(metric_name, value, acc) | ||
end) | ||
end | ||
|
||
defp update_histogram_acc(metric_name, count, sum, acc) do | ||
acc | ||
|> update_in([Access.key(metric_name, %{}), Access.key(:sum, 0)], &(&1 + sum)) | ||
|> update_in([Access.key(metric_name, %{}), Access.key(:count, 0)], &(&1 + count)) | ||
end | ||
|
||
defp update_guage_acc(metric_name, value, acc) do | ||
update_in(acc, [Access.key(metric_name, 0)], &(&1 + value)) | ||
end | ||
|
||
@doc """ | ||
Aggregate and summarize the metrics | ||
For histograms, it produces an average of the values by doing: `sum/count` | ||
## Examples | ||
iex> %{ | ||
...> "archethic_mining_full_transaction_validation_duration" => %{count: 5, sum: 10}, | ||
...> "archethic_p2p_send_message_duration" => %{count: 600, sum: 60}, | ||
...> "tps" => 10.0, | ||
...> "vm_memory_atom" => 600.0 | ||
...> }|> Aggregator.summarize() | ||
[ | ||
%{"archethic_mining_full_transaction_validation_duration" => 2.0}, | ||
%{"archethic_p2p_send_message_duration" => 0.1}, | ||
%{"tps" => 10.0}, | ||
%{"vm_memory_atom" => 600.0} | ||
] | ||
""" | ||
@spec summarize(%{ | ||
String.t() => number(), | ||
String.t() => %{count: number(), sum: number()} | ||
}) :: [%{String.t() => number()}, ...] | ||
def summarize(map_of_metrics) do | ||
Enum.map(map_of_metrics, fn {metric_name, metric_value} -> | ||
case metric_value do | ||
%{count: 0, sum: _sum} -> %{metric_name => 0.0} | ||
%{count: count, sum: sum} -> %{metric_name => sum / count} | ||
value -> %{metric_name => value} | ||
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,62 @@ | ||
defmodule ArchEthic.Metrics.Collector do | ||
@moduledoc """ | ||
Handle the flow of metrics collection | ||
""" | ||
|
||
alias ArchEthic.Metrics.Aggregator | ||
alias ArchEthic.Metrics.Parser | ||
alias ArchEthic.P2P | ||
|
||
@callback fetch_metrics(:inet.ip_address()) :: {:ok, String.t()} | {:error, any()} | ||
|
||
@doc """ | ||
Get the list of Node IP addresses | ||
""" | ||
@spec retrieve_node_ip_addresses() :: list(:inet.ip_address()) | ||
def retrieve_node_ip_addresses do | ||
Enum.map(P2P.authorized_nodes(), & &1.ip) | ||
end | ||
|
||
@doc """ | ||
Responsible for retrieving network metrics. | ||
""" | ||
@spec retrieve_network_metrics(list(:inet.ip_address())) :: map() | ||
def retrieve_network_metrics(node_ip_addresses) do | ||
Task.async_stream(node_ip_addresses, &service().fetch_metrics(&1)) | ||
|> Stream.filter(&match?({:ok, {:ok, _}}, &1)) | ||
|> Stream.map(fn {:ok, {:ok, result}} -> result end) | ||
|> Stream.map(&Parser.extract_from_string/1) | ||
|> Stream.map(&filter_metrics/1) | ||
|> Stream.map(&Parser.reduce_metrics/1) | ||
|> Enum.flat_map(& &1) | ||
|> Aggregator.inject_tps() | ||
|> Aggregator.reduce_values() | ||
|> Aggregator.summarize() | ||
|> reduce_to_single_map() | ||
end | ||
|
||
defp service do | ||
Application.get_env( | ||
:archethic, | ||
__MODULE__, | ||
__MODULE__.MetricsEndpoint | ||
) | ||
end | ||
|
||
defp filter_metrics(metrics) do | ||
Enum.filter(metrics, fn %{name: name} -> accept_metric?(name) end) | ||
end | ||
|
||
defp accept_metric?("archethic_mining_proof_of_work_duration"), do: true | ||
defp accept_metric?("archethic_mining_full_transaction_validation_duration"), do: true | ||
defp accept_metric?("archethic_p2p_send_message_duration"), do: true | ||
defp accept_metric?(_), do: false | ||
|
||
defp reduce_to_single_map(data_list_of_maps) do | ||
Enum.reduce(data_list_of_maps, fn a, b -> | ||
Map.merge(a, b, fn _key, a1, a2 -> | ||
a1 + a2 | ||
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,53 @@ | ||
defmodule ArchEthic.Metrics.Collector.MetricsEndpoint do | ||
@moduledoc """ | ||
This module provides a REST endpoint for metrics. | ||
""" | ||
|
||
alias ArchEthic.Metrics.Collector | ||
|
||
@behaviour Collector | ||
|
||
@node_metric_endpoint_uri "/metrics" | ||
@node_metric_request_type "GET" | ||
|
||
@impl Collector | ||
def fetch_metrics(ip_address) do | ||
with {:ok, conn_ref} <- establish_connection(ip_address), | ||
{:ok, conn, _req_ref} <- request(conn_ref) do | ||
stream_responses(conn) | ||
end | ||
end | ||
|
||
defp establish_connection(ip) do | ||
port = | ||
Application.get_env(:archethic, ArchEthicWeb.Endpoint) | ||
|> Keyword.get(:http) | ||
|> Keyword.get(:port) | ||
|
||
Mint.HTTP.connect(:http, ip |> :inet.ntoa() |> to_string(), port) | ||
end | ||
|
||
defp request(conn_ref) do | ||
Mint.HTTP.request( | ||
conn_ref, | ||
@node_metric_request_type, | ||
@node_metric_endpoint_uri, | ||
[], | ||
[] | ||
) | ||
end | ||
|
||
defp stream_responses(conn) do | ||
receive do | ||
message -> | ||
with {:ok, conn, [{:status, _, 200}, {:headers, _, _}, {:data, _, data}, {:done, _}]} <- | ||
Mint.HTTP.stream(conn, message), | ||
{:ok, _} <- Mint.HTTP.close(conn) do | ||
{:ok, data} | ||
end | ||
after | ||
5_000 -> | ||
{:error, :timeout} | ||
end | ||
end | ||
end |
Oops, something went wrong.