Skip to content

Commit

Permalink
replace reserved characters in stat names
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkittelson committed Nov 11, 2014
1 parent 0f7abd7 commit d37886b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/dogstatsd/dogstatsd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ defmodule DogStatsd.DogStatsd do
def format_stats(_dogstatsd, _stat, _delta, _type, %{:sample_rate => sr, :sample => s}) when s > sr, do: nil
def format_stats(dogstatsd, stat, delta, type, %{:sample => s} = opts), do: format_stats(dogstatsd, stat, delta, type, Map.delete(opts, :sample))
def format_stats(dogstatsd, stat, delta, type, %{:sample_rate => sr} = opts) do
"#{DogStatsd.prefix(dogstatsd)}#{stat}:#{delta}|#{type}|@#{sr}"
"#{DogStatsd.prefix(dogstatsd)}#{format_stat(stat)}:#{delta}|#{type}|@#{sr}"
|> add_tags(opts[:tags])
end
def format_stats(dogstatsd, stat, delta, type, opts) do
"#{DogStatsd.prefix(dogstatsd)}#{stat}:#{delta}|#{type}"
"#{DogStatsd.prefix(dogstatsd)}#{format_stat(stat)}:#{delta}|#{type}"
|> add_tags(opts[:tags])
end

def format_stat(stat) do
String.replace stat, ~r/[:|@]/, "_"
end

def send_to_socket(_dogstatsd, nil), do: nil
def send_to_socket(_dogstatsd, message) when byte_size(message) > 8 * 1024, do: nil
def send_to_socket(dogstatsd, message) do
Expand Down
10 changes: 10 additions & 0 deletions test/dogstatsd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,14 @@ defmodule DogStatsdTest do
assert_receive {:udp, _port, _from_ip, _from_port, 'service.foobar:500|g'}
end


############
# stat names
############

test "replaces statsd reserved chars in the stat name" do
DogStatsd.increment(:dogstatsd, "ray@hostname.blah|blah.blah:blah")
assert_receive {:udp, _port, _from_ip, _from_port, 'ray_hostname.blah_blah.blah_blah:1|c'}
end

end

0 comments on commit d37886b

Please sign in to comment.