-
Notifications
You must be signed in to change notification settings - Fork 5
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
8 changed files
with
184 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule PrometheusTelemetry.Metrics.EctoTest do | ||
use ExUnit.Case, async: true | ||
alias PrometheusTelemetry.Metrics.Ecto | ||
|
||
describe "metrics/1" do | ||
test "able to create a list of metrics" do | ||
repos = [Repo, Repo.CMS] | ||
assert length(Ecto.metrics(repos)) === length(repos) * 4 | ||
end | ||
|
||
test "able to reduce replica repo into one" do | ||
repos = [Repo, Repo.Replica1, Repo.Replica2, Repo.Replica3] | ||
# Should have 8 metrics | ||
# 4 for regular repo | ||
# 4 for replicas | ||
assert length(Ecto.metrics(repos)) === 16 | ||
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,22 @@ | ||
defmodule PrometheusTelemetry.RouterTest do | ||
use ExUnit.Case, async: true | ||
|
||
@test_port 4050 | ||
@finch_name RouterTest.Finch | ||
|
||
setup do | ||
assert {:ok, _} = Finch.start_link(name: @finch_name) | ||
:ok | ||
end | ||
|
||
describe "&fetch_metrics/0" do | ||
test "fetches and renders metric data" do | ||
assert {:ok, %Finch.Response{body: body}} = | ||
:get | ||
|> Finch.build("http://localhost:#{@test_port}") | ||
|> Finch.request(@finch_name) | ||
|
||
assert String.length(body) > 1 | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,106 @@ | ||
defmodule PrometheusTelemetryTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Telemetry.Metrics, only: [counter: 2] | ||
|
||
@event_name_a [:some_event, :name] | ||
@event_name_b [:some_event, :name, :secondary] | ||
|
||
setup_all do | ||
key = generate_key() | ||
name = String.to_atom(key) | ||
|
||
{:ok, pid} = | ||
PrometheusTelemetry.start_link( | ||
name: name, | ||
metrics: metrics(key) | ||
) | ||
|
||
%{pid: pid, key: key, name: name} | ||
end | ||
|
||
describe "&start_link/1" do | ||
test "can start multiple metrics under a supervisor", %{key: key, pid: pid, name: name} do | ||
assert {:ok, ^pid} = | ||
PrometheusTelemetry.start_link( | ||
name: name, | ||
metrics: new_metrics(key) | ||
) | ||
end | ||
|
||
test "raise error if metrics and periodic_measurements is nil" do | ||
assert_raise NimbleOptions.ValidationError, fn -> | ||
PrometheusTelemetry.start_link( | ||
periodic_measurements: nil, | ||
metrics: nil | ||
) | ||
end | ||
end | ||
|
||
test "can have multiple supervisors under seperate names to group metrics", %{pid: pid} do | ||
key = generate_key() | ||
|
||
assert {:ok, new_pid} = | ||
PrometheusTelemetry.start_link( | ||
name: String.to_atom(key), | ||
metrics: metrics(key) | ||
) | ||
|
||
assert new_pid !== pid | ||
end | ||
end | ||
|
||
describe "&list/0" do | ||
test "lists all supervisors", %{key: key} do | ||
new_key = generate_key() | ||
|
||
assert {:ok, _new_pid} = | ||
PrometheusTelemetry.start_link( | ||
name: String.to_atom(new_key), | ||
metrics: metrics(new_key) | ||
) | ||
|
||
supervisor_length = | ||
PrometheusTelemetry.list() | ||
|> Enum.filter(fn supervisor_name -> | ||
string_name = Atom.to_string(supervisor_name) | ||
|
||
string_name =~ key or string_name =~ new_key | ||
end) | ||
|> length() | ||
|
||
assert supervisor_length === 2 | ||
end | ||
end | ||
|
||
describe "&list_prometheus_cores/1" do | ||
test "lists all the metric cores from a supervisor", %{pid: pid} do | ||
assert [prometheus_core | _] = PrometheusTelemetry.list_prometheus_cores(pid) | ||
|
||
assert prometheus_core |> TelemetryMetricsPrometheus.Core.scrape() |> is_binary() | ||
end | ||
end | ||
|
||
defp metrics(key) do | ||
[ | ||
counter("#{key}.some_thing.test", | ||
event_name: @event_name_a, | ||
measurement: :count, | ||
description: "HELLO" | ||
) | ||
] | ||
end | ||
|
||
defp new_metrics(key) do | ||
[ | ||
counter("#{key}.some_thing.test.version_2", | ||
event_name: @event_name_b, | ||
measurement: :count, | ||
description: "HELLO22", | ||
tag: [:second_item] | ||
) | ||
] | ||
end | ||
|
||
defp generate_key, do: Faker.Pokemon.name() |> String.replace(~r/[^\d\w]/, "") | ||
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,22 @@ | ||
defmodule PrometheusTelemetry.Support.MockSupervisor do | ||
@moduledoc "gen server use for mock metric" | ||
import Telemetry.Metrics, only: [counter: 2] | ||
@event_name_b "magic_snort_snort" | ||
|
||
@spec setup :: none | ||
def setup do | ||
PrometheusTelemetry.start_link( | ||
name: :test_exporter, | ||
exporter: [enabled?: true], | ||
metrics: [ | ||
counter("some_thing.test.magic", | ||
event_name: @event_name_b, | ||
measurement: :count, | ||
description: "HELLO", | ||
tag: [:bacon] | ||
) | ||
] | ||
) | ||
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 +1,3 @@ | ||
PrometheusTelemetry.Support.MockSupervisor.setup() | ||
|
||
ExUnit.start() |