Skip to content
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

fail fast on unknown options #93

Merged
merged 2 commits into from
Jun 7, 2018
Merged
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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AllCops:
DisplayCopNames: true
TargetRubyVersion: 1.9
TargetRubyVersion: 2.0

LineLength:
Max: 120
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
sudo: false
language: ruby
rvm:
- 1.9
- 2.0
- 2.1
- 2.2
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem "yard", "~> 0.8.7.3"
gem 'single_cov'

if RUBY_VERSION >= "2.0.0"
gem 'rubocop', "~> 0.49.0" # bump this and TargetRubyVersion once we drop ruby 1.9
gem 'rubocop', "~> 0.50.0" # bump this and TargetRubyVersion once we drop ruby 2.0
end

if RUBY_VERSION >= "2.3.0"
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'bundler/gem_tasks'
require 'rake/testtask'
require 'yard'

task :default => :spec
task default: [:spec, :rubocop]

Rake::TestTask.new(:spec) do |spec|
spec.loader = :direct
Expand Down
1 change: 1 addition & 0 deletions dogstatsd-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ Gem::Specification.new do |s|
s.files = Dir["LICENSE.txt", "README.md", "lib/**/*.rb",]
s.homepage = "http://github.com/datadog/dogstatsd-ruby"
s.licenses = ["MIT"]
s.required_ruby_version = '>= 2.0.0'
end

33 changes: 20 additions & 13 deletions lib/datadog/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,33 @@ class Statsd

# @param [String] host your statsd host
# @param [Integer] port your statsd port
# @option opts [String] :namespace set a namespace to be prepended to every metric name
# @option opts [Array<String>] :tags tags to be added to every metric
# @option opts [Loger] :logger for debugging
# @option opts [Integer] :max_buffer_size max messages to buffer
def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, opts = EMPTY_OPTIONS)
# @option [String] namespace set a namespace to be prepended to every metric name
# @option [Array<String>] tags tags to be added to every metric
# @option [Loger] logger for debugging
# @option [Integer] max_buffer_size max messages to buffer
# @option [String] socket_path unix socket path
def initialize(
host = DEFAULT_HOST,
port = DEFAULT_PORT,
namespace: nil,
tags: nil,
max_buffer_size: nil,
socket_path: nil,
logger: nil
)
@host = host || DEFAULT_HOST
@port = port || DEFAULT_PORT
@logger = opts[:logger]

@socket_path = opts[:socket_path]
@logger = logger
@socket_path = socket_path

@namespace = opts[:namespace]
@namespace = namespace
@prefix = @namespace ? "#{@namespace}.".freeze : nil

tags = opts[:tags]
raise ArgumentError, 'tags must be a Array<String>' unless tags.nil? or tags.is_a? Array
@tags = (tags || []).compact.map! {|tag| escape_tag_content(tag)}

@buffer = Array.new
@max_buffer_size = opts[:max_buffer_size] || 50
@max_buffer_size = max_buffer_size || 50
@batch_nesting_depth = 0
end

Expand Down Expand Up @@ -461,7 +468,7 @@ def send_to_socket(message)
else
sock.sendmsg_nonblock(message)
end
rescue => boom
rescue StandardError => boom
# Give up on this socket if it looks like it is bad
bad_socket = !@socket_path.nil? && (
boom.is_a?(Errno::ECONNREFUSED) ||
Expand All @@ -480,7 +487,7 @@ def send_to_socket(message)
begin
@socket = connect_to_socket
retry
rescue => e
rescue StandardError => e
boom = e
end
end
Expand Down
16 changes: 12 additions & 4 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class Datadog::Statsd
Datadog::Statsd.new nil, nil, :tags => 'global'
end
end

it "fails on unknown options" do
assert_raises ArgumentError do
Datadog::Statsd.new nil, nil, :foo => 'bar'
end
end
end

describe "#increment" do
Expand Down Expand Up @@ -243,10 +249,12 @@ class Datadog::Statsd
end

it "should still time if block is failing" do
@statsd.time('foobar') do
stub_time 1
raise StandardError, 'This is failing'
end rescue
assert_raises StandardError do
@statsd.time('foobar') do
stub_time 1
raise StandardError, 'This is failing'
end
end
@statsd.socket.recv.must_equal ['foobar:1000|ms']
end

Expand Down