From ed0417dc753ae5e213c44661ca0f0242b1a0911e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Thu, 6 Jun 2024 08:40:55 -1000 Subject: [PATCH] Add support for a minimum TTL for events 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. --- lib/riemann/tools.rb | 8 ++++++-- spec/riemann/tools_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 spec/riemann/tools_spec.rb diff --git a/lib/riemann/tools.rb b/lib/riemann/tools.rb index fe4f14f3..e2376036 100644 --- a/lib/riemann/tools.rb +++ b/lib/riemann/tools.rb @@ -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 @@ -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. @@ -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] diff --git a/spec/riemann/tools_spec.rb b/spec/riemann/tools_spec.rb new file mode 100644 index 00000000..da82ee4a --- /dev/null +++ b/spec/riemann/tools_spec.rb @@ -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