Skip to content

Commit

Permalink
Allow for injection of registry; use registry mock to test gauge setu…
Browse files Browse the repository at this point in the history
…p; move things around
  • Loading branch information
ssciolla committed May 16, 2024
1 parent 9e951f5 commit 8c84e50
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 66 deletions.
62 changes: 36 additions & 26 deletions lib/metrics_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,25 @@ def self.time_processing
end

class MetricsProvider
def initialize(start_time:, end_time:, status_event_repo:, push_gateway_url:)
def initialize(
status_event_repo:,
push_gateway_url:,
start_time:,
end_time:,
registry: nil
)
@start_time = start_time
@end_time = end_time
@status_event_repo = status_event_repo
@push_gateway_url = push_gateway_url
@registry = registry
end

def registry
@registry ||= Prometheus::Client::Registry.new
end
private :registry

def get_latest_bag_events_by_time
st_time = Time.at(@start_time)
@status_event_repo.get_latest_event_for_bags(start_time: st_time)
Expand All @@ -49,15 +61,15 @@ def get_failed_bag_ids(events_by_time)
def set_success_count(events_by_time)
dark_blue_success_count = registry.gauge(
:dark_blue_success_count,
docstring: "Successful number of bag transfer"
docstring: "Number of successful bag transfers"
)
dark_blue_success_count.set(get_success_count(events_by_time))
end

def set_failed_count(events_by_time)
dark_blue_failed_count = registry.gauge(
:dark_blue_failed_count,
docstring: "Failed number of bag transfer"
docstring: "Number of failed bag transfers"
)
dark_blue_failed_count.set(get_failure_count(events_by_time))
end
Expand All @@ -75,47 +87,45 @@ def set_failed_bag_id(events_by_time)
end

def set_last_successful_run
dark_blue_last_successful_run = registry.gauge(:dark_blue_last_successful_run,
docstring: "Timestamp of the last successful run of the cron job")
return unless dark_blue_last_successful_run
dark_blue_last_successful_run = registry.gauge(
:dark_blue_last_successful_run,
docstring: "Timestamp of the last successful run of the cron job"
)
# converting starttime to milliseconds to support converting epoch time to datetime
# https://github.com/grafana/grafana/issues/6297
time_in_milli_sec = (@start_time * 1000)
time_in_milli_sec = @start_time * 1000
dark_blue_last_successful_run.set(time_in_milli_sec)
end

def set_processing_duration
dark_blue_processing_duration = registry.gauge(:dark_blue_processing_duration,
docstring: "Duration of processing in seconds for the cron job")
return unless dark_blue_processing_duration
dark_blue_processing_duration = registry.gauge(
:dark_blue_processing_duration,
docstring: "Duration of processing in seconds for the cron job"
)
dark_blue_processing_duration.set(@end_time - @start_time)
end

def set_all_metrics
set_last_successful_run
set_processing_duration
latest_events = get_latest_bag_events_by_time
set_success_count(latest_events)
set_failed_count(latest_events)
set_failed_bag_id(latest_events)
push_metrics
end

private

def registry
@registry ||= Prometheus::Client::Registry.new
end

def gateway
@gateway ||= Prometheus::Client::Push.new(
job: "DarkBlueMetric",
gateway: @push_gateway_url
)
end
private :gateway

def push_metrics
gateway.add(registry)
end
private :push_metrics

def set_all_metrics
set_last_successful_run
set_processing_duration
latest_events = get_latest_bag_events_by_time
set_success_count(latest_events)
set_failed_count(latest_events)
set_failed_bag_id(latest_events)
push_metrics
end
end
end
87 changes: 47 additions & 40 deletions test/test_metrics_collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require "minitest/mock"

require_relative "setup_db"
require_relative "../db/database_schema"
require_relative "../lib/bag_status"
require_relative "../lib/bag_repository"
require_relative "../lib/metrics_collector"
Expand All @@ -18,36 +17,45 @@ def setup
@status_event_repo = StatusEventRepository::StatusEventInMemoryRepository.new
@push_gateway_url = "http://fake.pushgateway"

@metrics = DarkBlueMetrics::MetricsProvider.new(start_time: @start_time, end_time: @end_time, status_event_repo: @status_event_repo, push_gateway_url: @push_gateway_url)
@registry = Prometheus::Client::Registry.new
@registry = Minitest::Mock.new
@gauge_mock = Minitest::Mock.new
end

def test_initialize
assert_equal @start_time, @metrics.instance_variable_get(:@start_time)
assert_equal @end_time, @metrics.instance_variable_get(:@end_time)
@metrics = DarkBlueMetrics::MetricsProvider.new(
start_time: @start_time,
end_time: @end_time,
status_event_repo: @status_event_repo,
push_gateway_url: @push_gateway_url,
registry: @registry
)
end

