Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all host ips as prometheus targets #3147

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/trento_web/controllers/v1/prometheus_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defmodule TrentoWeb.V1.PrometheusJSON do

def target(%{target: target}),
do: %{
targets: ["#{List.first(target.ip_addresses, target.hostname)}:#{@node_exporter_port}"],
targets:
Enum.map(target.ip_addresses ++ [target.hostname], &"#{&1}:#{@node_exporter_port}"),
labels: %{
# TODO: in the future renaeme this label which also is used by node_exporter json
agentID: "#{target.id}",
Expand Down
28 changes: 28 additions & 0 deletions test/trento_web/controllers/v1/prometheus_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ defmodule TrentoWeb.V1.PrometheusControllerTest do
} in response
end

test "should return the expected targets when host have multiple IP address", %{
conn: conn
} do
ip1 = Faker.Internet.ip_v4_address()
ip2 = Faker.Internet.ip_v4_address()
%{id: id, hostname: hostname} = insert(:host, ip_addresses: [ip1, ip2])

response =
conn
|> get("/api/v1/prometheus/targets")
|> json_response(200)

assert [
%{
"labels" => %{
"agentID" => "#{id}",
"hostname" => "#{hostname}",
"exporter_name" => "Node Exporter"
},
"targets" => [
"#{ip1}:9100",
"#{ip2}:9100",
"#{hostname}:9100"
]
}
] == response
end

test "should return the exporters status", %{conn: conn} do
expect(Trento.Infrastructure.Prometheus.Mock, :get_exporters_status, fn _ ->
{:ok, %{"Node Exporter" => :passing}}
Expand Down
Loading