diff --git a/logstash-output-opentsdb.gemspec b/logstash-output-opentsdb.gemspec index 10459c1..81ac75c 100644 --- a/logstash-output-opentsdb.gemspec +++ b/logstash-output-opentsdb.gemspec @@ -23,5 +23,6 @@ Gem::Specification.new do |s| s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0" s.add_development_dependency 'logstash-devutils' + s.add_development_dependency 'logstash-codec-plain' end diff --git a/spec/outputs/opentsdb_spec.rb b/spec/outputs/opentsdb_spec.rb index 6bf5945..75cae98 100644 --- a/spec/outputs/opentsdb_spec.rb +++ b/spec/outputs/opentsdb_spec.rb @@ -1 +1,62 @@ -require "logstash/devutils/rspec/spec_helper" +require 'logstash/devutils/rspec/spec_helper' +require 'logstash/outputs/opentsdb' + +require_relative '../spec_helper' + +describe LogStash::Outputs::Opentsdb do + + before do + subject.register + subject.receive event + end + + subject { LogStash::Outputs::Opentsdb.new(config) } + + context 'single metric, no tags' do + let(:config) do + { + 'metrics' => ['metric_1', '%{val}'] + } + end + + let(:event) { LogStash::Event.new('val' => 123) } + + it 'sends the correct opentsdb format' do + expect(subject.socket.pop).to match(/^put metric_1 \d+ 123 \n/) + end + end + + context 'single metric, multiple tags' do + let(:config) do + { + 'metrics' => ['metric_1', '%{val}', 'tag1', '%{tag1}', 'tag2', '%{tag2}'] + } + end + + let(:event) { LogStash::Event.new('val' => 123, 'tag1' => 'foo', 'tag2' => 'bar') } + + it 'sends the correct opentsdb format' do + expect(subject.socket.pop).to match(/^put metric_1 \d+ 123 tag1=foo tag2=bar\n/) + end + end + + context 'socket.puts fails with ECONNRESET' do + let(:config) do + { + 'metrics' => ['metric_1', '%{val}'] + } + end + let(:event) { LogStash::Event.new('val' => 123) } + + before do + subject.register + end + + it 'tries to reconnect' do + allow(subject.socket).to receive(:puts).and_raise(Errno::ECONNRESET) + expect(subject).to receive(:connect) + + subject.receive event + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..f877d5b --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,10 @@ +require_relative 'support/server' + +class LogStash::Outputs::Opentsdb + attr_reader :socket + + # Mock connect to return a fake server + def connect + @socket = Mocks::Server.new + end +end diff --git a/spec/support/server.rb b/spec/support/server.rb new file mode 100644 index 0000000..002452c --- /dev/null +++ b/spec/support/server.rb @@ -0,0 +1,29 @@ +module Mocks + class Server + + def initialize + @queue = Array.new + end + + def size + @queue.length + end + + def pop + @queue.pop + end + + def stop + end + + def empty? + @queue.empty? + end + + def puts(data) + data.split("\n").each do |line| + @queue << "#{line}\n" + end + end + end +end