Skip to content

Commit

Permalink
Add support for a minimum TTL for events
Browse files Browse the repository at this point in the history
When running with riemann-wrappers, one may want to run different tools
with distinct ttl, but the `--ttl` flag cannot be specified twice, so
the following configuration will not work:

```
---
options: --ttl 300
tools:
- name: "health"
- name: "rdap"
  options: "--interval 21600 --ttl 86400 --domains example.com example.net"
```

A workaround is to add an explicit ttl for each tool, but it is not
convenient.

Introduce a `--minimum-ttl` flag that can be used as a global option to
circumvent this issue.
  • Loading branch information
smortex committed Jun 29, 2024
1 parent 100b59c commit ed0417d
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 ed0417d

Please sign in to comment.