Skip to content

Commit 8d07cd8

Browse files
authored
Fix handling of empty and missing tag values with standard formatter (#86)
Fixes #81
1 parent 29e0a20 commit 8d07cd8

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

lib/telemetry_metrics_statsd/formatter/standard.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ defmodule TelemetryMetricsStatsd.Formatter.Standard do
5858
end
5959

6060
defp do_format_metric_tags([{_, nil} | tags]) do
61-
[?., "nil" | format_metric_tags(tags)]
61+
[?., "nil" | do_format_metric_tags(tags)]
6262
end
6363

6464
defp do_format_metric_tags([{_, tag_value} | tags]) do
65-
[?., to_string(tag_value) | format_metric_tags(tags)]
65+
[?., to_string(tag_value) | do_format_metric_tags(tags)]
6666
end
6767

6868
defp format_metric_value(%Metrics.Counter{}, _value), do: "1|c"

test/telemetry_metrics_statsd/formatter/standard_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ defmodule TelemetryMetricsStatsd.Formatter.StandardTest do
6464
test "nil tags are included in the formatted metric" do
6565
m = given_last_value("my.awesome.metric", tags: [:method, :status])
6666

67-
assert format(m, 131, method: nil, status: 200) ==
68-
"my.awesome.metric.nil.200:131|g"
67+
assert format(m, 131, method: "GET", status: nil) ==
68+
"my.awesome.metric.GET.nil:131|g"
6969
end
7070

7171
test "empty string tag values are dropped" do
7272
m = given_last_value("my.awesome.metric", tags: [:method, :status])
7373

74-
assert format(m, 131, method: "", status: 200) == ""
74+
assert format(m, 131, method: "GET", status: "") == ""
7575
end
7676

7777
test "tags passed as explicit argument are used for the formatted metric" do

test/telemetry_metrics_statsd_test.exs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,45 @@ defmodule TelemetryMetricsStatsdTest do
144144
"invalid value for :formatter option: expected :formatter be either :standard or :datadog, got :my_formatter"
145145
end
146146

147-
test "it doesn't crash when tag values are missing" do
147+
test "it doesn't crash when tag values are missing with standard formatter" do
148+
{socket, port} = given_udp_port_opened()
149+
150+
counter = given_counter("http.request.count", tags: [:method, :status])
151+
152+
start_reporter(
153+
metrics: [counter],
154+
port: port
155+
)
156+
157+
handlers_before = :telemetry.list_handlers([])
158+
159+
:telemetry.execute([:http, :request], %{latency: 172}, %{method: "GET"})
160+
assert_reported(socket, "")
161+
162+
handlers_after = :telemetry.list_handlers([])
163+
assert handlers_after == handlers_before
164+
end
165+
166+
test "it doesn't crash when tag values are nil with standard formatter" do
167+
{socket, port} = given_udp_port_opened()
168+
169+
counter = given_counter("http.request.count", tags: [:method, :status])
170+
171+
start_reporter(
172+
metrics: [counter],
173+
port: port
174+
)
175+
176+
handlers_before = :telemetry.list_handlers([])
177+
178+
:telemetry.execute([:http, :request], %{latency: 172}, %{method: "GET", status: nil})
179+
assert_reported(socket, "http.request.count.GET.nil:1|c")
180+
181+
handlers_after = :telemetry.list_handlers([])
182+
assert handlers_after == handlers_before
183+
end
184+
185+
test "it doesn't crash when tag values are missing with DataDog formatter" do
148186
{socket, port} = given_udp_port_opened()
149187

150188
counter = given_counter("http.request.count", tags: [:method, :status])

0 commit comments

Comments
 (0)