Skip to content

Invalid tags exceptions #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/opentsdb/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/opentsdb/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
92 changes: 56 additions & 36 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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