Skip to content

Commit

Permalink
Merge pull request #294 from riemann/add-minimum-ttl
Browse files Browse the repository at this point in the history
Add support for a minimum TTL for events
  • Loading branch information
jamtur01 authored Jun 29, 2024
2 parents c4ef790 + ed0417d commit 649770d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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

0 comments on commit 649770d

Please sign in to comment.