Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a minimum TTL for events #294

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/riemann/tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def options
opt :event_host, 'Event hostname', type: String
opt :interval, 'Seconds between updates', default: 5
opt :tag, 'Tag to add to events', type: String, multi: true
opt :ttl, 'TTL for events', type: Integer
opt :ttl, 'TTL for events (twice the interval when unspecified)', type: Integer
opt :minimum_ttl, 'Minimum TTL for events', type: Integer, short: :none
opt :attribute, 'Attribute to add to the event', type: String, multi: true
opt :timeout, 'Timeout (in seconds) when waiting for acknowledgements', default: 30
opt :tcp, 'Use TCP transport instead of UDP (improves reliability, slight overhead.', default: true
Expand All @@ -50,6 +51,9 @@ def initialize(allow_arguments: false)
options
@argv = ARGV.dup
abort "Error: stray arguments: #{ARGV.map(&:inspect).join(', ')}" if ARGV.any? && !allow_arguments

options[:ttl] ||= options[:interval] * 2
options[:ttl] = [options[:minimum_ttl], options[:ttl]].compact.max
end

# Returns parsed options (cached) from command line.
Expand All @@ -68,7 +72,7 @@ def attributes
def report(event)
event[:tags] = event.fetch(:tags, []) + options[:tag]

event[:ttl] ||= options[:ttl] || (options[:interval] * 2)
event[:ttl] ||= options[:ttl]

event[:host] = options[:event_host].dup if options[:event_host]

Expand Down
32 changes: 32 additions & 0 deletions spec/riemann/tools_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'riemann/tools/health'

class ExampleTool
include Riemann::Tools
end

RSpec.describe Riemann::Tools do
describe '#options' do
describe ':ttl' do
subject(:tool) { ExampleTool.new }

{
'' => 10,
'--ttl=60' => 60,
'--interval 10' => 20,
'--minimum-ttl 300' => 300,
'--minimum-ttl 300 --ttl=60' => 300,
'--minimum-ttl 30 --ttl 60' => 60,
}.each do |argv, expected_ttl|
context "with ARGV=\"#{argv}\"" do
before do
ARGV.replace argv.split
end

it { expect(tool.options[:ttl]).to eq expected_ttl }
end
end
end
end
end