Skip to content

Commit

Permalink
test: add tests for BlitzPrometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaAK committed Apr 20, 2022
1 parent 8cae2f3 commit b691f8e
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 9 deletions.
8 changes: 7 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule PrometheusTelemetry.MixProject do
elixir: "~> 1.12",
description: "Prometheus metrics exporter using Telemetry.Metrics as a foundation",
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
deps: deps(),
docs: docs(),
package: package()
Expand All @@ -33,7 +34,9 @@ defmodule PrometheusTelemetry.MixProject do
{:plug, "~> 1.8"},
{:plug_cowboy, "~> 2.5"},

{:ex_doc, ">= 0.0.0", only: :dev}
{:ex_doc, ">= 0.0.0", only: :dev},
{:faker, "~> 0.17", only: [:test, :dev]},
{:finch, "~> 0.11", only: :test}
]
end

Expand Down Expand Up @@ -66,4 +69,7 @@ defmodule PrometheusTelemetry.MixProject do
]
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
end
6 changes: 6 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
%{
"absinthe": {:hex, :absinthe, "1.7.0", "36819e7b1fd5046c9c734f27fe7e564aed3bda59f0354c37cd2df88fd32dd014", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0 or ~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "566a5b5519afc9b29c4d367f0c6768162de3ec03e9bf9916f9dc2bcbe7c09643"},
"castore": {:hex, :castore, "0.1.16", "2675f717adc700475345c5512c381ef9273eb5df26bdd3f8c13e2636cf4cc175", [:mix], [], "hexpm", "28ed2c43d83b5c25d35c51bc0abf229ac51359c170cba76171a462ced2e4b651"},
"cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"earmark_parser": {:hex, :earmark_parser, "1.4.23", "1d5f22a2802160fd454404fbf5e8f5d14cd8eb727c63701397b72d8c35267e69", [:mix], [], "hexpm", "2ec13bf14b2f4bbb4a15480970e295eede8bb01087fad6ceca27b724ab8e9d18"},
"ex_doc": {:hex, :ex_doc, "0.28.2", "e031c7d1a9fc40959da7bf89e2dc269ddc5de631f9bd0e326cbddf7d8085a9da", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "51ee866993ffbd0e41c084a7677c570d0fc50cb85c6b5e76f8d936d9587fa719"},
"faker": {:hex, :faker, "0.17.0", "671019d0652f63aefd8723b72167ecdb284baf7d47ad3a82a15e9b8a6df5d1fa", [:mix], [], "hexpm", "a7d4ad84a93fd25c5f5303510753789fc2433ff241bf3b4144d3f6f291658a6a"},
"finch": {:hex, :finch, "0.11.0", "622d31c224c801444c6003544fa1964a29551895b45c3543f6e4d8b0dab80f71", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "047550448148d828ddea1fb76fa3f0d2d0742eca6919e548fdd8357630cae449"},
"hpax": {:hex, :hpax, "0.1.1", "2396c313683ada39e98c20a75a82911592b47e5c24391363343bde74f82396ca", [:mix], [], "hexpm", "0ae7d5a0b04a8a60caf7a39fcf3ec476f35cc2cc16c05abea730d3ce6ac6c826"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"mint": {:hex, :mint, "1.4.1", "49b3b6ea35a9a38836d2ad745251b01ca9ec062f7cb66f546bf22e6699137126", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "cd261766e61011a9079cccf8fa9d826e7a397c24fbedf0e11b49312bea629b58"},
"nimble_options": {:hex, :nimble_options, "0.4.0", "c89babbab52221a24b8d1ff9e7d838be70f0d871be823165c94dd3418eea728f", [:mix], [], "hexpm", "e6701c1af326a11eea9634a3b1c62b475339ace9456c1a23ec3bc9a847bca02d"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"},
"plug": {:hex, :plug, "1.13.4", "addb6e125347226e3b11489e23d22a60f7ab74786befb86c14f94fb5f23ca9a4", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "06114c1f2a334212fe3ae567dbb3b1d29fd492c1a09783d52f3d489c1a6f4cf2"},
"plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"},
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
Expand Down
19 changes: 19 additions & 0 deletions test/prometheus_telemetry/metrics/ecto_test.exs
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
22 changes: 22 additions & 0 deletions test/prometheus_telemetry/router_test.exs
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
8 changes: 0 additions & 8 deletions test/prometheus_telemetry_elixir_test.exs

This file was deleted.

106 changes: 106 additions & 0 deletions test/prometheus_telemetry_test.exs
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
22 changes: 22 additions & 0 deletions test/support/mock_supervisor.ex
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

2 changes: 2 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
PrometheusTelemetry.Support.MockSupervisor.setup()

ExUnit.start()

0 comments on commit b691f8e

Please sign in to comment.