Skip to content

Commit

Permalink
Ignore nil tags and convert symbol ones
Browse files Browse the repository at this point in the history
  • Loading branch information
pschambacher committed Jun 22, 2017
1 parent d77b4bb commit 7f8d8b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/datadog/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def port=(port) #:nodoc:

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

# Sends an increment (count = 1) for the given stat to the statsd server.
Expand Down Expand Up @@ -358,10 +358,11 @@ def escape_event_content(msg)
end

def escape_tag_content(tag)
remove_pipes(tag).gsub COMMA, BLANK
remove_pipes(tag.to_s).gsub COMMA, BLANK
end

def escape_tag_content!(tag)
tag = tag.to_s
tag.gsub!(PIPE, BLANK)
tag.gsub!(COMMA, BLANK)
tag
Expand Down
16 changes: 16 additions & 0 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ class Datadog::Statsd
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
end
end

describe "#increment" do
Expand Down

0 comments on commit 7f8d8b7

Please sign in to comment.