Skip to content

Commit 90d8cfe

Browse files
author
David Staehler
committed
Remove logging of events
1 parent e56ec36 commit 90d8cfe

File tree

6 files changed

+128
-71
lines changed

6 files changed

+128
-71
lines changed

.formatter.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
inputs: ["mix.exs", "*.{ex,exs}"]
3+
]

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/_build
2+
/cover
3+
/deps
4+
/doc
5+
/.fetch
6+
erl_crash.dump
7+
*.ez
8+
*.beam
9+
/config/*.secret.exs
10+
.env
11+
.elixir_ls
12+
113
/_build
214
/deps
315
erl_crash.dump

lib/dogstatsd/batched.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ defmodule DogStatsd.Batched do
33

44
def send_to_socket(dogstatsd, message) do
55
buffer = DogStatsd.add_to_buffer(dogstatsd, message)
6-
if length(buffer) == DogStatsd.max_buffer_size(dogstatsd) do
6+
7+
if length(buffer) == DogStatsd.max_buffer_size(dogstatsd) do
78
DogStatsd.flush_buffer(dogstatsd)
89
end
910
end
10-
end
11+
end

lib/dogstatsd/statsd.ex

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,102 @@
11
defmodule DogStatsd.Statsd do
2-
32
defmacro __using__(_opts) do
43
quote do
54
require Logger
65

76
def gauge(dogstatsd, stat, value, opts \\ %{}) do
8-
send_stats dogstatsd, stat, value, :g, opts
7+
send_stats(dogstatsd, stat, value, :g, opts)
98
end
109

1110
def event(dogstatsd, title, text, opts \\ %{}) do
1211
event_string = format_event(title, text, opts)
1312

1413
if byte_size(event_string) > 8 * 1024 do
15-
Logger.warn "Event #{title} payload is too big (more that 8KB), event discarded"
14+
Logger.warn("Event #{title} payload is too big (more that 8KB), event discarded")
1615
end
1716

18-
send_to_socket dogstatsd, event_string
17+
send_to_socket(dogstatsd, event_string)
1918
end
2019

2120
def format_event(title, text, opts \\ %{}) do
2221
title = escape_event_content(title)
23-
text = escape_event_content(text)
22+
text = escape_event_content(text)
2423

2524
add_opts("_e{#{String.length(title)},#{String.length(text)}}:#{title}|#{text}", opts)
2625
end
2726

28-
def add_opts(event, %{:date_happened => opt} = opts), do: add_opts("#{event}|d:#{rm_pipes(opt)}", Map.delete(opts, :date_happened))
29-
def add_opts(event, %{:hostname => opt} = opts), do: add_opts("#{event}|h:#{rm_pipes(opt)}", Map.delete(opts, :hostname))
30-
def add_opts(event, %{:aggregation_key => opt} = opts), do: add_opts("#{event}|k:#{rm_pipes(opt)}", Map.delete(opts, :aggregation_key))
31-
def add_opts(event, %{:priority => opt} = opts), do: add_opts("#{event}|p:#{rm_pipes(opt)}", Map.delete(opts, :priority))
32-
def add_opts(event, %{:source_type_name => opt} = opts), do: add_opts("#{event}|s:#{rm_pipes(opt)}", Map.delete(opts, :source_type_name))
33-
def add_opts(event, %{:alert_type => opt} = opts), do: add_opts("#{event}|t:#{rm_pipes(opt)}", Map.delete(opts, :alert_type))
27+
def add_opts(event, %{:date_happened => opt} = opts),
28+
do: add_opts("#{event}|d:#{rm_pipes(opt)}", Map.delete(opts, :date_happened))
29+
30+
def add_opts(event, %{:hostname => opt} = opts),
31+
do: add_opts("#{event}|h:#{rm_pipes(opt)}", Map.delete(opts, :hostname))
32+
33+
def add_opts(event, %{:aggregation_key => opt} = opts),
34+
do: add_opts("#{event}|k:#{rm_pipes(opt)}", Map.delete(opts, :aggregation_key))
35+
36+
def add_opts(event, %{:priority => opt} = opts),
37+
do: add_opts("#{event}|p:#{rm_pipes(opt)}", Map.delete(opts, :priority))
38+
39+
def add_opts(event, %{:source_type_name => opt} = opts),
40+
do: add_opts("#{event}|s:#{rm_pipes(opt)}", Map.delete(opts, :source_type_name))
41+
42+
def add_opts(event, %{:alert_type => opt} = opts),
43+
do: add_opts("#{event}|t:#{rm_pipes(opt)}", Map.delete(opts, :alert_type))
44+
3445
def add_opts(event, %{} = opts), do: add_tags(event, opts[:tags])
3546

3647
def add_tags(event, nil), do: event
37-
def add_tags(event, []), do: event
48+
def add_tags(event, []), do: event
49+
3850
def add_tags(event, tags) do
39-
tags = tags
40-
|> Enum.map(&rm_pipes/1)
41-
|> Enum.join(",")
51+
tags =
52+
tags
53+
|> Enum.map(&rm_pipes/1)
54+
|> Enum.join(",")
4255

4356
"#{event}|##{tags}"
4457
end
4558

4659
def increment(dogstatsd, stat, opts \\ %{}) do
47-
count dogstatsd, stat, 1, opts
60+
count(dogstatsd, stat, 1, opts)
4861
end
4962

5063
def decrement(dogstatsd, stat, opts \\ %{}) do
51-
count dogstatsd, stat, -1, opts
64+
count(dogstatsd, stat, -1, opts)
5265
end
5366

5467
def count(dogstatsd, stat, count, opts \\ %{}) do
55-
send_stats dogstatsd, stat, count, :c, opts
68+
send_stats(dogstatsd, stat, count, :c, opts)
5669
end
5770

5871
def histogram(dogstatsd, stat, value, opts \\ %{}) do
59-
send_stats dogstatsd, stat, value, :h, opts
72+
send_stats(dogstatsd, stat, value, :h, opts)
6073
end
6174

6275
def timing(dogstatsd, stat, ms, opts \\ %{}) do
63-
send_stats dogstatsd, stat, ms, :ms, opts
76+
send_stats(dogstatsd, stat, ms, :ms, opts)
6477
end
6578

6679
defmacro time(dogstatsd, stat, opts \\ Macro.escape(%{}), do_block) do
6780
quote do
68-
function = fn -> unquote do_block[:do] end
81+
function = fn -> unquote(do_block[:do]) end
6982

7083
{elapsed, result} = :timer.tc(DogStatsd, :_time_apply, [function])
71-
DogStatsd.timing(unquote(dogstatsd), unquote(stat), trunc(elapsed / 1000), unquote(opts))
84+
85+
DogStatsd.timing(
86+
unquote(dogstatsd),
87+
unquote(stat),
88+
trunc(elapsed / 1000),
89+
unquote(opts)
90+
)
91+
7292
result
7393
end
7494
end
7595

7696
def _time_apply(function), do: function.()
7797

7898
def set(dogstatsd, stat, value, opts \\ %{}) do
79-
send_stats dogstatsd, stat, value, :s, opts
99+
send_stats(dogstatsd, stat, value, :s, opts)
80100
end
81101

82102
def batch(dogstatsd, function) do
@@ -85,45 +105,65 @@ defmodule DogStatsd.Statsd do
85105
end
86106

87107
def send_stats(dogstatsd, stat, delta, type, opts \\ %{})
108+
88109
def send_stats(dogstatsd, stat, delta, type, %{:sample_rate => _sample_rate} = opts) do
89-
opts = Map.put(opts, :sample, :rand.uniform)
90-
send_to_socket dogstatsd, get_global_tags_and_format_stats(dogstatsd, stat, delta, type, opts)
110+
opts = Map.put(opts, :sample, :rand.uniform())
111+
112+
send_to_socket(
113+
dogstatsd,
114+
get_global_tags_and_format_stats(dogstatsd, stat, delta, type, opts)
115+
)
91116
end
117+
92118
def send_stats(dogstatsd, stat, delta, type, opts) do
93-
send_to_socket dogstatsd, get_global_tags_and_format_stats(dogstatsd, stat, delta, type, opts)
119+
send_to_socket(
120+
dogstatsd,
121+
get_global_tags_and_format_stats(dogstatsd, stat, delta, type, opts)
122+
)
94123
end
95124

96125
def get_global_tags_and_format_stats(dogstatsd, stat, delta, type, opts) do
97-
opts = update_in opts, [:tags], &((DogStatsd.tags(dogstatsd) ++ (&1 || [])) |> Enum.uniq)
126+
opts =
127+
update_in(opts, [:tags], &((DogStatsd.tags(dogstatsd) ++ (&1 || [])) |> Enum.uniq()))
128+
98129
format_stats(dogstatsd, stat, delta, type, opts)
99130
end
100131

101-
def format_stats(_dogstatsd, _stat, _delta, _type, %{:sample_rate => sr, :sample => s}) when s > sr, do: nil
102-
def format_stats(dogstatsd, stat, delta, type, %{:sample => s} = opts), do: format_stats(dogstatsd, stat, delta, type, Map.delete(opts, :sample))
132+
def format_stats(_dogstatsd, _stat, _delta, _type, %{:sample_rate => sr, :sample => s})
133+
when s > sr,
134+
do: nil
135+
136+
def format_stats(dogstatsd, stat, delta, type, %{:sample => s} = opts),
137+
do: format_stats(dogstatsd, stat, delta, type, Map.delete(opts, :sample))
138+
103139
def format_stats(dogstatsd, stat, delta, type, %{:sample_rate => sr} = opts) do
104140
"#{DogStatsd.prefix(dogstatsd)}#{format_stat(stat)}:#{delta}|#{type}|@#{sr}"
105141
|> add_tags(opts[:tags])
106142
end
143+
107144
def format_stats(dogstatsd, stat, delta, type, opts) do
108145
"#{DogStatsd.prefix(dogstatsd)}#{format_stat(stat)}:#{delta}|#{type}"
109146
|> add_tags(opts[:tags])
110147
end
111148

112149
def format_stat(stat) do
113-
String.replace stat, ~r/[:|@]/, "_"
150+
String.replace(stat, ~r/[:|@]/, "_")
114151
end
115152

116153
def send_to_socket(_dogstatsd, nil), do: nil
117154
def send_to_socket(_dogstatsd, []), do: nil
118155
def send_to_socket(_dogstatsd, message) when byte_size(message) > 8 * 1024, do: nil
119-
def send_to_socket(dogstatsd, message) do
120-
Logger.debug "DogStatsd: #{message}"
121156

157+
def send_to_socket(dogstatsd, message) do
122158
{:ok, socket} = :gen_udp.open(0)
123-
:gen_udp.send(socket,
124-
host(dogstatsd) |> String.to_char_list,
125-
port(dogstatsd),
126-
message |> String.to_char_list)
159+
160+
:gen_udp.send(
161+
socket,
162+
host(dogstatsd) |> String.to_charlist(),
163+
port(dogstatsd),
164+
message |> String.to_char_list()
165+
)
166+
127167
:gen_udp.close(socket)
128168
end
129169

@@ -135,9 +175,7 @@ defmodule DogStatsd.Statsd do
135175
String.replace(msg, "|", "")
136176
end
137177

138-
defoverridable [send_to_socket: 2]
139-
178+
defoverridable send_to_socket: 2
140179
end
141180
end
142-
143-
end
181+
end

mix.exs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ defmodule DogStatsd.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[app: :dogstatsd,
6-
version: version(),
7-
elixir: "~> 1.0",
8-
test_coverage: [tool: ExCoveralls],
9-
deps: deps(),
10-
package: package(),
11-
description: "A client for DogStatsd, an extension of the StatsD metric server for Datadog."
12-
]
5+
[
6+
app: :dogstatsd,
7+
version: version(),
8+
elixir: "~> 1.0",
9+
test_coverage: [tool: ExCoveralls],
10+
deps: deps(),
11+
package: package(),
12+
description: "A client for DogStatsd, an extension of the StatsD metric server for Datadog."
13+
]
1314
end
1415

1516
# Configuration for the OTP application
@@ -37,18 +38,18 @@ defmodule DogStatsd.Mixfile do
3738

3839
defp package do
3940
[
40-
name: :dogstatsd,
41-
files: ["lib/*", "mix.exs", "README.md", "LICENSE.md", "CHANGELOG.md", "VERSION.yml"],
42-
maintainers: ["Adam Kittelson"],
43-
licenses: ["MIT"],
44-
links: %{ "GitHub" => "https://github.com/adamkittelson/dogstatsd-elixir" }
45-
]
41+
name: :dogstatsd,
42+
files: ["lib/*", "mix.exs", "README.md", "LICENSE.md", "CHANGELOG.md", "VERSION.yml"],
43+
maintainers: ["Adam Kittelson"],
44+
licenses: ["MIT"],
45+
links: %{"GitHub" => "https://github.com/adamkittelson/dogstatsd-elixir"}
46+
]
4647
end
4748

4849
defp version do
49-
~r/[0-9]+/
50-
|> Regex.scan(File.read!("VERSION.yml"))
51-
|> List.flatten
52-
|> Enum.join(".")
50+
~r/[0-9]+/
51+
|> Regex.scan(File.read!("VERSION.yml"))
52+
|> List.flatten()
53+
|> Enum.join(".")
5354
end
5455
end

mix.lock

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
%{"certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []},
2-
"earmark": {:hex, :earmark, "1.2.0", "bf1ce17aea43ab62f6943b97bd6e3dc032ce45d4f787504e3adf738e54b42f3a", [:mix], []},
3-
"ex_doc": {:hex, :ex_doc, "0.15.1", "d5f9d588fd802152516fccfdb96d6073753f77314fcfee892b15b6724ca0d596", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
4-
"excoveralls": {:hex, :excoveralls, "0.6.3", "894bf9254890a4aac1d1165da08145a72700ff42d8cb6ce8195a584cb2a4b374", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
5-
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
6-
"hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
7-
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
1+
%{
2+
"certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], [], "hexpm", "44a5aa4261490a7d7fa6909ab4bcf14bff928a4fef49e80fc1e7a8fdb7b45f79"},
3+
"earmark": {:hex, :earmark, "1.2.0", "bf1ce17aea43ab62f6943b97bd6e3dc032ce45d4f787504e3adf738e54b42f3a", [:mix], [], "hexpm", "6709251dd10e70cca0d50be8a25adc38c701b39eac2da3b1c166e3e7e4d358ed"},
4+
"ex_doc": {:hex, :ex_doc, "0.15.1", "d5f9d588fd802152516fccfdb96d6073753f77314fcfee892b15b6724ca0d596", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm", "00d41bec3590533279b90870d68ebc4fdfcc3a1c7b6629796167b49bfa199d54"},
5+
"excoveralls": {:hex, :excoveralls, "0.6.3", "894bf9254890a4aac1d1165da08145a72700ff42d8cb6ce8195a584cb2a4b374", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "33b8e869a0afe422e2a05794ac67bed6570498173a02a80f0c88bc0fe3580287"},
6+
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "b55727b206dab96feb025267e5c122ddb448f55b6648f9156b8d481215d80290"},
7+
"hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "4.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "ec8309cb6d42251513492ef683d212c614d78b20594e5f4d89a05d8411dd0dea"},
8+
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], [], "hexpm", "f1b699f7275728538da7b5e35679f9e0f41ad8e0a49896e6a27b61867ed344eb"},
89
"jsex": {:hex, :jsex, "2.0.0", "d54a39e49418b34b1a78236a92f73e4d7c46329c220275d7223fab4b3dc6120f", [:mix], [{:jsx, "~> 2.0", [hex: :jsx, optional: false]}]},
9-
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], []},
10-
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
11-
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
12-
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
10+
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], [], "hexpm", "b4c5d3230b397c8d95579e4a3d72826bb6463160130ccf4182f5be8579b5f44c"},
11+
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
12+
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm", "7a4c8e1115a2732a67d7624e28cf6c9f30c66711a9e92928e745c255887ba465"},
13+
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm", "4f8805eb5c8a939cf2359367cb651a3180b27dfb48444846be2613d79355d65e"},
14+
}

0 commit comments

Comments
 (0)