-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement probe for ruby-kafka * fix rubocop warnings * code review fixes * support multi-threaded consumers * ruby-kafka: add verbose option
- Loading branch information
Showing
8 changed files
with
288 additions
and
8 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
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
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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module HttpHealthCheck | ||
module Probes | ||
class RubyKafka | ||
Heartbeat = Struct.new(:time, :group, :topic_partitions) | ||
include ::HttpHealthCheck::Probe | ||
|
||
def initialize(opts = {}) | ||
@heartbeat_event_name = opts.fetch(:heartbeat_event_name, /heartbeat.consumer.kafka/) | ||
@heartbeat_interval_sec = opts.fetch(:heartbeat_interval_sec, 10) | ||
@verbose = opts.fetch(:verbose, false) | ||
@consumer_groups = opts.fetch(:consumer_groups) | ||
.each_with_object(Hash.new(0)) { |group, hash| hash[group] += 1 } | ||
@heartbeats = {} | ||
@timer = opts.fetch(:timer, Time) | ||
|
||
setup_subscriptions | ||
end | ||
|
||
def probe(_env) | ||
now = @timer.now | ||
failed_heartbeats = select_failed_heartbeats(now) | ||
return probe_ok groups: meta_from_heartbeats(@heartbeats, now) if failed_heartbeats.empty? | ||
|
||
probe_error failed_groups: meta_from_heartbeats(failed_heartbeats, now) | ||
end | ||
|
||
private | ||
|
||
def select_failed_heartbeats(now) | ||
@consumer_groups.each_with_object({}) do |(group, concurrency), hash| | ||
heartbeats = @heartbeats[group] || {} | ||
ok_heartbeats_count = heartbeats.count { |_id, hb| hb.time + @heartbeat_interval_sec >= now } | ||
hash[group] = heartbeats if ok_heartbeats_count < concurrency | ||
end | ||
end | ||
|
||
def meta_from_heartbeats(heartbeats_hash, now) # rubocop: disable Metrics/MethodLength, Metrics/AbcSize | ||
heartbeats_hash.each_with_object({}) do |(group, heartbeats), hash| | ||
concurrency = @consumer_groups[group] | ||
if heartbeats.empty? | ||
hash[group] = { had_heartbeat: false, concurrency: concurrency } | ||
next | ||
end | ||
|
||
hash[group] = { had_heartbeat: true, concurrency: concurrency, threads: {} } | ||
heartbeats.each do |thread_id, heartbeat| | ||
thread_meta = { seconds_since_last_heartbeat: now - heartbeat.time } | ||
thread_meta[:topic_partitions] = heartbeat.topic_partitions if @verbose | ||
hash[group][:threads][thread_id] = thread_meta | ||
end | ||
end | ||
end | ||
|
||
def setup_subscriptions | ||
ActiveSupport::Notifications.subscribe(@heartbeat_event_name) do |*args| | ||
event = ActiveSupport::Notifications::Event.new(*args) | ||
group = event.payload[:group_id] | ||
|
||
@heartbeats[group] ||= {} | ||
@heartbeats[group][event.transaction_id] = Heartbeat.new(event.time, group, event.payload[:topic_partitions]) | ||
end | ||
end | ||
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,121 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/notifications' | ||
require_relative '../../../lib/http_health_check/probes/ruby_kafka' | ||
|
||
describe HttpHealthCheck::Probes::RubyKafka do | ||
let(:timer) { double(Time) } | ||
let(:event_name) { 'fake.heartbeat.consumer.kafka' } | ||
let(:consumer_groups) { nil } | ||
let(:verbose) { true } | ||
let!(:probe) do | ||
described_class.new( | ||
consumer_groups: consumer_groups, | ||
timer: timer, | ||
heartbeat_event_name: event_name, | ||
verbose: verbose | ||
) | ||
end | ||
|
||
def emit_hb_event(group, topic_partitions: nil) | ||
topic_partitions ||= { 'some_topic' => %w[1 2] } | ||
ActiveSupport::Notifications.instrument(event_name, group_id: group, topic_partitions: topic_partitions) {} | ||
end | ||
|
||
context 'with list of consumer groups' do | ||
let(:group) { 'important-consumer' } | ||
let(:consumer_groups) { [group] } | ||
|
||
context 'when heartbeat is expired' do | ||
it 'returns an error' do | ||
topic_partitions = { 'foo' => %w[1 2], 'bar' => %w[3 4] } | ||
emit_hb_event(group, topic_partitions: topic_partitions) | ||
|
||
expect(timer).to receive(:now).and_return(Time.now + 15) | ||
result = probe.call(nil) | ||
expect(result).not_to be_ok | ||
|
||
meta = result.meta[:failed_groups][group] | ||
expect(meta[:had_heartbeat]).to eq(true) | ||
expect(meta[:threads].size).to eq(1) | ||
|
||
thread_meta = meta[:threads].values.first | ||
expect(thread_meta[:seconds_since_last_heartbeat]).to be_within(1).of(15) | ||
expect(thread_meta[:topic_partitions]).to eq(topic_partitions) | ||
end | ||
end | ||
|
||
context 'with multiple threads' do | ||
let(:consumer_groups) { [group, group] } | ||
|
||
context 'when number of heartbeats is equal to group concurrency' do | ||
it 'returns ok' do | ||
emit_hb_event(group) | ||
Thread.new { emit_hb_event(group) } | ||
sleep(0.01) | ||
|
||
expect(timer).to receive(:now).and_return(Time.now) | ||
result = probe.call(nil) | ||
expect(result).to be_ok | ||
end | ||
end | ||
|
||
context 'when some heartbeats are missing' do | ||
it 'returns an error' do | ||
Thread.new { emit_hb_event(group) } | ||
sleep(0.01) | ||
|
||
expect(timer).to receive(:now).and_return(Time.now) | ||
result = probe.call(nil) | ||
expect(result).not_to be_ok | ||
|
||
meta = result.meta[:failed_groups][group] | ||
expect(meta[:had_heartbeat]).to eq(true) | ||
expect(meta[:threads].size).to eq(1) | ||
end | ||
end | ||
end | ||
|
||
context 'when heartbeat had not been emitted yet' do | ||
it 'return an error' do | ||
expect(timer).to receive(:now).and_return(Time.now) | ||
|
||
result = probe.call(nil) | ||
expect(result).not_to be_ok | ||
|
||
meta = result.meta[:failed_groups][group] | ||
expect(meta[:had_heartbeat]).to eq(false) | ||
end | ||
end | ||
|
||
context 'when it noted specified group heartbeat recently' do | ||
it 'returns ok' do | ||
emit_hb_event(group) | ||
|
||
expect(timer).to receive(:now).and_return(Time.now + 5) | ||
result = probe.call(nil) | ||
expect(result).to be_ok | ||
|
||
meta = result.meta[:groups][group] | ||
thread_meta = meta[:threads].values.first | ||
expect(thread_meta[:seconds_since_last_heartbeat]).to be_within(1).of(5) | ||
end | ||
end | ||
|
||
context 'when verbose=false' do | ||
let(:verbose) { false } | ||
|
||
it 'does not include topic_partitions into result meta' do | ||
emit_hb_event(group) | ||
|
||
expect(timer).to receive(:now).and_return(Time.now + 5) | ||
result = probe.call(nil) | ||
expect(result).to be_ok | ||
|
||
meta = result.meta[:groups][group] | ||
thread_meta = meta[:threads].values.first | ||
expect(thread_meta.keys).not_to include(:topic_partitions) | ||
end | ||
end | ||
end | ||
end |