Simplify code
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
defmodule KafkaexLagExporter.ConsumerOffsetFetcher do
|
defmodule KafkaexLagExporter.ConsumerOffsetFetcher do
|
||||||
@moduledoc "Calculate summarized lag for each consumer group"
|
@moduledoc "Calculate summarized lag for each consumer group"
|
||||||
|
|
||||||
require Logger
|
|
||||||
|
|
||||||
alias KafkaexLagExporter.KafkaUtils
|
alias KafkaexLagExporter.KafkaUtils
|
||||||
|
|
||||||
# TODO fix type
|
# TODO fix type
|
||||||
@@ -14,30 +12,25 @@ defmodule KafkaexLagExporter.ConsumerOffsetFetcher do
|
|||||||
consumer_group_names = KafkaUtils.get_consumer_group_names(endpoint)
|
consumer_group_names = KafkaUtils.get_consumer_group_names(endpoint)
|
||||||
|
|
||||||
consumer_lags =
|
consumer_lags =
|
||||||
KafkaUtils.topic_names_for_consumer_groups(
|
KafkaUtils.topic_names_for_consumer_groups(endpoint, [], consumer_group_names)
|
||||||
endpoint,
|
|> Enum.flat_map(&get_lag_per_topic(&1))
|
||||||
[],
|
|
||||||
consumer_group_names
|
|
||||||
)
|
|
||||||
|> Enum.map(fn {consumer_group, topics} ->
|
|
||||||
{consumer_group, get_lag_for_consumer(consumer_group, topics)}
|
|
||||||
end)
|
|
||||||
|
|
||||||
consumer_lag_sum = get_lag_for_consumer_sum(consumer_lags)
|
consumer_lag_sum = get_lag_for_consumer_sum(consumer_lags)
|
||||||
|
|
||||||
%{lags: consumer_lags, sum: consumer_lag_sum}
|
%{lags: consumer_lags, sum: consumer_lag_sum}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_lag_for_consumer(consumer_group, topics) do
|
defp get_lag_per_topic({consumer_group, topics}) do
|
||||||
topics
|
Enum.map(topics, fn topic ->
|
||||||
|> Enum.flat_map(fn topic ->
|
lag = KafkaUtils.lag(topic, consumer_group, :client1)
|
||||||
KafkaUtils.lag(topic, consumer_group, :client1)
|
{consumer_group, topic, lag}
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_lag_for_consumer_sum(lags_per_consumer_group) do
|
defp get_lag_for_consumer_sum(lags_per_consumer_group) do
|
||||||
lags_per_consumer_group
|
Enum.map(lags_per_consumer_group, fn {consumer_group, topic, lag_per_partition} ->
|
||||||
|> Enum.map(fn {topic, lag_per_partition} -> {topic, sum_topic_lag(lag_per_partition, 0)} end)
|
{consumer_group, topic, sum_topic_lag(lag_per_partition, 0)}
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp sum_topic_lag([], acc), do: acc
|
defp sum_topic_lag([], acc), do: acc
|
||||||
|
|||||||
@@ -4,7 +4,11 @@ defmodule KafkaexLagExporter.ConsumerOffsetFetcher.Test do
|
|||||||
|
|
||||||
@test_consumer_group_name1 "test_consumer_1"
|
@test_consumer_group_name1 "test_consumer_1"
|
||||||
@test_consumer_group_name2 "test_consumer_2"
|
@test_consumer_group_name2 "test_consumer_2"
|
||||||
@test_lags [{0, 23}, {1, 42}, {2, 666}]
|
@test_lags1 [{0, 23}, {1, 42}, {2, 666}]
|
||||||
|
@test_lags2 [{0, 1}, {1, 2}, {2, 3}]
|
||||||
|
@test_topic1 "test_topic_1"
|
||||||
|
@test_topic2 "test_topic_2"
|
||||||
|
@test_topic3 "test_topic_3"
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
patch(
|
patch(
|
||||||
@@ -13,15 +17,15 @@ defmodule KafkaexLagExporter.ConsumerOffsetFetcher.Test do
|
|||||||
fn _ -> [@test_consumer_group_name1, @test_consumer_group_name2] end
|
fn _ -> [@test_consumer_group_name1, @test_consumer_group_name2] end
|
||||||
)
|
)
|
||||||
|
|
||||||
patch(KafkaexLagExporter.KafkaUtils, :lag, fn _, _, _ -> @test_lags end)
|
patch(KafkaexLagExporter.KafkaUtils, :lag, &lag(&1, &2, &3))
|
||||||
|
|
||||||
patch(
|
patch(
|
||||||
KafkaexLagExporter.KafkaUtils,
|
KafkaexLagExporter.KafkaUtils,
|
||||||
:topic_names_for_consumer_groups,
|
:topic_names_for_consumer_groups,
|
||||||
fn _, _, _ ->
|
fn _, _, _ ->
|
||||||
[
|
[
|
||||||
{@test_consumer_group_name1, ["test_topic_1", "test_topic_2"]},
|
{@test_consumer_group_name1, [@test_topic1, @test_topic2]},
|
||||||
{@test_consumer_group_name2, ["test_topic_3"]}
|
{@test_consumer_group_name2, [@test_topic3]}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
@@ -35,13 +39,18 @@ defmodule KafkaexLagExporter.ConsumerOffsetFetcher.Test do
|
|||||||
%{sum: sum, lags: lags} = KafkaexLagExporter.ConsumerOffsetFetcher.get(test_endpoint)
|
%{sum: sum, lags: lags} = KafkaexLagExporter.ConsumerOffsetFetcher.get(test_endpoint)
|
||||||
|
|
||||||
assert sum == [
|
assert sum == [
|
||||||
{@test_consumer_group_name1, 1462},
|
{@test_consumer_group_name1, @test_topic1, 731},
|
||||||
{@test_consumer_group_name2, 731}
|
{@test_consumer_group_name1, @test_topic2, 6},
|
||||||
|
{@test_consumer_group_name2, @test_topic3, 6}
|
||||||
]
|
]
|
||||||
|
|
||||||
assert lags == [
|
assert lags == [
|
||||||
{@test_consumer_group_name1, @test_lags ++ @test_lags},
|
{@test_consumer_group_name1, @test_topic1, @test_lags1},
|
||||||
{@test_consumer_group_name2, @test_lags}
|
{@test_consumer_group_name1, @test_topic2, @test_lags2},
|
||||||
|
{@test_consumer_group_name2, @test_topic3, @test_lags2}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp lag(@test_topic1, _, _), do: @test_lags1
|
||||||
|
defp lag(_, _, _), do: @test_lags2
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user