def test_set_last_successful_run
expected_time = (@start_time.to_i * 1000)
@registry.stub(:gauge, @gauge_mock) do
actual_time = @metrics.set_last_successful_run
@gauge_mock.verify
assert_equal(expected_time, actual_time)
end
@registry.expect(
:gauge,
@gauge_mock,
[:dark_blue_last_successful_run],
docstring: "Timestamp of the last successful run of the cron job"
)
@gauge_mock.expect(:set, nil, [expected_time])
@metrics.set_last_successful_run
@gauge_mock.verify
end

def test_set_processing_duration
expect_duration = @end_time - @start_time
@registry.stub(:gauge, @gauge_mock) do
actual_duration = @metrics.set_processing_duration
@gauge_mock.verify
assert_equal(expect_duration, actual_duration)
end
expected_duration = 5
@registry.expect(
:gauge,
@gauge_mock,
[:dark_blue_processing_duration],
docstring: "Duration of processing in seconds for the cron job"
)
@gauge_mock.expect(:set, nil, [expected_duration])
@metrics.set_processing_duration
@gauge_mock.verify
end

def test_set_success_count
@status_event_repo.status_events.clear
@bag_identifier_one = "repository.context-0001"
@bag_identifier_two = "repository.context-0002"
@deposited_at = Time.utc(2024, 3, 18)
Expand All @@ -64,17 +72,20 @@ def test_set_success_count
timestamp: @deposited_at
)

@registry.expect(
:gauge,
@gauge_mock,
[:dark_blue_success_count],
docstring: "Number of successful bag transfers"
)
expected = 1
@registry.stub(:gauge, @gauge_mock) do
events_by_time = @metrics.get_latest_bag_events_by_time
actual = @metrics.set_success_count(events_by_time)
@gauge_mock.verify
assert_equal(expected, actual)
end
events_by_time = @metrics.get_latest_bag_events_by_time
@gauge_mock.expect(:set, nil, [expected])
@metrics.set_success_count(events_by_time)
@gauge_mock.verify
end

def test_set_failed_count
@status_event_repo.status_events.clear
@bag_identifier_one = "repository.context-0001"
@bag_identifier_two = "repository.context-0002"
@deposited_at = Time.utc(2024, 3, 18)
Expand All @@ -91,16 +102,19 @@ def test_set_failed_count
timestamp: @deposited_at
)
expected = 1
@registry.stub(:gauge, @gauge_mock) do
events_by_time = @metrics.get_latest_bag_events_by_time
actual = @metrics.set_failed_count(events_by_time)
@gauge_mock.verify
assert_equal(expected, actual)
end
@registry.expect(
:gauge,
@gauge_mock,
[:dark_blue_failed_count],
docstring: "Number of failed bag transfers"
)
events_by_time = @metrics.get_latest_bag_events_by_time
@gauge_mock.expect(:set, nil, [expected])
@metrics.set_failed_count(events_by_time)
@gauge_mock.verify
end

def test_get_latest_bag_events_by_time
@status_event_repo.status_events.clear
@bag_identifier_one = "repository.context-0001"
@bag_identifier_two = "repository.context-0002"
@deposited_at = Time.utc(2024, 3, 18)
Expand All @@ -121,7 +135,6 @@ def test_get_latest_bag_events_by_time
end

def test_get_success_count
@status_event_repo.status_events.clear
@bag_identifier_one = "repository.context-0001"
@bag_identifier_two = "repository.context-0002"
@deposited_at = Time.utc(2024, 3, 18)
Expand All @@ -143,7 +156,6 @@ def test_get_success_count
end

def test_get_failure_count
@status_event_repo.status_events.clear
@bag_identifier_one = "repository.context-0001"
@bag_identifier_two = "repository.context-0002"
@deposited_at = Time.utc(2024, 3, 18)
Expand All @@ -165,7 +177,6 @@ def test_get_failure_count
end

def test_get_failed_bag_ids
@status_event_repo.status_events.clear
@bag_identifier_one = "repository.context-0001"
@bag_identifier_two = "repository.context-0002"
@deposited_at = Time.utc(2024, 3, 18)
Expand All @@ -187,27 +198,23 @@ def test_get_failed_bag_ids
end

def test_get_latest_bag_events_by_time_empty_array
@status_event_repo.status_events.clear
actual_result = @metrics.get_latest_bag_events_by_time
assert_equal [], actual_result
end

def test_get_success_count_nil
@status_event_repo.status_events.clear
events_by_time = @metrics.get_latest_bag_events_by_time
actual_result = @metrics.get_success_count(events_by_time)
assert_equal 0, actual_result
end

def test_get_failure_count_nil
@status_event_repo.status_events.clear
events_by_time = @metrics.get_latest_bag_events_by_time
actual_result = @metrics.get_failure_count(events_by_time)
assert_equal 0, actual_result
end

def test_get_failed_bag_ids_nil
@status_event_repo.status_events.clear
events_by_time = @metrics.get_latest_bag_events_by_time
actual_result = @metrics.get_failed_bag_ids(events_by_time)
assert_equal [], actual_result
Expand Down

0 comments on commit 8c84e50

Please sign in to comment.