diff --git a/README.md b/README.md index 981f972..a39e0e4 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,11 @@ ruby 1.9.3+ tags: { factor: 'awesome', host: 'ponies' } } + # Query one metric @client.put(sample) + + # Query many metrics + @client.put([sample, sample]) ``` ## License diff --git a/lib/opentsdb/client.rb b/lib/opentsdb/client.rb index 1fb4a6c..f52ce94 100644 --- a/lib/opentsdb/client.rb +++ b/lib/opentsdb/client.rb @@ -16,6 +16,10 @@ def initialize(options = {}) end def to_command(options) + if !options.key?(:tags) || !options[:tags].is_a?(Hash) || options[:tags].empty? + fail OpenTSDB::Errors::InvalidTagsError + end + timestamp = options[:timestamp].to_i metric_name = options[:metric] value = options[:value].to_f diff --git a/lib/opentsdb/errors.rb b/lib/opentsdb/errors.rb index 2ede806..ba9ace8 100644 --- a/lib/opentsdb/errors.rb +++ b/lib/opentsdb/errors.rb @@ -5,5 +5,11 @@ def to_s 'Unable to connect or invalid connection data' end end + + class InvalidTagsError < StandardError + def to_s + 'The tags you provided are invalid. Note that you need at least one tag to proceed.' + end + end end end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index f648d2f..fba86e3 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -8,8 +8,12 @@ allow(TCPSocket).to receive(:new).and_return(socket) end - describe 'error handling' do - subject { described_class.new } + describe '#initialize' do + subject { client } + + it 'creates a new client' do + expect { subject }.to_not raise_error + end it 'raises a custom error if the connection failed' do expect(TCPSocket).to receive(:new).and_raise('connection error') @@ -18,43 +22,59 @@ end end - it 'creates a new client' do - expect { client }.to_not raise_error - end + describe '#put' do + let(:metric) do + { timestamp: 1_411_104_988, metric: 'users', value: 100, tags: { foo: 'bar' } } + end + + let(:other_metric) do + { timestamp: 1_411_104_999, metric: 'users', value: 150, tags: { bar: 'baz' } } + end - it 'writes a single metric to the socket' do - expect(socket).to receive(:puts).with('put users 1411104988 100.0 foo=bar') + describe 'error handling' do + context 'no tags' do + subject { client.put({}) } - metric = { - timestamp: 1411104988, - metric: 'users', - value: 100, - tags: { foo: 'bar' }, - } + it { expect { subject }.to raise_error(OpenTSDB::Errors::InvalidTagsError) } + end - client.put(metric) - end + context 'tags' do + subject { client.put(metric.merge(tags: tags)) } + + context 'empty tags' do + let(:tags) { {} } + + it { expect { subject }.to raise_error(OpenTSDB::Errors::InvalidTagsError) } + end + + context 'invalid tags' do + let(:tags) { [:test, 123] } - it 'writes multiple metrics to the socket' do - expect(socket).to receive(:puts).with( - "put users 1411104988 100.0 foo=bar\nput users 1411104999 150.0 bar=baz" - ) - - metrics = [ - { - timestamp: 1411104988, - metric: 'users', - value: 100, - tags: { foo: 'bar' }, - }, - { - timestamp: 1411104999, - metric: 'users', - value: 150, - tags: { bar: 'baz' }, - }, - ] - - client.put(metrics) + it { expect { subject }.to raise_error(OpenTSDB::Errors::InvalidTagsError) } + end + end + end + + context 'one metric' do + subject { client.put(metric) } + + it 'writes a single metric to the socket' do + expect(socket).to receive(:puts).with('put users 1411104988 100.0 foo=bar') + + subject + end + end + + context 'many metrics' do + subject { client.put([metric, other_metric]) } + + it 'writes multiple metrics to the socket' do + expect(socket).to receive(:puts).with( + "put users 1411104988 100.0 foo=bar\nput users 1411104999 150.0 bar=baz" + ) + + subject + end + end end end