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

port / host / tags / namespace can no longer be set on the instance t… #87

Merged
merged 1 commit into from
May 31, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## NEXT

### Breaking changes

* port / host / tags / namespace can no longer be set on the instance to allow thread-safety [#87][] by [@grosser][]

## 3.3.0 / 2018.02.04

* [FEATURE] Add distribution support (beta). See [#72][].
Expand Down
31 changes: 10 additions & 21 deletions lib/datadog/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,22 @@ def self.VERSION
# @option opts [Array<String>] :tags tags to be added to every metric
# @option opts [Integer] :max_buffer_size max messages to buffer
def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, opts = EMPTY_OPTIONS)
self.host, self.port = host, port
@host = host || DEFAULT_HOST
@port = port || DEFAULT_PORT

@socket_path = opts[:socket_path]
@prefix = nil
@socket = connect_to_socket if @socket_path.nil?
self.namespace = opts[:namespace]
self.tags = opts[:tags]
@buffer = Array.new
@max_buffer_size = opts[:max_buffer_size] || 50
@batch_nesting_depth = 0
end

def namespace=(namespace) #:nodoc:
@namespace = namespace
@prefix = namespace.nil? ? nil : "#{namespace}.".freeze
end

def host=(host) #:nodoc:
@host = host || DEFAULT_HOST
end

def port=(port) #:nodoc:
@port = port || DEFAULT_PORT
end
@namespace = opts[:namespace]
@prefix = @namespace ? "#{@namespace}.".freeze : nil

def tags=(tags) #:nodoc:
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
@batch_nesting_depth = 0
end

# Sends an increment (count = 1) for the given stat to the statsd server.
Expand Down
97 changes: 20 additions & 77 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Datadog::Statsd
attr_accessor :socket
end

let(:namespace) { nil }

before do
@statsd = Datadog::Statsd.new('localhost', 1234)
@statsd = Datadog::Statsd.new('localhost', 1234, namespace: namespace)
@statsd.socket = FakeUDPSocket.new
Datadog::Statsd.logger = nil
end
Expand All @@ -25,102 +27,49 @@ class Datadog::Statsd
end

describe "#initialize" do
it "should set the host and port" do
it "sets the host and port" do
@statsd.host.must_equal 'localhost'
@statsd.port.must_equal 1234
end

it "should create a UDPSocket when nothing is given" do
statsd = Datadog::Statsd.new
statsd.socket.must_be_instance_of(UDPSocket)
it "uses default host and port when nil is given to allow only passing options" do
@statsd = Datadog::Statsd.new(nil, nil, {})
@statsd.host.must_equal '127.0.0.1'
@statsd.port.must_equal 8125
end

it "should create a UDPSocket when host and port are given" do
statsd = Datadog::Statsd.new('localhost', 1234)
it "creates a UDPSocket when nothing is given" do
statsd = Datadog::Statsd.new
statsd.socket.must_be_instance_of(UDPSocket)
end

it "should not create a socket when socket_path is given" do
it "does not create a socket when socket_path is given" do
# the socket may not exist when creating the Statsd object
statsd = Datadog::Statsd.new('localhost', 1234, {socket_path: '/tmp/socket'})
assert_nil statsd.socket
end

it "should default the host to 127.0.0.1, port to 8125, namespace to nil, and tags to []" do
it "defaults host, port, namespace, and tags" do
statsd = Datadog::Statsd.new
statsd.host.must_equal '127.0.0.1'
statsd.port.must_equal 8125
assert_nil statsd.namespace
statsd.tags.must_equal []
end

it 'should be able to set host, port, namespace, and global tags' do
it 'sets host, port, namespace, and tags' do
statsd = Datadog::Statsd.new '1.3.3.7', 8126, :tags => %w(global), :namespace => 'space'
statsd.host.must_equal '1.3.3.7'
statsd.port.must_equal 8126
statsd.namespace.must_equal 'space'
statsd.instance_variable_get('@prefix').must_equal 'space.'
statsd.tags.must_equal ['global']
end
end

describe "writers" do
it "should set host, port, namespace, and global tags" do
@statsd.host = '1.2.3.4'
@statsd.port = 5678
@statsd.namespace = 'n4m35p4c3'
@statsd.tags = ['t4g5']

@statsd.host.must_equal '1.2.3.4'
@statsd.port.must_equal 5678
@statsd.namespace.must_equal 'n4m35p4c3'
@statsd.tags.must_equal ['t4g5']
end

it "should not resolve hostnames to IPs" do
@statsd.host = 'localhost'
@statsd.host.must_equal 'localhost'
end

it "should set nil host to default" do
@statsd.host = nil
@statsd.host.must_equal '127.0.0.1'
end

it "should set nil port to default" do
@statsd.port = nil
@statsd.port.must_equal 8125
end

it 'should set prefix to nil when namespace is set to nil' do
@statsd.namespace = nil
assert_nil @statsd.namespace
assert_nil @statsd.instance_variable_get('@prefix')
end

it 'should set nil tags to default' do
@statsd.tags = nil
@statsd.tags.must_equal []
end

it 'should reject non-array tags' do
lambda { @statsd.tags = 'tsdfs' }.must_raise ArgumentError
end

it 'ignore nil tags' do
@statsd.tags = ['tag1', nil, 'tag2']
@statsd.tags.must_equal %w[tag1 tag2]
end

it 'converts symbols to strings' do
@statsd.tags = [:tag1, :tag2]
@statsd.tags.must_equal %w[tag1 tag2]
end

it 'assigns regular tags' do
tags = %w[tag1 tag2]
@statsd.tags = tags
@statsd.tags.must_equal tags
it 'fails on invalid tags' do
assert_raises ArgumentError do
Datadog::Statsd.new nil, nil, :tags => 'global'
end
end
end

Expand Down Expand Up @@ -395,7 +344,7 @@ def helper_time_return
end

describe "with namespace" do
before { @statsd.namespace = 'service' }
let(:namespace) { 'service' }

it "should add namespace to increment" do
@statsd.increment('foobar')
Expand Down Expand Up @@ -680,22 +629,16 @@ def send(*)
end

it "global tags setter" do
@statsd.tags = %w(country:usa other)
@statsd.instance_variable_set(:@tags, %w(country:usa other))
@statsd.increment("c")
@statsd.socket.recv.must_equal ['c:1|c|#country:usa,other']
end

it "global tags setter and regular tags" do
@statsd.tags = %w(country:usa other)
@statsd.instance_variable_set(:@tags, %w(country:usa other))
@statsd.increment("c", :tags=>%w(somethingelse))
@statsd.socket.recv.must_equal ['c:1|c|#country:usa,other,somethingelse']
end

it "nil global tags" do
@statsd.tags = nil
@statsd.increment("c")
@statsd.socket.recv.must_equal ['c:1|c']
end
end

describe "batched" do
Expand Down